Skip to content

Commit 976fe1e

Browse files
ryansolidClaude via Cursorcursoragent
committed
test(signals): pin the lane pass of a born-held effect (#3802)
The broader condition also skips the run an optimistic lane queues for a born-held effect; that run applied an uncommitted value too. Also align the inline comment with effect.ts ('the commit replays the effect'). Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 56b2ffb commit 976fe1e

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
571571
!(el as any)._modified &&
572572
Object.is((el as any)._prevValue, value)
573573
) &&
574-
// A re-staging pass owes no run: the commit queues it (#3802).
574+
// A re-staging pass owes no run: the commit replays the effect (#3802).
575575
el._pendingValue === NOT_PENDING
576576
) {
577577
(el as any)._modified = !el._x?._error;

‎packages/signals/tests/held-effect-restage-run-3802.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { afterEach, expect, it, vi } from "vitest";
55
import {
66
action,
77
createMemo,
8+
createOptimistic,
89
createRenderEffect,
910
createRoot,
1011
createSignal,
@@ -64,3 +65,58 @@ it("a re-staging pass of a born-held effect does not run it before the commit",
6465
expect(error).not.toHaveBeenCalled();
6566
expect(runs).toEqual([[true, "Updated"]]);
6667
});
68+
69+
// The same effect re-derived by an optimistic write's lane rather than a
70+
// mainline write: the lane's run had nothing committed to apply either.
71+
it("a lane pass of a born-held effect does not run it with no committed value", async () => {
72+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
73+
const [value, setValue] = createSignal(false);
74+
const [mounted, setMounted] = createSignal(false);
75+
const [guess, setGuess] = createOptimistic(0);
76+
let resolveSave!: () => void;
77+
let resolveVote!: () => void;
78+
const runs: [boolean, number][] = [];
79+
80+
const save = action(function* () {
81+
setValue(true);
82+
yield new Promise<void>(r => (resolveSave = r));
83+
});
84+
const vote = action(function* () {
85+
setGuess(5);
86+
yield new Promise<void>(r => (resolveVote = r));
87+
});
88+
89+
createRoot(() => {
90+
const child = createMemo(() => {
91+
if (!mounted()) return;
92+
createMemo(() => value());
93+
createRenderEffect(
94+
() => ({ v: value(), g: guess() }),
95+
state => {
96+
runs.push([state.v, state.g]);
97+
}
98+
);
99+
});
100+
createRenderEffect(child, () => {});
101+
});
102+
flush();
103+
104+
const saving = save();
105+
await settle();
106+
setMounted(true);
107+
await settle();
108+
const voting = vote();
109+
await settle();
110+
111+
expect(error).not.toHaveBeenCalled();
112+
113+
resolveVote();
114+
await voting;
115+
await settle();
116+
resolveSave();
117+
await saving;
118+
await settle();
119+
120+
expect(error).not.toHaveBeenCalled();
121+
expect(runs.at(-1)).toEqual([true, 0]);
122+
});

0 commit comments

Comments
 (0)