Skip to content

Commit c6767e3

Browse files
committed
fix: address review findings
A tracked effect no longer re-runs for a dependency whose staged value nets to the committed value (`setS(1); setS(0)` on a committed 0), using the same `===` and `Object.is` netted test as the other staged-read paths. The post-run dependency walk is skipped when nothing is staged in the flush (`pendingNodes` is empty), which is the case for every run the commit already notified. The comment above the walk is cut to one line.
1 parent fb0841d commit c6767e3

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

‎.changeset/tracked-effect-same-pass-write.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
A `createTrackedEffect` whose first run reads a signal written earlier in the same effect phase (another effect's first run, a parent's `onSettled` writing a prop) now re-runs once the write commits, as `createEffect(compute, fn)` does (#4598). A tracked effect reads the committed value and links its dependencies in the effect phase, after the write had already walked the signal's subscribers, so the commit at the next seam notified nobody. After a tracked effect's run, a dependency staged this flush whose subscriber walk predates the run queues the effect again for the round that commits it. The pass's own write-then-read is still never observed and never re-runs (#3006), and `onSettled` stays one-shot.
5+
A `createTrackedEffect` whose first run reads a signal written earlier in the same effect phase (another effect's first run, a parent's `onSettled` writing a prop) now re-runs once the write commits, as `createEffect(compute, fn)` does (#4598). A tracked effect reads the committed value and links its dependencies in the effect phase, after the write had already walked the signal's subscribers, so the commit at the next seam notified nobody. After a tracked effect's run, a dependency staged this flush whose subscriber walk predates the run queues the effect again for the round that commits it; writes that net to the committed value stage no change and queue nothing. The pass's own write-then-read is still never observed and never re-runs (#3006), and `onSettled` stays one-shot.

‎packages/signals/src/core/effect.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
haltReactivity,
4242
haltReactivityAt,
4343
notifyEpoch,
44+
pendingNodes,
4445
resetUnhandledAsync,
4546
schedule,
4647
setTrackedQueueCallback,
@@ -308,16 +309,15 @@ export function trackedEffect(fn: () => void | (() => void), options?: NodeOptio
308309
} finally {
309310
if (__DEV__) setTrackedQueueCallback(false);
310311
}
311-
// A dependency an earlier callback staged this flush walked its subscribers
312-
// before this pass linked it (#4598): the pass read the committed value and
313-
// the seam's commit notifies nobody, so run again after it. Not the pass's
314-
// own write (walked after `epoch`, never observed: #3006), nor a held node.
312+
if (pendingNodes.length === 0) return;
313+
// A dep staged before this run linked it commits without a notify.
315314
for (let d = node._deps; d !== null; d = d._nextDep) {
316315
const dep = d._dep;
317316
if (
318317
dep._pendingValue !== NOT_PENDING &&
319318
dep._notifiedAt <= epoch &&
320-
!(dep._config & (CONFIG_HELD | CONFIG_OVERRIDE))
319+
!(dep._config & (CONFIG_HELD | CONFIG_OVERRIDE)) &&
320+
(dep._pendingValue !== dep._value || !Object.is(dep._pendingValue, dep._value))
321321
) {
322322
enqueueSub(node);
323323
schedule();

‎packages/signals/tests/tracked-effect-same-pass-write-4598.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,22 @@ it("re-runs for a write the creator's onSettled makes", async () => {
4848
await settle();
4949
expect(log).toEqual([0, 1]);
5050
});
51+
52+
it("does not re-run for writes that net to the committed value", async () => {
53+
const log: number[] = [];
54+
createRoot(() => {
55+
const [s, setS] = createSignal(0);
56+
createEffect(
57+
() => 1,
58+
() => {
59+
setS(1);
60+
setS(0);
61+
}
62+
);
63+
createTrackedEffect(() => {
64+
log.push(s());
65+
});
66+
});
67+
await settle();
68+
expect(log).toEqual([0]);
69+
});

0 commit comments

Comments
 (0)