-
Notifications
You must be signed in to change notification settings - Fork 63
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
Signal class lacks implementation of setting new value using previous value #92
Comments
I think, explained in another Issue (I forget where), but you can achieve a modified value via Example: const doubled = Signal.Computed(() => signal.get() * 2); and for the event / user interaction use case, you're not in a tracking frame, so there is no risk of infinite render anywhere: const userClicked = () => {
signal.set(signal.get() + 1)
}
<button onClick={userClicked}>Increment</button> Additionally, I'm testing a thin wrapper around the polyfill here: https://github.com/NullVoxPopuli/signal-utils/pull/1/files#diff-a97765e1344fe9e023876e2b4c82e072ef8899c89d0f79038876a8809cbb7374R14 which is using native JS's theSignal++ (which is, in spirit, the same as what you describe (set + get)). it's still very WIP -- I hope to have something usable for folks in a couple days, but in my tests I need to have some effect implementation to pull on the reactive layer for testing / protecting for/against accidental infinite loops. |
Thanks Milo, So in the case presented, it won't loop on declaration(lazy evaluation), nor even when the signal updates(lazy update), only when it's called to retrieve the value. In a possible effect implementation, which is most likely the primary use case for a primitive like this, will loop. const c = computed(()=> signal.write(signal.read())
effect(()=>{ c() }) It's worrying to see reads and writes on the same sentence. From an educational point of view, read and writes shouldn't happen that way, and an api to do read+write at the same time without causing tracking could help people understand reactivity better, as they will ask/think why one has to do |
Say you have the following:
This is reading and writing to the signal at the same time Very problematic in tracked scopes as it will loop forever.
This is solved in Solid via the following form, which uses the previous value of the signal, giving it to the callback without causing tracking on the current scope.
While this form works well, it has three problems:
setter
of the signal has to do atypeof
to see if something is aFunction
to call it with the previous value.It could get even more ugly if you are creating the body of the function to save at the same time, it will look like
So I propose a third
method
, to support this functionality, to avoid the two/three problems mentioned above.The text was updated successfully, but these errors were encountered: