Skip to content

Commit 2982c08

Browse files
committed
fix(solid): Show's narrowed accessor reads the screen during a closing hold (#4354)
Non-keyed Show judged staleness from a `shown` flag the selection memo set on every run. When a hold kept the branch on screen, the selection had already re-run in the held world and set the flag false, so an unrelated write that re-ran an on-screen binding threw the stale-value error and halted. The guard now reads `condition()` tracked, as Match's guard does: the reading binding is a frame reader and sees the screen's truthiness, while a read after unmount still sees the committed falsy and throws. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 38e0c49 commit 2982c08

4 files changed

Lines changed: 313 additions & 10 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Non-keyed Show's narrowed accessor no longer throws a stale-value error when an on-screen branch re-runs during a hold that is closing it: the guard reads the condition as the reading binding sees it (the screen), as Match already does

‎packages/solid/src/client/flow.ts‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,9 @@ export function Show<T>(props: {
217217
}
218218
: { equals: (a, b) => !a === !b, sync: true }
219219
);
220-
// Truthiness that selected the child. The accessor runs from a binding:
221-
// `untrack(condition)` there serves the committed frame (undefined on the
222-
// first mount of `when={latest(x)}`, #3948), and an ownerless read serves
223-
// that same frame for a value staged this flush, so a live branch looks
224-
// stale and halts. The flag is the selecting read, updated when selection
225-
// re-runs — not a second read, and not a `latest()` import (that pulls the
226-
// lane engine into every Show bundle).
227-
let shown: T | undefined | null | boolean = false;
228220
return createMemo(
229221
() => {
230222
const c = condition();
231-
shown = c;
232223
if (c) {
233224
const child = props.children;
234225
const fn = typeof child === "function" && child.length > 0;
@@ -238,7 +229,13 @@ export function Show<T>(props: {
238229
: untrack(
239230
() =>
240231
(child as any)(() => {
241-
if (!shown) throw narrowedError("Show");
232+
// A tracked read, as Match's guard: the binding that reads
233+
// is a frame reader, so it sees the screen's truthiness —
234+
// held-closing branch still on screen (#4354), a
235+
// `latest()`-selected first mount (#3948) — while a read
236+
// after unmount sees the committed falsy and throws.
237+
// `untrack(condition)` serves neither.
238+
if (!condition()) throw narrowedError("Show");
242239
return conditionValue();
243240
}),
244241
IS_DEV && "<Show>"
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/**
2+
* #4354: `<Show when={open()}>{v => …}</Show>` beside a `<Loading>` over an
3+
* async memo that re-asks on `open`. A write to `open` is held by that memo's
4+
* flight, so the screen keeps the old branch while Show's selection has
5+
* already re-run in the held world. An unrelated write (`setFmt`) then re-runs
6+
* an on-screen binding. A binding is a frame reader: it reads what the screen
7+
* shows, so the narrowed accessor must not judge the branch stale from the
8+
* held selection — <Switch>/<Match> already behaves this way. A read after the
9+
* branch really unmounts still throws (#3948's guard).
10+
*/
11+
import { afterEach, expect, test, vi } from "vitest";
12+
import {
13+
createLoadingBoundary,
14+
createMemo,
15+
createRenderEffect,
16+
createRoot,
17+
createSignal,
18+
flush,
19+
Match,
20+
resetErrorHalt,
21+
Show,
22+
Switch
23+
} from "../src/index.js";
24+
25+
afterEach(() => {
26+
resetErrorHalt();
27+
vi.restoreAllMocks();
28+
});
29+
30+
const settle = async () => {
31+
for (let i = 0; i < 3; i++) {
32+
await Promise.resolve();
33+
flush();
34+
}
35+
};
36+
37+
type Kind = "Show" | "Switch";
38+
39+
function setup(kind: Kind, keyed: boolean, initial: boolean) {
40+
const [open, setOpen] = createSignal(initial);
41+
const [fmt, setFmt] = createSignal("ru");
42+
const resolvers: (() => void)[] = [];
43+
const screen: Record<string, unknown> = {};
44+
let accessor: (() => string) | undefined;
45+
createRoot(() => {
46+
const data = createMemo(() => {
47+
const o = open();
48+
return new Promise<string>(r => resolvers.push(() => r(`data-${o}`)));
49+
});
50+
const when = () => open() && "sel";
51+
const child = (v: string | (() => string)) => {
52+
const read = typeof v === "function" ? ((accessor = v), v) : () => v;
53+
createRenderEffect(
54+
() => `${fmt()}:${read()}`,
55+
t => void (screen.p = t)
56+
);
57+
return "branch";
58+
};
59+
const fallback = () => {
60+
createRenderEffect(
61+
() => `${fmt()}:closed`,
62+
t => void (screen.f = t)
63+
);
64+
return "closed";
65+
};
66+
const view =
67+
kind === "Show"
68+
? Show({
69+
get when() {
70+
return when();
71+
},
72+
keyed,
73+
get fallback() {
74+
return fallback();
75+
},
76+
children: child
77+
} as any)
78+
: Switch({
79+
get fallback() {
80+
return fallback();
81+
},
82+
get children() {
83+
return Match({
84+
get when() {
85+
return when();
86+
},
87+
keyed,
88+
children: child
89+
} as any) as any;
90+
}
91+
});
92+
createRenderEffect(
93+
() => (view as any)(),
94+
v => void (screen.view = v)
95+
);
96+
createRenderEffect(
97+
createLoadingBoundary(
98+
() => data(),
99+
() => "wait"
100+
),
101+
v => void (screen.data = v)
102+
);
103+
});
104+
return { screen, resolvers, setOpen, setFmt, accessor: () => accessor };
105+
}
106+
107+
for (const kind of ["Show", "Switch"] as const)
108+
for (const keyed of [false, true]) {
109+
const name = `${kind}${keyed ? " keyed" : ""}`;
110+
111+
test(`${name}: an on-screen branch re-run during the hold that closes it does not halt`, async () => {
112+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
113+
const { screen, resolvers, setOpen, setFmt, accessor } = setup(kind, keyed, true);
114+
flush();
115+
resolvers.shift()!();
116+
await settle();
117+
expect(screen).toEqual({ view: "branch", p: "ru:sel", data: "data-true" });
118+
119+
setOpen(false); // held by data's re-ask: the branch stays on screen
120+
flush();
121+
expect(screen).toMatchObject({ view: "branch", p: "ru:sel", data: "data-true" });
122+
123+
setFmt("en"); // unrelated write re-runs the on-screen binding mid-hold
124+
flush();
125+
expect(screen).toMatchObject({ view: "branch", p: "en:sel", data: "data-true" });
126+
127+
resolvers.shift()!();
128+
await settle();
129+
expect(screen).toEqual({ view: "closed", p: "en:sel", f: "en:closed", data: "data-false" });
130+
expect(error.mock.calls.map(c => String(c[0]).split("\n")[0])).toEqual([]);
131+
132+
// After the close lands, a read of the narrowed accessor is a real
133+
// stale read and still throws.
134+
if (!keyed) expect(() => accessor()!()).toThrow(kind === "Show" ? /<Show>/ : /<Match>/);
135+
});
136+
137+
test(`${name}: the fallback re-run during the hold that opens the branch does not halt`, async () => {
138+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
139+
const { screen, resolvers, setOpen, setFmt } = setup(kind, keyed, false);
140+
flush();
141+
resolvers.shift()!();
142+
await settle();
143+
expect(screen).toEqual({ view: "closed", f: "ru:closed", data: "data-false" });
144+
145+
setOpen(true); // held: the fallback stays on screen
146+
flush();
147+
expect(screen.view).toBe("closed");
148+
expect(screen.data).toBe("data-false");
149+
150+
setFmt("en");
151+
flush();
152+
expect(screen.view).toBe("closed");
153+
expect(screen.f).toBe("en:closed");
154+
155+
resolvers.shift()!();
156+
await settle();
157+
expect(screen.view).toBe("branch");
158+
expect(screen.p).toBe("en:sel");
159+
expect(screen.data).toBe("data-true");
160+
expect(error.mock.calls.map(c => String(c[0]).split("\n")[0])).toEqual([]);
161+
});
162+
}
163+
164+
test("Show: a branch closed and reopened inside one hold keeps reading the screen", async () => {
165+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
166+
const [open, setOpen] = createSignal<string | false>("a");
167+
const [fmt, setFmt] = createSignal("ru");
168+
const resolvers: (() => void)[] = [];
169+
const out: string[] = [];
170+
createRoot(() => {
171+
const data = createMemo(() => {
172+
const o = open();
173+
return new Promise<string>(r => resolvers.push(() => r(`data-${o}`)));
174+
});
175+
const view = Show({
176+
get when() {
177+
return open();
178+
},
179+
fallback: "closed",
180+
children: (v: () => string) => {
181+
createRenderEffect(
182+
() => `${fmt()}:${v()}`,
183+
t => void out.push(t)
184+
);
185+
return "branch";
186+
}
187+
});
188+
createRenderEffect(
189+
() => (view as any)(),
190+
() => {}
191+
);
192+
createRenderEffect(
193+
createLoadingBoundary(
194+
() => data(),
195+
() => "wait"
196+
),
197+
() => {}
198+
);
199+
});
200+
flush();
201+
resolvers.shift()!();
202+
await settle();
203+
setOpen(false);
204+
flush();
205+
setFmt("en");
206+
flush();
207+
setOpen("b");
208+
flush();
209+
setFmt("fr");
210+
flush();
211+
expect(out).toEqual(["ru:a", "en:a", "fr:a"]);
212+
while (resolvers.length) resolvers.shift()!();
213+
await settle();
214+
expect(out).toEqual(["ru:a", "en:a", "fr:a", "fr:b"]);
215+
expect(error.mock.calls.map(c => String(c[0]).split("\n")[0])).toEqual([]);
216+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*
5+
* #4354 as it ships: `setOpen(false)` is held by a `<Loading>` sibling's
6+
* re-ask, so the `<Show>` branch stays on screen while its selection has
7+
* already re-run in the held world. An unrelated write re-runs the branch's
8+
* text binding, which reads the narrowed accessor beside `fmt()`; it must
9+
* read the screen's value, not halt. The branch closes when the hold lands.
10+
*/
11+
import { afterEach, describe, expect, test, vi } from "vitest";
12+
import { createMemo, createSignal, flush, Loading, resetErrorHalt, Show } from "solid-js";
13+
import { render } from "../src/index.js";
14+
15+
afterEach(() => {
16+
resetErrorHalt();
17+
vi.restoreAllMocks();
18+
});
19+
20+
async function settle() {
21+
for (let i = 0; i < 3; i++) {
22+
await Promise.resolve();
23+
flush();
24+
}
25+
}
26+
27+
describe("<Show> branch re-run during the hold that closes it (#4354)", () => {
28+
for (const keyed of [false, true])
29+
test(keyed ? "keyed" : "non-keyed", async () => {
30+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
31+
const container = document.createElement("div");
32+
document.body.appendChild(container);
33+
const [open, setOpen] = createSignal(true);
34+
const [fmt, setFmt] = createSignal("ru");
35+
const resolvers: (() => void)[] = [];
36+
const data = createMemo(() => {
37+
const o = open();
38+
return new Promise<string>(r => resolvers.push(() => r(`data-${o}`)));
39+
});
40+
const dispose = render(
41+
() => (
42+
<>
43+
{keyed ? (
44+
<Show when={open() && "sel"} keyed fallback={<i>closed</i>}>
45+
{v => <p>{`${fmt()}:${v}`}</p>}
46+
</Show>
47+
) : (
48+
<Show when={open() && "sel"} fallback={<i>closed</i>}>
49+
{v => <p>{`${fmt()}:${v()}`}</p>}
50+
</Show>
51+
)}
52+
<Loading fallback={<b>wait</b>}>
53+
<span>{data()}</span>
54+
</Loading>
55+
</>
56+
),
57+
container
58+
);
59+
const view = () => container.textContent;
60+
61+
flush();
62+
resolvers.shift()!();
63+
await settle();
64+
expect(view()).toBe("ru:seldata-true");
65+
66+
setOpen(false); // held by data's re-ask: the branch stays on screen
67+
flush();
68+
expect(view()).toBe("ru:seldata-true");
69+
70+
setFmt("en"); // unrelated write re-runs the on-screen binding mid-hold
71+
flush();
72+
expect(view()).toBe("en:seldata-true");
73+
74+
resolvers.shift()!();
75+
await settle();
76+
expect(view()).toBe("closeddata-false");
77+
expect(error.mock.calls.map(c => String(c[0]).split("\n")[0])).toEqual([]);
78+
79+
setFmt("fr");
80+
flush();
81+
expect(view()).toBe("closeddata-false");
82+
dispose();
83+
container.remove();
84+
});
85+
});

0 commit comments

Comments
 (0)