From e4f07a95b7f365cb02110cb970b77b906e132c4a Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 16 Oct 2026 09:28:35 -0700 Subject: [PATCH] fix(spf): preserve effect order across composition teardown --- packages/spf/src/core/signals/effect.ts | 18 +++++--- .../spf/src/core/signals/tests/effect.test.ts | 43 +++++++++++++++++++ 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/packages/spf/src/core/signals/effect.ts b/packages/spf/src/core/signals/effect.ts index 4cf7c21086..85680f81a0 100644 --- a/packages/spf/src/core/signals/effect.ts +++ b/packages/spf/src/core/signals/effect.ts @@ -1,21 +1,22 @@ import { Signal } from 'signal-polyfill'; -// Computeds waiting to re-run after their dependencies changed. -const pending = new Set>(); +// Keep registration order independently of Watcher: unwatch() swaps the last +// watched computed into the removed slot, which can move a reactor's binding +// effect ahead of its entry effect when another composition is destroyed. +const effects = new Set>(); const watcher = new Signal.subtle.Watcher(() => { queueMicrotask(runPending); }); function runPending() { - for (const c of watcher.getPending()) { - pending.add(c as Signal.Computed); - } + const pending = new Set(watcher.getPending()); watcher.watch(); // re-arm before running effects, in case they write signals - for (const c of pending) { - pending.delete(c); + for (const c of effects) { + if (!pending.has(c)) continue; + c.get(); // re-run the effect body revalidateSources(c); } @@ -60,10 +61,13 @@ export function effect(fn: () => (() => void) | void): () => void { cleanup = fn(); }); + effects.add(c); watcher.watch(c); c.get(); // initial run revalidateSources(c); // an initial run may self-write too — see the note return () => { + if (!effects.delete(c)) return; + watcher.unwatch(c); if (typeof cleanup === 'function') cleanup(); diff --git a/packages/spf/src/core/signals/tests/effect.test.ts b/packages/spf/src/core/signals/tests/effect.test.ts index c1821a5ba9..0db16c5ba1 100644 --- a/packages/spf/src/core/signals/tests/effect.test.ts +++ b/packages/spf/src/core/signals/tests/effect.test.ts @@ -7,6 +7,49 @@ import { computed, signal } from '../primitives'; const flush = () => Promise.resolve().then(() => Promise.resolve()); describe('effect', () => { + it('preserves registration order after an older effect is disposed', async () => { + const trigger = signal(false); + const stopOld = effect(() => {}); + const runs: string[] = []; + const stopEntry = effect(() => { + if (trigger.get()) runs.push('entry'); + }); + const stopBinding = effect(() => { + if (trigger.get()) runs.push('binding'); + }); + + stopOld(); + trigger.set(true); + await flush(); + expect(runs).toEqual(['entry', 'binding']); + stopEntry(); + stopBinding(); + }); + + it('does not run an effect disposed by an earlier effect in the same flush', async () => { + const trigger = signal(false); + let stopLater = () => {}; + const stopFirst = effect(() => { + if (trigger.get()) stopLater(); + }); + let runs = 0; + let cleanups = 0; + + stopLater = effect(() => { + trigger.get(); + runs++; + return () => { + cleanups++; + }; + }); + + trigger.set(true); + await flush(); + expect(runs).toBe(1); + expect(cleanups).toBe(1); + stopFirst(); + }); + describe('self-write through an intermediate computed (lost-wakeup regression)', () => { // An effect body may write a signal that an intermediate computed in its // own dependency graph reads (the concrete SPF shape: a track-selection