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

fix(runtime): prevent watchers from prematurely firing in custom elements build #2971

Merged
merged 3 commits into from
Jul 21, 2021

Conversation

rwaskiewicz
Copy link
Member

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Tests (npm run test.karma.prod) was run locally and passed

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

When using the custom elements build, it's possible that @Watch annotated methods will fire before our components are fully instantiated. This is inconsistent with the lazy load build, as well as the Stencil documentation:

The Watch decorator does not fire when a component initially loads.

GitHub Issue Number: N/A

What is the new behavior?

  • Wait until a component is fully instantiated before a @Watch method can be fired
    • The ramification here is that watchers fired before fully instantiated will never run
    • With this PR, the behavior will match the behavior in the the lazy load builds today

Does this introduce a breaking change?

  • Yes
  • No

However, folks that are using the custom elements build may see watchers not firing as they expect. So we should either

  1. Make this very explicit in the release notes
  2. Push this to v3.0.0-dev instead

Testing

Programmatic tests have been pushed to a future story, as the work required to get that wired up is beyond the scope of this effort

Manual:

Replication

What's happening?

For the custom element build, runTest is being invoked when the component is ready according to the CustomElementRegistry, not according to Stencil. Stencil hasn't finished building the component on its side, but the @Watch is fired.

Testing the Fix

  • Pull down this branch, run npm ci && npm run build && npm pack
  • In the directory housing your clone of https://github.com/rwaskiewicz/demo-stencil-watch, npm i <PATH_TO_STENCIL>/stencil-core-2.7.0-0.tgz
  • Serve the lazy loaded bundle (npm start)
  • Serve the custom elements build (npm serve)
  • Note the behavior is the same:
    Screen Shot 2021-07-20 at 9 49 43 AM

Other information

  • Internal ticket: STENCIL-13

…ent build

- wait for a component to be defined in the CustomElementsRegistry
  before setting the isWatchReady flag
Copy link
Contributor

@splitinfinities splitinfinities left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@rwaskiewicz rwaskiewicz merged commit 8c375bd into master Jul 21, 2021
@rwaskiewicz rwaskiewicz deleted the stencil13/watchers-firing-prematurely branch July 21, 2021 13:31
rwaskiewicz added a commit that referenced this pull request Aug 17, 2021
…ents build (#2971)

- wait for a component to be defined in the CustomElementsRegistry
  before setting the isWatchReady flag
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.

None yet

3 participants