preactjs / signals

Manage state with style in every framework
https://preactjs.com/blog/introducing-signals/
MIT License
3.71k stars 91 forks source link

Added `untracked` function #380

Closed XantreDev closed 1 year ago

XantreDev commented 1 year ago

Resolves https://github.com/preactjs/signals/issues/378

As mentioned in the proposal this allows us to write effects that essentially aren't re-executed by the signals that get accessed in the function body of untracked.

Allowing functionality like

function reaction(sender, listener) {
  effect(() => {
    const value = sender();
    untracked(() => {
      listener(value);
    });
  }
}
changeset-bot[bot] commented 1 year ago

🦋 Changeset detected

Latest commit: 5c72e9ae2e32c87e87adda2a24c1a4520089c67d

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

netlify[bot] commented 1 year ago

Deploy Preview for preact-signals-demo ready!

Name Link
Latest commit 5c72e9ae2e32c87e87adda2a24c1a4520089c67d
Latest deploy log https://app.netlify.com/sites/preact-signals-demo/deploys/64c024072037800008e67821
Deploy Preview https://deploy-preview-380--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.

XantreDev commented 1 year ago

@andrewiggins I will be appreciate if you give feedback)

andrewiggins commented 1 year ago

Hey! One question that comes to mind for me is what use case does this help that use const value = signal.peek() doesn't work for?

XantreDev commented 1 year ago

Some reasons is specified in proposal linked to the PR. But shortly: its useful for creating utils that works with callbacks. Most of reactive runtime provides untracked, so it's common pattern, that allows to manipulate reactivity not on sender side, but in consumer side. Yes, I can emulate untracked with computed, but it has too much overhead IMHO.

const untracked = (callback) => computed(callback).peek()

And I am not sure that I can modify values in computed, it feels weird

andrewiggins commented 1 year ago

Oh whoops! I missed the linked proposal. Lemme take a look and mull it over with the team

XantreDev commented 1 year ago

I think this thing are essential for writing Store-like primitives and dealing with it without tricks

XantreDev commented 1 year ago

@andrewiggins when you will return with feedback?

andrewiggins commented 1 year ago

I have some vacation coming up so will be away for a week or two. Though if other core team members have thoughts, no need to block on me

XantreDev commented 1 year ago

@JoviDeCroock what do you think about untracked?

JoviDeCroock commented 1 year ago

This looks good to me, tests seem to be failing and needs a changeset

XantreDev commented 1 year ago

This looks good to me, tests seem to be failing and needs a changeset

Fixed

betula commented 1 year ago

Thanks a lot👍