Skip to content
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

Glitch free reactive stores #1882

Merged
merged 2 commits into from
Dec 21, 2018
Merged

Glitch free reactive stores #1882

merged 2 commits into from
Dec 21, 2018

Conversation

Rich-Harris
Copy link
Member

Started replying to @marvinhagemeister on sveltejs/rfcs#5 but got side-tracked by trying to see if we could make our reactive stores glitch-free (i.e. if a derived store depends on a and b, and b is also a derived store that depends on a, don't recalculate when a changes until b is also up-to-date).

I think this works? But would appreciate a sanity check if anyone is familiar with this problem space...

Explanation: each store's subscribe method now takes an optional second invalidate method:

store.subscribe(
  value => console.log(`the value is`, value),
  () => console.log(`here comes a new value... I wonder what it will be?`)
);

When a store's value changes, it should call all the invalidate functions and then call the main callback. Most of the time the second argument is unnecessary, but the derive function uses it to update an internal number that keeps track of which inputs are invalid.

@Rich-Harris Rich-Harris merged commit b40c153 into master Dec 21, 2018
@Rich-Harris Rich-Harris deleted the glitch-free branch December 21, 2018 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant