-
-
Notifications
You must be signed in to change notification settings - Fork 4.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
Functions in reactive statements do not invalidate variables #7971
Comments
This is not about invalidation but execution order of the reactive statements. The invalidation happens but after the log statements. The problem is that the dependency chain is hidden. Hence the automatic topological ordering does not take the effects of the function into consideration. Because of this, the manual order of the reactive statements starts to matter. You can fix the chain by manually ordering the statements correctly: $: if(count > 3) {
change();
}
$: console.log("count:", count);
$: console.log("reset:", reset); |
Yes, this is the intended behavior, and has come up several times before. See my comment here. |
Thank you both for your answers! Yes, I know that the topological order could matter sometimes. It's not clear to me, why this doesn't happen... I would expect something like:
Step 4 and 5 are skipped but I would expect that just the intermediate states could be skipped ( |
It is pretty simple, you can look at the compiled code: $$self.$$.update = () => {
if ($$self.$$.dirty & /*count*/ 1) {
$: console.log("count:", count);
}
if ($$self.$$.dirty & /*reset*/ 4) {
$: console.log("reset:", reset);
}
if ($$self.$$.dirty & /*count*/ 1) {
$: if (count > 3) {
// doesn't invalidate 'count' and 'reset'
change();
}
}
}; All reactive statements are evaluated in sequence and at the end of the the update loop, the
|
Thanks a lot! Now is pretty clear to me, where is the error. I would add this explanation to the documentation, if possible. Again, thanks a lot for the help! 🙏 |
|
Describe the bug
If a function is called in a reactive statement
$:
and changes the value of other variables, these variables won't be invalidated and the reactivity chain won't be called for them.If the change is made in place (without calling other functions), it works fine.
Not sure why a function call makes that big difference, I wouldn't expect it 😕
Thanks in advance for the help 😄
Reproduction
The issue is reproduced in the following REPL:
https://svelte.dev/repl/8945e581e9ac4638ad9870eb59cb97c1?version=3.52.0
The component is a counter and adds one to the
count
variable every time the button is pressed.As soon as the limit (4) is reached, the count starts again from zero.
A boolean flag
reset
shows if the count has already reached its limit or not.If the update of the variables
count
andreset
is made in thechange()
functions, the reactive statements at lines 5 and 6 are not executed (the console doesn't showcount: 0
andreset: true
).If the change is made directly in the reactive statement without calling the
change()
function, all works fine and the console shows the changes.Another workaround is to call the
await tick()
before callingchange()
.Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: