Skip to content

Commit 0761467

Browse files
committed
fix(signals): a deferred born-held effect drops its staging instead of stubbing
Rework for size and creation cost: the one-shot callback stub cost +53 B minified on every core row and touched effect creation. A deferred effect now drops the staging of a first pass born held, so the commit has nothing to replay; the plain path keeps its shape (one ternary instead of the leading !defer). A source change during the hold still runs the effect once, at the commit; the spec covers it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 857c3fd commit 0761467

2 files changed

Lines changed: 63 additions & 9 deletions

File tree

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,12 @@ export function effect<T>(
7979
// staged); the flush's commit replays this effect. Its first run is not this
8080
// creation's (A29). A lane's node likewise: its run is the lane's, queued
8181
// by the pass and released at the reveal (lanes.ts).
82-
if (node._pendingValue !== NOT_PENDING || node._x?._transaction?._lane) {
83-
// `defer` skips the first run (#4543): here that is the replay, so the
84-
// replay applies the value without calling back.
85-
if (options?.defer) {
86-
const fn = node._effectFn;
87-
node._effectFn = () => void (node._effectFn = fn);
88-
}
89-
} else
90-
!options?.defer &&
82+
// `defer` skips the first run, the commit's replay of a node born staged
83+
// included (#4543): it drops the staging, so the commit replays nothing.
84+
options?.defer
85+
? (node._pendingValue = NOT_PENDING)
86+
: node._pendingValue === NOT_PENDING &&
87+
!node._x?._transaction?._lane &&
9188
(node._type === EFFECT_USER || options?.schedule
9289
? globalQueue.enqueue(node._type, runEffect.bind(null, node))
9390
: // Type 0: the synchronous first render, not a queued run (D-19).

‎packages/web/test/defer-effect-held-mount-4543.spec.tsx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,60 @@ it("control: defer skips the first run when the mount is not held", async () =>
7474
it("defer skips the first run when the mount is held by an async memo (#4543)", async () => {
7575
expect(await mountChild(true)).toEqual({ text: "B", runs: [], after: [[2], [2]] });
7676
});
77+
78+
it("a change during the hold runs a deferred effect once, at the commit (#4543)", async () => {
79+
const runs: unknown[] = [];
80+
const renders: unknown[] = [];
81+
let answerPath!: () => void;
82+
let answerData!: () => void;
83+
const [path, setPath] = createSignal("a");
84+
const matched = createMemo(() => {
85+
const p = path();
86+
return p === "a" ? p : new Promise<string>(r => (answerPath = () => r(p)));
87+
});
88+
let bump: ((v: number) => void) | undefined;
89+
function Child() {
90+
const [local, setLocal] = createSignal(1);
91+
const keys = createMemo(() => [local()]);
92+
createEffect(
93+
() => keys(),
94+
k => void runs.push(k),
95+
{ defer: true }
96+
);
97+
createRenderEffect(
98+
() => keys(),
99+
k => void renders.push(k),
100+
{ defer: true }
101+
);
102+
bump = setLocal;
103+
// A second async read keeps the mount held after `matched` answers.
104+
const data = createMemo(() => new Promise<string>(r => (answerData = () => r("d"))));
105+
return <p>B{data()}</p>;
106+
}
107+
const el = document.createElement("div");
108+
const dispose = render(
109+
() => (
110+
<Loading fallback="root">
111+
<Show when={matched() === "b"} fallback={<p>A</p>}>
112+
<Child />
113+
</Show>
114+
</Loading>
115+
),
116+
el
117+
);
118+
setPath("b");
119+
flush();
120+
answerPath();
121+
await wait(0);
122+
flush();
123+
expect([el.textContent, !!bump]).toEqual(["A", true]);
124+
bump!(3);
125+
flush();
126+
expect([runs, renders]).toEqual([[], []]);
127+
answerData();
128+
await wait(0);
129+
flush();
130+
expect(el.textContent).toBe("Bd");
131+
expect([runs, renders]).toEqual([[[3]], [[3]]]);
132+
dispose();
133+
});

0 commit comments

Comments
 (0)