Skip to content

Commit e604b5a

Browse files
committed
fix(signals): keyed derived store over an async memo follows every landing (#4305)
The family's wake (#4271) parked the leaves' readers on whatever source took the derive pending: for an upstream memo's flight, a NotReady of that memo. The leaves are not that memo's dependents, and the landing's family settle retires only the derive, so a reader the landing left unchanged (a memo per row reading `row.name`) never re-ran and stayed pending, holding the frame on the first landing. The wake now always parks readers on the derive itself; its `error` argument goes. The same fix covers a derive re-run while its source is pending (its pass throws the memo's NotReady), which was broken before #4280 too. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 0847122 commit e604b5a

5 files changed

Lines changed: 143 additions & 8 deletions

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 keyed derived store over an async memo follows every landing (#4305): when the source goes pending, the readers of the store's keys wait on the derive itself, so a reader whose value a landing leaves unchanged (a memo per row under `mapArray`/`<For>`) is released by the landing instead of holding the first one forever. The same holds when the derive re-runs while its source is pending.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
10351035
notifyStatus(sub, status, error);
10361036
// A derived store's derive (store/projection.ts): marked, not re-run —
10371037
// its leaves' readers are not its dependents, so the family walks them.
1038-
(sub as any).fam?.st(status, error);
1038+
(sub as any).fam?.st(status);
10391039
}
10401040
});
10411041
}

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,14 @@ function cloneState<T extends object>(v: T, shallow: boolean): T {
143143
* pending derivatively on the derive (its readers hold; it re-runs at the
144144
* landing). The leaves do not subscribe to the derive, so the family does
145145
* the walk. */
146-
function wakeFamily(fam: StoreFamily, error: unknown): void {
146+
function wakeFamily(fam: StoreFamily): void {
147147
const fw = fam.node!;
148+
// Pending on the derive itself, whatever source took it there (an upstream
149+
// memo's flight reaches it as a NotReady of that memo, #4271): the leaves
150+
// are not that source's dependents, so only the family's settle — which
151+
// retires the derive (`settleFamily`) — reaches the readers parked here. A
152+
// reader the landing leaves unchanged does not re-run to clear it (#4305).
153+
const error = new NotReadyError(fw);
148154
fam.woke = true;
149155
forEachFamilyNode(fam, n => {
150156
if (n._subs === null) return;
@@ -188,8 +194,8 @@ function settleFamily(fam: StoreFamily): void {
188194
/** A source's status reached the derive by propagation (core
189195
* `propagateStatus`), its pass not re-run: the family learns it as from the
190196
* pass's own throw. */
191-
function familyStatus(this: StoreFamily, status: number, error: unknown): void {
192-
if (status & STATUS_PENDING) wakeFamily(this, error);
197+
function familyStatus(this: StoreFamily, status: number): void {
198+
if (status & STATUS_PENDING) wakeFamily(this);
193199
else if (status & STATUS_ERROR) errorFamily(this);
194200
}
195201

@@ -287,7 +293,7 @@ export function runProjectionComputed<T extends object>(
287293
// #2897) — and the flush that runs them advances the clock, so a
288294
// later tracked re-read may retry (core `read`).
289295
if (first && owner._statusFlags & STATUS_UNINITIALIZED) return;
290-
if (pending) wakeFamily(fam, error);
296+
if (pending) wakeFamily(fam);
291297
else errorFamily(fam);
292298
});
293299
if (!owner._loading) commit(sync as void | T);
@@ -302,7 +308,7 @@ export function runProjectionComputed<T extends object>(
302308
// they do not subscribe to the derive.
303309
// A synchronous error the same way (#3956: a rerun that finds the
304310
// entry failed throws): every live reader re-derives and meets it.
305-
if (!first) e instanceof NotReadyError ? wakeFamily(fam, e) : errorFamily(fam);
311+
if (!first) e instanceof NotReadyError ? wakeFamily(fam) : errorFamily(fam);
306312
throw e;
307313
}
308314
// A pass that returned with a flight up (an iterator's sync first yield):
@@ -315,7 +321,7 @@ export function runProjectionComputed<T extends object>(
315321
!owner._loading &&
316322
!first
317323
)
318-
wakeFamily(fam, owner._x?._error);
324+
wakeFamily(fam);
319325
return owner;
320326
}
321327

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface StoreFamily {
3636
* per-leaf chain; a target leaves it with its last node. */
3737
live: Set<StoreTarget>;
3838
/** A source's status reaching the derive by propagation (`familyStatus`). */
39-
st?: (status: number, error: unknown) => void;
39+
st?: (status: number) => void;
4040
shallow?: boolean;
4141
/** Derive run counter (proj R37): the run whose draft is live. */
4242
run?: number;
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* #4305: a keyed projection over an async memo follows every landing. The
3+
* memo going pending marks the derive without re-running it (#4271), and the
4+
* family wakes the leaves' readers pending on the derive — the source the
5+
* landing's settle walk retires. A per-row memo reading a leaf whose value the
6+
* landing leaves unchanged does not re-run, so a wake parked on the upstream
7+
* memo instead held it (and the render effect over it) after the first landing.
8+
* "rederive": the derive re-runs while the memo is pending (another source of
9+
* it changes), its pass throwing the memo's NotReady — the same wake.
10+
*/
11+
import { describe, expect, it } from "vitest";
12+
import {
13+
createMemo,
14+
createProjection,
15+
createRenderEffect,
16+
createRoot,
17+
createSignal,
18+
flush,
19+
mapArray
20+
} from "../src/index.js";
21+
22+
type Row = { id: number; name: string };
23+
24+
const tick = async () => {
25+
for (let i = 0; i < 3; i++) {
26+
await new Promise(r => setTimeout(r, 0));
27+
flush();
28+
}
29+
};
30+
31+
for (const mode of ["row-memo", "direct", "rederive"] as const) {
32+
describe(`#4305 (${mode})`, () => {
33+
it("a keyed projection over an async memo follows every landing", async () => {
34+
const [q, setQ] = createSignal("");
35+
const [other, setOther] = createSignal(0);
36+
const res: Record<string, (rows: Row[]) => void> = {};
37+
let screen = "";
38+
const dispose = createRoot(d => {
39+
const src = createMemo(() =>
40+
q() === "" ? ([] as Row[]) : new Promise<Row[]>(r => (res[q()] = r))
41+
);
42+
const rows = createProjection(
43+
() => {
44+
other();
45+
return src();
46+
},
47+
[] as Row[],
48+
{ key: "id" }
49+
);
50+
if (mode === "direct")
51+
createRenderEffect(
52+
() => rows.map(r => r.name).join(","),
53+
v => {
54+
screen = v;
55+
}
56+
);
57+
else {
58+
const views = mapArray(
59+
() => rows,
60+
row => {
61+
const name = createMemo(() => row.name);
62+
return () => name();
63+
}
64+
);
65+
createRenderEffect(
66+
() =>
67+
views()
68+
.map(f => f())
69+
.join(","),
70+
v => {
71+
screen = v;
72+
}
73+
);
74+
}
75+
return d;
76+
});
77+
flush();
78+
await tick();
79+
expect(screen).toBe("");
80+
81+
setQ("ap");
82+
flush();
83+
await tick();
84+
res.ap([
85+
{ id: 1, name: "apple" },
86+
{ id: 2, name: "apricot" }
87+
]);
88+
await tick();
89+
expect(screen).toBe("apple,apricot");
90+
91+
// Row 1 survives with its name unchanged: its memo does not re-run.
92+
setQ("app");
93+
flush();
94+
await tick();
95+
if (mode === "rederive") {
96+
setOther(1);
97+
flush();
98+
await tick();
99+
}
100+
expect(screen).toBe("apple,apricot");
101+
res.app([
102+
{ id: 1, name: "apple" },
103+
{ id: 3, name: "applesauce" },
104+
{ id: 4, name: "apple pie" }
105+
]);
106+
await tick();
107+
expect(screen).toBe("apple,applesauce,apple pie");
108+
109+
// Row 1 survives with a changed name; row 4 survives unchanged.
110+
setQ("appl");
111+
flush();
112+
await tick();
113+
expect(screen).toBe("apple,applesauce,apple pie");
114+
res.appl([
115+
{ id: 1, name: "apple tart" },
116+
{ id: 4, name: "apple pie" }
117+
]);
118+
await tick();
119+
expect(screen).toBe("apple tart,apple pie");
120+
121+
dispose();
122+
});
123+
});
124+
}

0 commit comments

Comments
 (0)