Skip to content

fix: ensure custom elements do not sync flush on mount #12787

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

Merged
merged 7 commits into from
Aug 10, 2024

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Aug 10, 2024

Fixes #12772.

We currently have an inconsistency around using $effect together with custom elements. If you put in an $effect inside a custom element, then we flush the effects sync before moving on the handling the next custom element. This is problematic when you have things like event handlers or other sequence/propagation sensitive things around as normally effects happen after we write we've handled the DOM, not incrementally as we're handling the DOM.

This is compounded if someone were to have $effect.pre in one custom element that is a parent of a child that has an $effect, the child $effect would run before the parent $effect.pre which makes for glitchy behaviour when creating interactions that depend on the correct ordering.

To fix this, we remove the problematic flush_sync from Svelte4Component. If people want to force their custom elements to work sync, they can always manually flush_sync themselves, however that's rarely needed.

Copy link

changeset-bot bot commented Aug 10, 2024

🦋 Changeset detected

Latest commit: bfa02a3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trueadm trueadm changed the title fix: ensure Svelte4Components do not sync flush fix: ensure custom-elements do not sync flush on mount Aug 10, 2024
@trueadm trueadm changed the title fix: ensure custom-elements do not sync flush on mount fix: ensure custom elements do not sync flush on mount Aug 10, 2024
@dummdidumm
Copy link
Member

This would be a big breaking change for everyone else, I don't think we can do that. If we determine this needs to be fixed for custom elements somehow, then the flush_sync should only be avoided for the custom element wrappers

@trueadm
Copy link
Contributor Author

trueadm commented Aug 10, 2024

This would be a big breaking change for everyone else, I don't think we can do that. If we determine this needs to be fixed for custom elements somehow, then the flush_sync should only be avoided for the custom element wrappers

Updated it so it only happens for non custom-element wrappers.

@trueadm
Copy link
Contributor Author

trueadm commented Aug 10, 2024

I tweaked the test so it fails on main. The event handler needs to be added on after the first tick to emulate another custom element adding the event handler.

@Rich-Harris
Copy link
Member

ah whoops, gotcha — thought it was just about delaying the check until both the element upgrade and the effect

@Rich-Harris Rich-Harris merged commit d8954d7 into main Aug 10, 2024
9 checks passed
@Rich-Harris Rich-Harris deleted the flush-sync-legacty branch August 10, 2024 17:15
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.

CustomElement $effect caused due to props read
3 participants