Skip to content

fix: ensure component root effect updates occur first #15439

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

Closed
wants to merge 2 commits into from

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Mar 4, 2025

A better fix than #15437. We should prioritize updates to component root effects before other root effect types – this ensures teardown happens before we process other root effects.

Fixes #15426.

Copy link

changeset-bot bot commented Mar 4, 2025

🦋 Changeset detected

Latest commit: 9008723

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

Copy link
Contributor

github-actions bot commented Mar 4, 2025

Playground

pnpm add https://pkg.pr.new/svelte@15439

@svelte-docs-bot
Copy link

@Rich-Harris
Copy link
Member

It's possible for a similar problem to occur without components being involved — the inner effect runs before the outer one:

let obj = $state({ count: 0 });
  
$effect.root(() => {
  let teardown;

  $effect.pre(() => {
    if (obj) {
      teardown ??= $effect.root(() => {
        $effect.pre(() => {
          console.log(obj.count);
        });
      });
    } else {
      teardown?.();
      teardown = null;
    }
  });
});

This is obviously rather contrived but it makes me wonder if dividing effect roots into component and non-component roots is the right approach, or if we should process roots-with-parents in tree order instead. Opened #15446 for comparison

@trueadm trueadm closed this Mar 5, 2025
@trueadm trueadm deleted the root-effects-scheduling branch March 5, 2025 09:19
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.

toStore runs outside component lifecycle
3 participants