-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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: avoid disconnecting deriveds that are still active #13292
Conversation
🦋 Changeset detectedLatest commit: f63e493 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
I also applied it to key block as it looks to work exactly like the if block logic does. |
Not sure I understand how pausing a branch affects the block's dependencies — isn't that a bug? |
Nope. It's by design. Let's take:
The If Effect starts by not having a dependency on C, only the Inner Effect does. When we switch A off, the If Effect then reads A and C, but its not store in its dependencies till the end, in the mean time it also destroys Inner Effect which is depending on A and C. Now given the fact that we haven't yet associated the If Effect with C, the removal of the Inner Effect will detach Derived C from State B. When we associate If Effect with C, it will be too late, we will already have removed it from B. I can see if there's an alternative solution for this. |
Okay, so I found an alternative angle – we avoid disconnecting derived signals that are in the the current |
Co-authored-by: Rich Harris <rich.harris@vercel.com>
Fixes #13284.
This was an interesting bug. This was historically dealt with by having multiple effects for this – we'd have one for the condition and one for the actual handling of the branching. That's because the dependencies for effects are only realised after we execute the inner function. Normally this is fine, but if you synchronously add/remove effects during this, it can cause the current effect to not correctly attach its current dependencies.
One way of tackling this is to instead wrap the condition in a derived, which are more lightweight than effects. I originally did that in this PR but the instead adjusted the logic for determining how we disconnect a derived reaction – and instead we avoid disconnection if the derived is in our active reaction's dependencies.