Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gem] Use decorators instead lifetime method #159

Closed
mantou132 opened this issue Jun 12, 2024 · 2 comments
Closed

[gem] Use decorators instead lifetime method #159

mantou132 opened this issue Jun 12, 2024 · 2 comments
Labels

Comments

@mantou132
Copy link
Owner

mantou132 commented Jun 12, 2024

Old:

@customElement('my-element')
class MyElement extends GemElement {
  #data: number;
  willMount = () => {
    this.memo(
      () => this.#data = ...,
      () => [this.attr]
    )
    this.effect(
      () => { ... },
      () => [this.attr]
    )
  }
}

New:

easy to extends

@willMount @mounted @shouldUpdate @template @updated @unmounted @effect @memo

@customElement('my-element')
class MyElement extends GemElement {
  @memo(() => [this.attr])
  get #data() {
    // slow exec
    return this.attr * 10000;
  }

  @effect((that) => [that.attr])
  async #effect() {
    await fetch('./')
    this.setState({})
  }
}

https://github.com/tc39/proposal-decorators?tab=readme-ov-file#will-decorators-let-you-access-private-fields-and-methods

tc39/proposal-decorators#509
microsoft/TypeScript#59482

test fail and example error: evanw/esbuild#3866

@mantou132
Copy link
Owner Author

mantou132 commented Jul 13, 2024

  • 要确保 @willMounted @mounted 执行在其他 memo effect 之前/之后 // 没必要
  • 装饰器回调要保持编写顺序(方法先于字段)? // 没必要

@mantou132 mantou132 unpinned this issue Jul 17, 2024
@mantou132 mantou132 removed the v2 label Jul 17, 2024
@mantou132 mantou132 pinned this issue Jul 19, 2024
mantou132 added a commit that referenced this issue Aug 5, 2024
移除 @Style #183
添加 @willMount @mounted @render 装饰器 #159
内部元素生命周期使用装饰器
添加 createState #179
mantou132 added a commit that referenced this issue Aug 5, 2024
移除 @Style #183
添加 @willMount @mounted @render 装饰器 #159
内部元素生命周期使用装饰器
添加 createState #179
mantou132 added a commit that referenced this issue Aug 5, 2024
移除 @Style #183
添加 @willMount @mounted @renderTemplate 装饰器 #159
内部元素生命周期使用装饰器
添加 createState #179
mantou132 added a commit that referenced this issue Aug 5, 2024
移除 @Style #183
添加 @willMount @mounted @renderTemplate 装饰器 #159
内部元素生命周期使用装饰器
添加 createState #179
@mantou132 mantou132 unpinned this issue Sep 1, 2024
@mantou132
Copy link
Owner Author

mantou132 commented Sep 3, 2024

应该同时保存生命周期和装饰器?,装饰器多了一行:

如果通过编译,可以按命名约定省区装饰器:#118

@customElement('dy-chart')
class DyChartElement extends GemElement { 
  @effect([] => [])
  #fetch = () => {}

  @template()
  #content = () => {
    return html`content`
  }

  @fallback()
  #errorBoundary = () => {
    return html``
  }
}
@customElement('dy-chart')
class DyChartElement extends GemElement { 
  @effect([] => [])
  #fetch = () => {}

  render= () => {
    return html`content`
  }

  fallback = () => {
    return html``
  }
}
function DyChart() {
  useEffect(() => {
  }, []) 

  if (xxx) {
    return <></>
  }

  return <>content</>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant