-
Notifications
You must be signed in to change notification settings - Fork 59
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
How to allow set
in a Computed
to trigger a recomputation synchronously (until the result is stable)
#226
Comments
I believe that mutation during computation should be completely prohibited. What is in the computed should be a pure function. |
@szagi3891 Thank you for expressing your opinion. It is indeed a best practice to only have a pure function as the callback for computed. However, I am against completely prohibiting calling set in computed. The |
@divdavem have you tried using untrack within the computed? |
@NullVoxPopuli Thank you for your answer!
Do you mean around |
how would that be implemented algorithmically? |
@NullVoxPopuli I have opened proposal-signals/signal-polyfill#19 to show how this could be implemented. |
The current behaviour of only invalidating the computed makes sense to me, allowing set inside a computed seems fine, but I don't see benefit in automatically recomputing if a dep changes inside the computed. If you want some kind of mutation of deps to affect the result like your example you can use const c = new Signal.Computed(() => {
Signal.subtle.untrack(() => {
if (s.get() < 10) {
s.set(10);
}
});
return s.get();
}); |
Hello,
As a maintainer of the tansu signal library, I am trying (here) to re-implement it using signal-polyfill and I came across the following difference of behavior, that I would like to solve, avoiding any breaking change.
With
tansu
, callingset
on a signal in acomputed
that has a dependency on that signal re-triggers the computation of the computed until the value is stable (with a fixed maximum number of iterations). For example:(cf this test)
With
signal-polyfill
, there was apparently a different design decision:My question is simple: how may I reproduce the behavior of
tansu
when implementing it usingsignal-polyfill
?Do you think this signals proposal could change to adopt this different behavior?
Alternatively, we could probably implement this if we had some other primitives that are missing in the current specification (and I think those would be useful anyway):
This way, maybe we could have a loop in our
tansu
computed
which, after each computation, goes over all tracked reads called during the computed and checks if any of them is dirty at the end of computed. If it is the case, it means one of the (direct or transitive) dependencies changed during the call to computed and that we may need to recompute (in case those dependencies really changed).What do you think?
The text was updated successfully, but these errors were encountered: