-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Make sure useSelector.ts doesn’t create stale values #4168
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 904e6c4:
|
Could you add a test case that would cover this case? |
@Andarist done! I think the comments in the code should be self descriptive, but here is a short summary since it’s pretty obscure Svelte behavior: Using {#if $state.context.count >= 2}
<div data-testid="selectorOutput”>{$count}</div>
{/if} Instead I created another variable ( Without the fix in this PR, the value would still read |
You might be wondering why this fix is needed in the first place, since you nearly always use a Svelte store in the template with the The bug appeared in our case, because we created the store and put it in a svelte context: const count = useSelector(/* etc... */);
setContext(‘count’, count); And then in some child component, at a later time, we used that store: const count = getContext(‘count’); And this is where the value was wrong. |
I wonder, are you using |
Yes that’s exactly what I’m doing. |
The same bug exists in v4, but since I’m using v5 and this is going to be the next version anyway, I thought I might just fix it in v5 (especially since that is an edge case that probably not many people encounter). |
The function provided to svelte's
readable
is only invoked when the first subscriber subscribes to this store.But because stately’s
actor.subscribe
does not fire immediately after subscribing (it only fires when something changes in the state) the svelte store could have a stale value (with the initialprevSelected
) until the state in the machine changes and triggers the update.This PR fixes that, by making sure that the value is not stale when the first subscription happens.