Skip to content

Commit e9d549f

Browse files
ryansolidclaude
andauthored
fix(signals): a projection's synchronous throw reaches its readers (#3956) (#3979)
A re-run of a projection that threw synchronously only woke the leaves' readers for NotReadyError; any other error stayed with the derive, which the leaves' readers do not subscribe to, so the screen kept the last good value. Wake them the same way so they re-derive and meet the error. Fixes #3956 Claude-Session: https://claude.ai/code/session_01LVm3wseHt9wRTY8LfPiHmQ Co-authored-by: Claude <[email protected]>
1 parent c51c5c9 commit e9d549f

3 files changed

Lines changed: 98 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 `createProjection` whose re-run throws synchronously (for example, reading an entry that has since failed) now delivers the error to the readers of its fields, so the nearest `<Errored>` shows it instead of the screen keeping the last good value.

‎packages/signals/src/store/projection.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ export function runProjectionComputed<T extends object>(
279279
// A flight went up (NotReady out of the pass — the derive's own, with
280280
// the derive as its source): the leaves' readers learn it from here —
281281
// they do not subscribe to the derive.
282-
if (e instanceof NotReadyError && !first) wakeFamily(fam, e);
282+
// A synchronous error the same way (#3956: a rerun that finds the
283+
// entry failed throws): every live reader re-derives and meets it.
284+
if (!first) e instanceof NotReadyError ? wakeFamily(fam, e) : errorFamily(fam);
283285
throw e;
284286
}
285287
// A pass that returned with a flight up (an iterator's sync first yield):
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { expect, it } from "vitest";
2+
import {
3+
createErrorBoundary,
4+
createLoadingBoundary,
5+
createProjection,
6+
createRenderEffect,
7+
createRoot,
8+
createSignal,
9+
flush
10+
} from "../../src/index.js";
11+
12+
const settle = async (ms: number) => {
13+
const end = Date.now() + ms;
14+
while (Date.now() < end) {
15+
await new Promise(r => setTimeout(r, 5));
16+
flush();
17+
}
18+
};
19+
20+
it("projection: a synchronous throw on a version-bumped rerun reaches Errored (#3956)", async () => {
21+
const fetcher = (k: string) =>
22+
new Promise<{ items: string[] }>((res, rej) =>
23+
setTimeout(() => (k === "bad" ? rej(new Error("refused")) : res({ items: [`row-${k}`] })), 20)
24+
);
25+
const [key, setKey] = createSignal("good");
26+
const [version, setVersion] = createSignal(0, { ownedWrite: true });
27+
type E = {
28+
status: "pending" | "ok" | "error";
29+
p: Promise<{ items: string[] }>;
30+
v?: { items: string[] };
31+
e?: unknown;
32+
};
33+
const cache = new Map<string, E>();
34+
const entry = (k: string) => {
35+
let e = cache.get(k);
36+
if (!e) {
37+
const ne: E = { status: "pending", p: fetcher(k) };
38+
ne.p.then(
39+
v => {
40+
ne.status = "ok";
41+
ne.v = v;
42+
setVersion(x => x + 1);
43+
},
44+
err => {
45+
ne.status = "error";
46+
ne.e = err;
47+
setVersion(x => x + 1);
48+
}
49+
);
50+
cache.set(k, ne);
51+
e = ne;
52+
}
53+
return e;
54+
};
55+
let screen: unknown;
56+
createRoot(() => {
57+
const p = createProjection<{ value?: { items: string[] } }>(() => {
58+
version();
59+
const e = entry(key());
60+
if (e.status === "error") throw e.e;
61+
if (e.status === "ok") return { value: e.v! } as never;
62+
return e.p.then(v => ({ value: v })) as never;
63+
}, {});
64+
const view = createErrorBoundary(
65+
() =>
66+
createLoadingBoundary(
67+
() => (p.value?.items ?? []).join(","),
68+
() => "PENDING"
69+
),
70+
() => "ERRORED"
71+
);
72+
createRenderEffect(
73+
() => {
74+
let v: any = view();
75+
while (typeof v === "function") v = v();
76+
return v;
77+
},
78+
v => {
79+
screen = v;
80+
}
81+
);
82+
});
83+
flush();
84+
await settle(60);
85+
expect(screen).toBe("row-good");
86+
setKey("bad");
87+
flush();
88+
await settle(200);
89+
expect(screen).toBe("ERRORED");
90+
});

0 commit comments

Comments
 (0)