fix: avoid duplicate reaction when reconnecting a derived

When a derived is disconnected (its last reader is hidden), then its
dependency is changed and the reader is shown again, `reconnect`
unconditionally pushed the derived into the dependency's `reactions`
array. If it was already present, this left a duplicate entry that
`remove_reaction` only removes once, leaking the derived (and any
component it is owned by) after unmount.

Guard the push with a membership check so a derived is only ever
registered once per dependency.
pull/18783/head
Xia Chao 4 days ago
parent 5895c637b0
commit 87336a9dbb

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: avoid duplicate reaction when reconnecting a derived after its reader is hidden

@ -736,7 +736,10 @@ function reconnect(derived) {
if (derived.deps === null) return;
for (const dep of derived.deps) {
(dep.reactions ??= []).push(derived);
var reactions = (dep.reactions ??= []);
if (index_of.call(reactions, derived) === -1) {
reactions.push(derived);
}
if ((dep.f & DERIVED) !== 0 && (dep.f & CONNECTED) === 0) {
unfreeze_derived_effects(/** @type {Derived} */ (dep));

Loading…
Cancel
Save