-
-
Notifications
You must be signed in to change notification settings - Fork 107
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 stale computed values #300
Conversation
The previous commit's solution to the stale atom problem also solves the diamond problem without needing special handling of atom levels.
Is It possible to return this behaviour? I use it a lot on my CRDT stores. The store value before
Seems like it is a bug. The dummy subscriber for listener-less |
I didn't change the behavior in this PR since it would be a breaking change. It's just a suggestion for a possible future change that would be possible after this PR. We can ignore it for now and discuss it as a separate issue if this PR gets merged. |
In this case everything looks amazing. I will merge it to |
I merge to Can you find the reason? https://github.com/nanostores/nanostores/actions/runs/8958545657/job/24602887313 |
The new code in #380 needed to be updated to reflect the new lower number of items in listenerQueue per listener.
Fixed at #301 |
* Prevent stale computed values * Remove atom levels The previous commit's solution to the stale atom problem also solves the diamond problem without needing special handling of atom levels. * Optimize computed dependency check using epochs * Update size-limit --------- Co-authored-by: Andrey Sitnik <andrey@sitnik.ru>
Fixes #299
This fixes stale values in computed stores by checking if dependencies have changed any time
get()
is called. It does have a perf impact, though I added anepoch
optimization to help with that. And correctness should take precedence over performance (no other state library I'm aware of has this stale value problem). The benefits are:l
) for each atomlistenerQueue
is now O(n) instead of O(n^2)Another potential benefit -- this change means that when calling
get()
on an atom with 0 subscribers, we no longer have to add a dummy subscriber. I'm holding off on making that change, because it would be a breaking change -- some code could be relying on the side effect ofonMount
firing. But it could be considered for a future major version -- IMO it would be cleaner. Right now, if you callget()
on a computed store, it will never unsubscribe, and the value will be needlessly recomputed when the dependencies change, even if the only listener is the dummy listener.