-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Call onMount when connected & clean up when disconnected for custom element #4522
Call onMount when connected & clean up when disconnected for custom element #4522
Conversation
* 'master' of https://github.com/sveltejs/svelte: (137 commits) -> v3.19.2 fix lazy code breaks in build fit bitmask overflow initial dirty value in 'if' blocks (sveltejs#4507) add dev runtime warning for unknown slot names (sveltejs#4501) fix render fallback slot content due to whitespace (sveltejs#4500) docs: describe falsy and nullish attribute behavior (sveltejs#4498) in spread, distinguish never-updating and always-updating props (sveltejs#4487) chore: more specific typings, and add README note about Yarn (sveltejs#4483) update changelog check for unknown props even if component doesn't have writable props (sveltejs#4454) Bump codecov from 3.5.0 to 3.6.5 (sveltejs#4433) fix bitmask overflow for slot (sveltejs#4485) mark module variables as mutated or reassigned (sveltejs#4469) docs: referenced_from_script var value (sveltejs#4486) docs: clarify default prop behaviour (sveltejs#4460) site: turn fancybutton into custombutton (sveltejs#4476) update changelog exclude global variables from $capture_state (sveltejs#4475) -> v3.19.1 don't treat $$-names as stores during invalidation (sveltejs#4453) ...
Thank you. What do you think about the discussion in that issue though — about the fact that we actually don't want |
@Rich-Harris splendid! Updated the PR so that |
@tanhauhau is this being merged? Looks like it's waiting on you, AND that you've apporved. I'm confused. 😅 |
* upstream/master: (190 commits) invalidate $$props and $$restProps only when there are changes (sveltejs#5123) site: use https in link in blog (sveltejs#5148) Simplify each block bindings example (sveltejs#5094) fix $$props reactive for slots (sveltejs#5125) site: add FAQ entry for how to document a svelte component (sveltejs#5131) site: remove an obsolete TODO in blog post (sveltejs#5135) Increase timeout for unit build Increase timeout for unit tests -> v3.24.0 spread condition for input element (sveltejs#5004) update changelog fix(5018): compare wholeText instead of data (sveltejs#5028) html anchor in head (sveltejs#5071) error on expression scope store (sveltejs#5079) update changelog preprocess self-closing script and style tags (sveltejs#5082) update changelog fix: Parameters with default values are optional (sveltejs#5083) make builds time out after a reasonable period (sveltejs#5100) site: fix blog typo (sveltejs#5090) ...
* 'master' of github.com:hontas/svelte:
* upstream/master: add updating guard to binding callback (sveltejs#5126) Bump lodash from 4.17.15 to 4.17.19 (sveltejs#5152) Bump lodash from 4.17.15 to 4.17.19 in /site (sveltejs#5155) Fixes sveltejs#5153 (sveltejs#5154)
@hontas I've been working on another solution to #2227 (see #5139) and comparing the impact on the various hooks as well as any top-level code. If the props are initialised before |
* upstream/master: (140 commits) support $$props and $$restProps for custom elements (sveltejs#5608) Bump eslint-config to 5.5.0 (sveltejs#5599) update changelog add Node and Element as known globals (sveltejs#5601) docs: fix a11y warning in media elements example (sveltejs#5606) Curly braces linting fixes (sveltejs#5585) refactor sourcemap and preprocessor tests (sveltejs#5583) -> v3.29.4 fix code generation error with nullish coalescing operator and logical operators (sveltejs#5564) -> v3.29.3 -> v3.29.2 -> v3.29.1 docs: fix a11y warning in media elements tutorial (sveltejs#5523) update changelog get context at start of {#if} update block instead of at the end (sveltejs#5531) update changelog only allow passing functions to lifecycle functions (sveltejs#5529) update changelog fix compiler hanging on <slot slot="..."> (sveltejs#5536) Add svelte@next caveat to bug report template (sveltejs#5561) ...
Can I ask if this issu is still being worked on?
Still, I don't like the fact that I'm changing something in the svelte internals. Still, for some cases it is needed to call onDestroy to clean up after removing a custom element. |
Certainly @yannkost ;) It’s still waiting for feedback from the maintainers. |
Are the issues discussed above solved by #5139 ? We should close one of these PRs, and focus on getting the other merged. Perhaps somebody who is closer to the issue (somebody who knows about custom elements) could clarify? |
Don't worry - I'm not here asking for "when it'll be merged" :-) Just wanna make sure I understand what this PR does, as it pertains to web components, and the comments @Rich-Harris made. So, apparently, it's not unheard of for a web component to be disconnected, and then re-connected / re-attached to the dom. And so what this PR does is fire only the onUnMount handler the user can optionally return from onMount, when the web component is disconnected, but otherwise not destroy anything. And then if the wc were to be re-attached to the dom, then the user's onMount handler would fire de novo, and the whole process would repeat. Is that about right? |
@arackaf yes that’s the idea 💡 |
@antony - per our discussion, I've pulled down this PR, and tested it locally. It appears to be a solid fix for web component connected / disconnected lifecycles, as they pertain to Svelte. Before, onMount would be called the moment the web component was created, and the unmount callback was never, ever called. Now, with this PR, the onMount handler is called when the wc is connected to the dom, with the unMount handler being called when it's disconnected. Rinse, repeat. I doubt this fixes all outstanding wc issues, but it seems to fix a big one, and well. I'm confident enough to wager @Rich-Harris's life on it ;) @hontas - your branch appears to be far behind master (I of course merged prior to testing). Would you mind merging and pushing? Heads up - expect one conflict in a test file. |
* 'master' of https://github.com/sveltejs/svelte: (129 commits) -> v3.32.3 fix remove of lone :host selectors (sveltejs#5984) -> v3.32.2 update changelog fix extra invalidation with component prop binding to object property (sveltejs#5890) update css-tree@^1.1.2 (sveltejs#5958) fix :host and :global css scoping (sveltejs#5957) Tutorial : a better explanation of component events (sveltejs#4639) "What's new in Svelte" February newsletter (sveltejs#5925) Change color on 404 page (sveltejs#5932) -> v3.32.1 warn module variables are nonreactive and make them truly nonreactive (sveltejs#5847) update changelog fix: "foreign" namespace elements should still allow binding 'this' (sveltejs#5942) inline `prop_values` in `init` helper (sveltejs#5909) update changelog don't create class update functions when dependencies aren't reactive (sveltejs#5926) fix extraneous store subscription in SSR (sveltejs#5929) make `SvelteComponentDev` typings more forgiving (sveltejs#5937) make animation/transition params optional (sveltejs#5936) ...
- Call onMount in connectedCallback for customElements - register onMount return values as on_disconnect-callbacks for customElements - run on_disconnect callbacks in disconnectedCallback
8fc8c81
to
9cc7dd2
Compare
@hontas I see you just updated your branch - are also you ok fixing the issue Rich mentioned? I'm happy to if this is a busy / bad time for you. |
@arackaf I'm on it! |
@hontas - you're amazing. I can't answer your question, but in the off chance there's no easy way to implement what @Rich-Harris suggested, would const isCustomElement = component instanceof SvelteElement; work? I think that would be a lot more performant that the original code. (untested of course - just an idea - apologies if there's something dumb wrong with that) |
- pass options.customElement down to mount_component - remove expensive isCustomElement check - only call add_render_callback if not customElement
@Rich-Harris updated PR with your suggestions 😃 please have a look if that's what you had in mind. I'm super excited! |
Do I understand this right when I say that the component will execute the callback fonction set up in onMount when disconnected (only if it's a custom élément)? (This is what I seem to understand in the code) |
@yannkost it’ll run the teardown function, which is returned from onMount, when the custom element disconnects. Likewise it’ll run the onMount function itself when the web component connects. |
@hontas thanks a ton for making this fix. Huge lifesaver. I'm not by any means asking you to fix this, but since you seem to have interest in using Svelte for web components, just an fyi that I recently found this issue: #5989 Seems like the fix would be incredibly similar to what you did here, or at the very least, something for you to be aware of, for your future use of Svelte :) Thanks again! |
Relates to #1152
Resolves #2227
Solves the issue with function returned from onMount not getting called for customElements.
Introduces three changes:
connectedCallback
onMount
ason_disconnect
and run them indisconnectedCallback
on_mount
after initial runBenefits:
onMount
happens (no moretick().then(/**/)
). Maps closer to mental model of a custom element.onDestroy
is still not called.Example: