Skip to content

Commit 72bd432

Browse files
committed
fix(signals): derived store follows a memo's later pending and errors (#4271)
A source's status reaching a projection's derive by propagation marks the derive without re-running it, so the family walk that its own pass does (wake / error) never ran: the leaves' readers saw no pending and the error was swallowed. propagateStatus now hands the status to the derive's family. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent d786bcf commit 72bd432

5 files changed

Lines changed: 96 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 derived store (`createProjection` or `createStore(fn)`) over a memo now follows the memo after it has shown a value: when the memo goes pending again, `isPending` on the store's keys reports it, and when the memo later throws, the error reaches the error boundary instead of the store silently keeping its last value. (#4271)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,9 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
10261026
joinFuture(txOf(sub));
10271027
}
10281028
notifyStatus(sub, status, error);
1029+
// A derived store's derive (store/projection.ts): marked, not re-run —
1030+
// its leaves' readers are not its dependents, so the family walks them.
1031+
(sub as any).fam?.st(status, error);
10291032
}
10301033
});
10311034
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
* rules a memo's read follows. No per-leaf back-pointer, nothing in core.
1818
*/
1919
import { attrHooks } from "../core/attribution-hooks.js";
20-
import { CONFIG_AUTO_DISPOSE, STATUS_PENDING, STATUS_UNINITIALIZED } from "../core/constants.js";
20+
import {
21+
CONFIG_AUTO_DISPOSE,
22+
STATUS_ERROR,
23+
STATUS_PENDING,
24+
STATUS_UNINITIALIZED
25+
} from "../core/constants.js";
2126
import { computed } from "../core/core.js";
2227
import { NotReadyError } from "../core/error.js";
2328
import {
@@ -180,6 +185,14 @@ function settleFamily(fam: StoreFamily): void {
180185
});
181186
}
182187

188+
/** A source's status reached the derive by propagation (core
189+
* `propagateStatus`), its pass not re-run: the family learns it as from the
190+
* pass's own throw. */
191+
function familyStatus(this: StoreFamily, status: number, error: unknown): void {
192+
if (status & STATUS_PENDING) wakeFamily(this, error);
193+
else if (status & STATUS_ERROR) errorFamily(this);
194+
}
195+
183196
function forEachFamilyNode(fam: StoreFamily, fn: (n: Signal<any>) => void): void {
184197
for (const t of fam.live) {
185198
const nodes = t.n;
@@ -319,6 +332,7 @@ function createProjectionInternal<T extends object = {}>(
319332
map: new WeakMap(),
320333
node: null,
321334
live: new Set(),
335+
st: familyStatus,
322336
shallow: !!options?.shallow,
323337
// The row key, for a child paired after the adoption (store.ts
324338
// `adoptChild`, #3712) as the adoption pairs them.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export interface StoreFamily {
3535
* their readers (A9) — the family's enumerable index, in place of a
3636
* per-leaf chain; a target leaves it with its last node. */
3737
live: Set<StoreTarget>;
38+
/** A source's status reaching the derive by propagation (`familyStatus`). */
39+
st?: (status: number, error: unknown) => void;
3840
shallow?: boolean;
3941
/** Derive run counter (proj R37): the run whose draft is live. */
4042
run?: number;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* #4271: a projection over a memo follows the memo after it has shown a
3+
* value — the memo going pending again reports pending through the
4+
* projection, and a later throw reaches the error boundary.
5+
*/
6+
import { expect, it } from "vitest";
7+
import {
8+
createErrorBoundary,
9+
createMemo,
10+
createProjection,
11+
createRenderEffect,
12+
createRoot,
13+
createSignal,
14+
flush,
15+
isPending,
16+
untrack
17+
} from "../src/index.js";
18+
19+
it("#4271: a projection over a memo reports its later pending and errors", async () => {
20+
const [q, setQ] = createSignal("a");
21+
let resolveB!: (v: { id: number; n: string }[]) => void;
22+
let screen = "";
23+
const dispose = createRoot(d => {
24+
const src = createMemo(() => {
25+
const v = q();
26+
if (v === "a") throw new Error("too short");
27+
if (v === "b") return new Promise<{ id: number; n: string }[]>(r => (resolveB = r));
28+
return [{ id: 1, n: v }];
29+
});
30+
const proj = createProjection(() => src(), [], { key: "id" });
31+
const out = untrack(() =>
32+
createErrorBoundary(
33+
() => `${proj.length} ${isPending(() => proj.length) ? "pending" : "-"}`,
34+
err => "error:" + (err() as Error).message
35+
)
36+
);
37+
createRenderEffect(
38+
() => out(),
39+
v => {
40+
screen = v;
41+
}
42+
);
43+
return d;
44+
});
45+
const tick = async () => {
46+
await new Promise(r => setTimeout(r, 0));
47+
flush();
48+
};
49+
flush();
50+
await tick();
51+
expect(screen).toBe("error:too short");
52+
setQ("c");
53+
flush();
54+
await tick();
55+
expect(screen).toBe("1 -");
56+
setQ("b");
57+
flush();
58+
await tick();
59+
expect(screen).toBe("1 pending");
60+
resolveB([
61+
{ id: 2, n: "b" },
62+
{ id: 3, n: "b" }
63+
]);
64+
await tick();
65+
expect(screen).toBe("2 -");
66+
setQ("a");
67+
flush();
68+
await tick();
69+
expect(screen).toBe("error:too short");
70+
dispose();
71+
});

0 commit comments

Comments
 (0)