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: always reset the evaluation context upon entering an untracked block #512

Merged
merged 3 commits into from
Feb 28, 2024
Merged

Conversation

jviide
Copy link
Contributor

@jviide jviide commented Feb 28, 2024

This pull request fixes an issue with multiple levels of mixed effect and untracked calls:

import { signal, effect, untracked } from "@preact/signals-core";

const s = signal(0);

untracked(() => {
  effect(() => {
    untracked(() => {
      console.log(s.value); // <-- s.value gets tracked while inside untracked(...)
    });
  });
});
// 0 gets printed to the console

s.value = 1;
// 1 gets printed to the console

The expected behavior is that the effect should not run when s.value is set to 1, because s.value was only used inside untracked. Therefore the effect should not become dependent on s.value.

The root cause is that only the outermost level of nested untracked() calls sets the evaluation context to undefined (which disables tracking). The fix is to unconditionally set the evaluation context to undefined inside every untracked call. The untrackedDepth counter then becomes unused.

The first commit of this pull request adds a test for the issue. The second commit applies the fix and removes the untrackedDepth counter.

The last commit does some minor refactoring: it just moves all tests related to untracked into their own section in the test file and modifies the test names accordingly. The last commit can be dropped if it feels superfluous.

Copy link

changeset-bot bot commented Feb 28, 2024

⚠️ No Changeset found

Latest commit: e357b4d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

Copy link

netlify bot commented Feb 28, 2024

Deploy Preview for preact-signals-demo ready!

Name Link
🔨 Latest commit e357b4d
🔍 Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/65de9cbe94e54900084abb3c
😎 Deploy Preview https://deploy-preview-512--preact-signals-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@marvinhagemeister marvinhagemeister merged commit d7f2afa into preactjs:main Feb 28, 2024
6 checks passed
@jviide jviide deleted the patch-01 branch February 28, 2024 23:25
@github-actions github-actions bot mentioned this pull request Mar 19, 2024
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.

None yet

2 participants