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

feat(atomic): add @watch decorator for Lit components #4875

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

y-lakhdar
Copy link
Contributor

Currently, there is no equivalent in Lit for Stencil’s Watch Decorator. Without this feature, monitoring changes to specific properties and triggering methods in response requires more boilerplate in Lit components.

Stencil Example with @watch

Using Stencil’s @Watch decorator, the code to monitor properties and invoke corresponding methods is concise:

  @Watch('searchHub')
  public updateSearchHub() {
    // ...
  }

  @Watch('pipeline')
  public updatePipeline() {
    // ...
  }

  @Watch('analytics')
  public toggleAnalytics() {
    // ...
  }

  @Watch('language')
  public updateLanguage() {
    // ...
  }

Equivalent Code in Lit Components

In Lit, achieving the same functionality requires additional logic in the willUpdate lifecycle method, as shown below:

  public updateSearchHub() {
    // ...
  }

  public updatePipeline() {
    // ...
  }

  public toggleAnalytics() {
    // ...
  }

  public updateLanguage() {
    // ...
  }

  async willUpdate(changedProperties: Map<string | number | symbol, unknown>) {
    if (changedProperties.has('searchHub')) {
      this.updateSearchHub();
    }
    if (changedProperties.has('pipeline')) {
      this.updatePipeline();
    }
    if (changedProperties.has('analytics')) {
      this.toggleAnalytics();
    }
    if (changedProperties.has('language')) {
      this.updateLanguage();
    }
  }

Introducing the @watch Decorator

To avoid the extra code, I added @watch decorator for Lit components that replicates the functionality of Stencil’s @Watch.

Cons

The decorator requires us to overwrite the willUpdate method which is considered protected.
Let me know if you think we should go with it?

Bonus of the watch decorator

Autocomplete Support: The decorator provides autocompletion for property names, enhancing developer experience.
image

https://coveord.atlassian.net/browse/KIT-3860

Copy link

Pull Request Report

PR Title

✅ Title follows the conventional commit spec.

Live demo links

Bundle Size

File Old (kb) New (kb) Change (%)
case-assist 243.8 243.8 0
commerce 355 355 0
search 414.9 414.9 0
insight 406.2 406.2 0
recommendation 256.1 256.1 0
ssr 408.8 408.8 0
ssr-commerce 372.7 372.7 0

SSR Progress

Use case SSR (#) CSR (#) Progress (%)
search 39 44 89
recommendation 0 4 0
case-assist 0 6 0
insight 0 27 0
commerce 0 15 0
Detailed logs search : buildInteractiveResult
search : buildInteractiveInstantResult
search : buildInteractiveRecentResult
search : buildInteractiveCitation
search : buildGeneratedAnswer
recommendation : missing SSR support
case-assist : missing SSR support
insight : missing SSR support
commerce : missing SSR support

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

Successfully merging this pull request may close these issues.

1 participant