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: don't depend on deriveds created inside the current reaction #15564

Merged
merged 7 commits into from
Mar 21, 2025

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Mar 20, 2025

Follow-up to #15553 which fixes this case.

Unfortunately it changes the behaviour of effects in a way that could be considered breaking — in a case like this, where a derived that's local to an effect depends on state outside the effect...

let count = $state(0);

$effect(() => {
  let double = $derived(count * 2)

  console.log('init ' + double);

  return function() {
    console.log('cleanup ' + double);
    console.log(this);
  };
});

...today the effect will re-run when count changes, but in this PR it won't. I'm not sure there's a way to have that behaviour and have effects not depend on deriveds that depend entirely on local state. I think this is the right trade-off, but I'd love to hear from other maintainers.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Mar 20, 2025

🦋 Changeset detected

Latest commit: f430de4

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

Playground

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

@svelte-docs-bot
Copy link

@Rich-Harris Rich-Harris changed the title Untrack own deriveds 2 fix: don't depend on deriveds created inside the current reaction Mar 20, 2025
@dummdidumm
Copy link
Member

If we don't want effects to rerun when state they create changes, then by extension we also don't want that for deriveds, regardless what sources they have. I also don't know in what situation you would create a derived inside an effect and want it to re-run when said derived changes.
Having said all that, these nuances make it increasingly hard to explain when effects rerun - we need to find a good mental model for that and put it down in the docs.

@trueadm
Copy link
Contributor

trueadm commented Mar 21, 2025

I think this is a sensible change, but it's also maybe a breaking change. I guess we can release it and address if that is the case.

@Rich-Harris Rich-Harris merged commit d2e7932 into main Mar 21, 2025
13 checks passed
@Rich-Harris Rich-Harris deleted the untrack-own-deriveds-2 branch March 21, 2025 13:31
@github-actions github-actions bot mentioned this pull request Mar 21, 2025
@maxnowack
Copy link

maxnowack commented Mar 24, 2025

Unfortunately this breaks the svelte integration of SignalDB 😕
See maxnowack/signaldb#1536

Some more information about the integration: https://signaldb.js.org/guides/svelte/

cc @ykrods

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.

4 participants