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

Support auto-disposing nested effects #158

Merged
merged 2 commits into from
Sep 16, 2022
Merged

Conversation

jviide
Copy link
Contributor

@jviide jviide commented Sep 16, 2022

This pull request adds support for auto-disposing nested effects. With the following setup:

const a = signal("a");
const b = signal("x");
const e = effect(() => {
  console.log("outer effect", a.value);
  effect(() => {
    console.log("inner effect", a.value + b.value);
  });
});

// Console: outer effect a
// Console: inner effect ax

Now if we run the outer effect again the previous inner effect is auto-disposed:

a.value = "b";
// Console: outer effect b
// Console: inner effect bx

If we only update the b signal only the latest version of the inner effect runs:

b.value = "y"
// Console: inner effect by

When we dispose the outer effect then the inner effect is also disposed:

e();
a.value = "c";
// Nothing in the console

Note that only immediate containing effects are tracked: For example creating an effect inside an computed inside an effect doesn't dispose the innermost effect when the outermost effect is disposed.

@changeset-bot
Copy link

changeset-bot bot commented Sep 16, 2022

🦋 Changeset detected

Latest commit: a367a31

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

This PR includes changesets to release 1 package
Name Type
@preact/signals-core Minor

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
Member

@marvinhagemeister marvinhagemeister left a comment

Choose a reason for hiding this comment

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

This is great!!

@marvinhagemeister marvinhagemeister merged commit 378a9d4 into setless-core Sep 16, 2022
@marvinhagemeister marvinhagemeister deleted the nested-effects branch September 16, 2022 13:38
@github-actions github-actions bot mentioned this pull request Sep 16, 2022
@WebReflection
Copy link

creating an effect inside an computed inside an effect doesn't dispose the innermost effect when the outermost effect is disposed.

usignal used to have WeakRef to deal with computed but it recently got moved for various reasons ... this scenario though seems like OK as once the inner computed gets collected its inner most effect should get collected too.

it's an interesting case though, 'cause the inner effect if run when the outer runs we have a reference, but if the computed is accessed conditionally we might have issues, but as long as the effect internals don't leak on purpose outside the effect I believe everything should be covered 👍

@jviide jviide mentioned this pull request Sep 20, 2022
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.

3 participants