Skip to content

Commit 7acc039

Browse files
brenelzryansolid
andauthored
fix(signals): skip the run for a re-staging effect pass (#3814)
An effect that still carries a staged value isn't queued by recompute; the commit replays it. Fixes #3802. Co-authored-by: Brenley Dueck <[email protected]> Co-authored-by: Ryan Carniato <[email protected]>
1 parent c12731a commit 7acc039

4 files changed

Lines changed: 198 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
A render effect created while an action holds a value it reads no longer runs its callback when a later pass re-stages it before the action lands (#3802). That run passed `undefined` as the value, because the effect had nothing committed yet, so compiled JSX bindings threw and halted reactivity. The effect now runs once, at the commit that lands the action, with the latest staged value.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,9 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
570570
el._flags & REACTIVE_SCREEN_READ &&
571571
!(el as any)._modified &&
572572
Object.is((el as any)._prevValue, value)
573-
)
573+
) &&
574+
// A re-staging pass owes no run: the commit replays the effect (#3802).
575+
el._pendingValue === NOT_PENDING
574576
) {
575577
(el as any)._modified = !el._x?._error;
576578
// Reuse one bound runner per effect — runEffect no-ops on a stale
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// #3802: a render effect born held under an action, re-run by a write to a
2+
// committed source, ran its callback with no committed value.
3+
4+
import { afterEach, expect, it, vi } from "vitest";
5+
import {
6+
action,
7+
createMemo,
8+
createOptimistic,
9+
createRenderEffect,
10+
createRoot,
11+
createSignal,
12+
flush
13+
} from "../src/index.js";
14+
15+
afterEach(() => vi.restoreAllMocks());
16+
17+
async function settle() {
18+
for (let i = 0; i < 8; i++) await Promise.resolve();
19+
flush();
20+
}
21+
22+
it("a re-staging pass of a born-held effect does not run it before the commit", async () => {
23+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
24+
const [value, setValue] = createSignal(false);
25+
const [mounted, setMounted] = createSignal(false);
26+
const [title, setTitle] = createSignal("");
27+
let resolve!: () => void;
28+
const gate = new Promise<void>(r => (resolve = r));
29+
const runs: [boolean, string][] = [];
30+
31+
const save = action(function* () {
32+
setValue(true);
33+
yield gate;
34+
});
35+
36+
createRoot(() => {
37+
const child = createMemo(() => {
38+
if (!mounted()) return;
39+
createMemo(() => value());
40+
createRenderEffect(
41+
() => ({ v: value(), t: title() }),
42+
state => {
43+
runs.push([state.v, state.t]);
44+
}
45+
);
46+
});
47+
createRenderEffect(child, () => {});
48+
});
49+
flush();
50+
51+
const pending = save();
52+
await settle();
53+
setMounted(true);
54+
await settle();
55+
setTitle("Updated");
56+
await settle();
57+
58+
expect(error).not.toHaveBeenCalled();
59+
expect(runs).toEqual([]);
60+
61+
resolve();
62+
await pending;
63+
await settle();
64+
65+
expect(error).not.toHaveBeenCalled();
66+
expect(runs).toEqual([[true, "Updated"]]);
67+
});
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+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
import { afterEach, expect, test, vi } from "vitest";
6+
import { Show, action, createMemo, createSignal, flush } from "solid-js";
7+
import { render } from "../src/index.js";
8+
9+
function deferred() {
10+
let resolve!: () => void;
11+
const promise = new Promise<void>(r => (resolve = r));
12+
return { promise, resolve };
13+
}
14+
15+
async function settle() {
16+
for (let i = 0; i < 8; i++) await Promise.resolve();
17+
flush();
18+
}
19+
20+
afterEach(() => vi.restoreAllMocks());
21+
22+
test("updating an attribute of a subtree mounted under a held action does not crash (#3802)", async () => {
23+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
24+
const div = document.createElement("div");
25+
const [value, setValue] = createSignal(false);
26+
const [mounted, setMounted] = createSignal(false);
27+
const [title, setTitle] = createSignal("");
28+
const gate = deferred();
29+
30+
const save = action(function* () {
31+
setValue(true);
32+
yield gate.promise;
33+
});
34+
35+
const dispose = render(
36+
() => (
37+
<main>
38+
<Show when={mounted()}>
39+
{_ => {
40+
createMemo(() => value());
41+
return <div class={String(value())} title={title()} />;
42+
}}
43+
</Show>
44+
</main>
45+
),
46+
div
47+
);
48+
flush();
49+
50+
const pending = save();
51+
await settle();
52+
setMounted(true);
53+
await settle();
54+
setTitle("Updated");
55+
await settle();
56+
57+
expect(error).not.toHaveBeenCalled();
58+
59+
gate.resolve();
60+
await pending;
61+
await settle();
62+
63+
expect(error).not.toHaveBeenCalled();
64+
const el = div.querySelector("div")!;
65+
expect(el.className).toBe("true");
66+
expect(el.title).toBe("Updated");
67+
dispose();
68+
});

0 commit comments

Comments
 (0)