Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions packages/spf/src/core/signals/effect.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { Signal } from 'signal-polyfill';

// Computeds waiting to re-run after their dependencies changed.
const pending = new Set<Signal.Computed<void>>();
// 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<Signal.Computed<void>>();

const watcher = new Signal.subtle.Watcher(() => {
queueMicrotask(runPending);
});

function runPending() {
for (const c of watcher.getPending()) {
pending.add(c as Signal.Computed<void>);
}
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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This makes every flush scan every live effect, even when only one effect is pending. Keep registration order while iterating only the pending effects, for example by assigning each effect a registration index and sorting the pending list by that index before running it.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/spf/src/core/signals/effect.ts, line 17:

<comment>This makes every flush scan every live effect, even when only one effect is pending. Keep registration order while iterating only the pending effects, for example by assigning each effect a registration index and sorting the pending list by that index before running it.</comment>

<file context>
@@ -1,21 +1,22 @@
 
-  for (const c of pending) {
-    pending.delete(c);
+  for (const c of effects) {
+    if (!pending.has(c)) continue;
+
</file context>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Update ordering documentation with the scheduler

For future scheduler and reactor maintenance, this loop now derives registration order from the live effects set rather than from Watcher.getPending(), but internal/design/spf/actor-reactor-factories.md:418-431, internal/design/spf/decisions.md:33-35, and internal/design/spf/signals.md:453-465 still say this load-bearing guarantee depends on watcher insertion order, while internal/design/spf/text-track-architecture.md:473-484 still treats explicit ordering as an open option. Those explanations are now factually wrong about the mechanism this fix relies on, so update them as part of this scheduler change.

AGENTS.md reference: AGENTS.md:L16-L16

Useful? React with 👍 / 👎.

if (!pending.has(c)) continue;

c.get(); // re-run the effect body
revalidateSources(c);
}
Expand Down Expand Up @@ -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();
Expand Down
43 changes: 43 additions & 0 deletions packages/spf/src/core/signals/tests/effect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading