-
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
Signal.subtle.watched/unwatched is not sufficient, we need to be notified when a signal is used (even without a watcher) #227
Comments
I ran into this problem in my framework too. It seems this proposal allows an unavoidable edge case when integrating external sources where the replica might be stale and there's no reliable way to know. I've opened a broader proposal in #237. |
One tricky part here is if you want a notion of "is this signal in use?" that incorporates unwatched signals, whatever that notion is ends up sensitive to GC timing: a computed that previously read your signal can become garbage later, without ever having another lifecycle event happen to it. If you accept that condition, you can deal with it explicitly and get something resembling... well, it resembles what you asked for, though not necessarily what you want. What you'd do is:
Of course, it may be that what you really want is more like #237 (a way to make things recompute every time no matter what) rather than a way to be notified when all (watched and unwatched both) consumers of a node become garbage. |
@shaylew Thank you for your comment.
I don't see things like this. For me, GC should not be involved at all in the process of knowing whether a signal is in use. For me, a signal should be considered in use:
That's what we implemented in tansu: there is only one way to read the value of a store which is subscribing to it. As long as there is a subscription, the store is used. When the last subscriber unsubscribes, the store is unused.
What I really want is indeed similar to #237. |
Ah, I see how you can make things work with that sort of design. But one of the goals of signals is for APIs that are resting to be able to be used from code that doesn't know or care about signals. If you can call .get from anywhere, and consumers don't need to manually subscribe and unsubscribe, "is it still live" is the only real approximation you can get of whether it might be accessed. As long as it's possible to read a signal from a regular non-signal context, you're going to have readers that the framework didn't know about ahead of time. (And "use watchers to keep things live" basically corresponds to the other answer, which is "okay, I'll trade manual subscribe/unsubscribe for more precise tracking of what's 'active'".) |
For info, I have opened this PR in the polyfill as a potential solution for this issue. |
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 (inspired by svelte stores), as described further in this article we have the ability to know whether a signal is used or not, in order to update the signal value and add or remove event listeners.
For example, we can create the following signal that contains a string value from the local storage and a computed that parses the value as JSON:
Now if I call
parsedPreferences$()
at any time, I have the current value of the "preferences" local storage item parsed as a json object.However, with the current signals proposal, this is not possible to achieve without constantly listening to the "storage" event.
I know the
Signal.subtle.watched
andSignal.subtle.unwatched
callbacks, but they are not called when the value is read through a one-time access to the value of a dependent signal:With the above code, if I read
parsedPreferences$.get()
once (without having a watcher) and then the value of the "preferences" local storage item changes from another tab, and then I read again (still without having a watcher)parsedPreferences$.get()
, the value will not be up-to-date.I believe it is fundamental that the signals proposal allows such a use case.
The text was updated successfully, but these errors were encountered: