Repository navigation
fix(spf): preserve effect order across composition teardown #65
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
base: mmcc/moq
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For future scheduler and reactor maintenance, this loop now derives registration order from the live AGENTS.md reference: AGENTS.md:L16-L16 Useful? React with 👍 / 👎. |
||
| 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(); | ||
|
|
||
There was a problem hiding this comment.
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