Skip to content

Commit 25b94f0

Browse files
authored
fix(signals): link a store reader to its derive's staged error (#4517) (#4519)
* fix(signals): link a store reader to its derive's staged error (#4517) A derive marked with a staged error (`stageMark`, #4456) carries no STATUS_ERROR until the commit, so `pullFamily` took it for a settled derive and pulled it untracked. The pull threw the staged error before the reader read any leaf, so the reader subscribed to nothing in the family: when the derive's source re-asked and landed, neither the wake nor the settle reached it. An `<Errored>` mounted in the change that errored its content stayed on its fallback over a keyed `createProjection`, while the same read of the memo recovered. A reader now links to a derive carrying a staged error as it does to a pending or errored one, and re-runs at the landing. The render-effect early return in that branch is for a derive with a flight up only: a staged error is an answer the frame reader has to meet (D-41; the #3887 pin). Pins: packages/signals/tests/errored-born-staged-error-projection-4517.test.ts, packages/web/test/errored-born-staged-error-projection-4517.spec.tsx (the issue's repro). Size: +8 to +10 B minified on the store scenarios, every one under its brotli cap; the three over-cap warnings are +0 B over next. * fix: address review findings The pullFamily comment block keeps its original lines, with one line on the staged-error pull in place of the three that narrated the issue. The web spec's header keeps only the jsxImportSource and vitest-environment pragmas, as the neighbouring specs do. The changeset states the behavior that changed in two sentences.
1 parent 2ba10bd commit 25b94f0

4 files changed

Lines changed: 174 additions & 2 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 reader of a derived store whose derive carries a staged error (D-41) now links to the derive, as a reader of a pending or errored one does, so it re-runs when the derive's source answers. An `<Errored>` mounted in the change that errored its content over a keyed `createProjection` showed its fallback for good; it now shows the answer.

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
CONFIG_MANUAL_WRITE,
3737
CONFIG_OVERRIDE,
3838
CONFIG_PROMOTED,
39+
CONFIG_STAGED_ERROR,
3940
CONFIG_VERDICT,
4041
EFFECT_RENDER,
4142
NOT_PENDING,
@@ -1844,7 +1845,8 @@ function pullFamily(target: StoreTarget): void {
18441845
// reader of a memo would — linked, so it re-runs at the landing (the
18451846
// next settled pass pulls without linking and the stale link trims), a
18461847
// verdict reader registered, a tracked pass suspended.
1847-
if (fw._statusFlags & (STATUS_PENDING | STATUS_ERROR)) {
1848+
// A staged error (D-41) throws at the pull, before any leaf would link the reader.
1849+
if (fw._statusFlags & (STATUS_PENDING | STATUS_ERROR) || fw._config & CONFIG_STAGED_ERROR) {
18481850
// A render effect outside the flight's own flush is the frame, not a
18491851
// derivation: it keeps what it shows and learns of the landing from the
18501852
// leaves the landing changes (unchanged leaves say nothing — it is not a
@@ -1858,7 +1860,7 @@ function pullFamily(target: StoreTarget): void {
18581860
if (
18591861
c !== null &&
18601862
c._type === EFFECT_RENDER &&
1861-
!(fw._statusFlags & STATUS_ERROR) &&
1863+
fw._statusFlags & STATUS_PENDING &&
18621864
fw._config & CONFIG_HELD &&
18631865
flushTransaction !== txOf(fw) &&
18641866
!linkedTo(c, fw)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { expect, it } from "vitest";
2+
import {
3+
createErrorBoundary,
4+
createLoadingBoundary,
5+
createMemo,
6+
createProjection,
7+
createRenderEffect,
8+
createRoot,
9+
createSignal,
10+
flush,
11+
mapArray,
12+
untrack
13+
} from "../src/index.js";
14+
15+
const tick = async () => {
16+
for (let i = 0; i < 4; i++) {
17+
await new Promise(r => setTimeout(r, 0));
18+
flush();
19+
}
20+
};
21+
22+
type Row = { id: number; name: string };
23+
24+
for (const keyed of [true, false])
25+
it(`an answer replaces the error a boundary was born with (projection: ${keyed}) (#4517)`, async () => {
26+
const asks: { key: string; res: (v: Row[]) => void }[] = [];
27+
const ask = (key: string) => new Promise<Row[]>(res => asks.push({ key, res }));
28+
const land = async (key: string, v: Row[]) => {
29+
for (const a of asks.filter(a => a.key === key)) {
30+
asks.splice(asks.indexOf(a), 1);
31+
a.res(v);
32+
}
33+
await tick();
34+
};
35+
const [q, setQ] = createSignal("");
36+
let screen: unknown;
37+
const dispose = createRoot(d => {
38+
const src = createMemo(() => {
39+
const s = q();
40+
if (s === "") return [] as Row[];
41+
if (s.length < 2) throw new Error("too short");
42+
return ask(`search:${s}`);
43+
});
44+
const results: () => Row[] = keyed
45+
? (() => {
46+
const p = createProjection(() => src(), [] as Row[], { key: "id" });
47+
return () => p;
48+
})()
49+
: src;
50+
const show = createMemo(() => q() !== "");
51+
const out = createMemo(() => {
52+
if (!show()) return undefined;
53+
return untrack(() =>
54+
createErrorBoundary(
55+
() =>
56+
createLoadingBoundary(
57+
() => mapArray(results, r => r.name),
58+
() => "loading"
59+
),
60+
e => `E:${(e() as Error).message}`
61+
)
62+
);
63+
});
64+
const resolve = (v: unknown): unknown =>
65+
typeof v === "function" ? resolve(v()) : Array.isArray(v) ? v.join("") : v;
66+
createRenderEffect(
67+
() => resolve(out()),
68+
v => void (screen = v)
69+
);
70+
return d;
71+
});
72+
await tick();
73+
expect(screen).toBe(undefined);
74+
setQ("a");
75+
await tick();
76+
expect(screen).toBe("E:too short");
77+
setQ("ap");
78+
await tick();
79+
expect(screen).toBe("E:too short");
80+
await land("search:ap", [
81+
{ id: 1, name: "apple" },
82+
{ id: 2, name: "apricot" }
83+
]);
84+
expect(screen).toBe("appleapricot");
85+
dispose();
86+
});
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
import { expect, test } from "vitest";
6+
import { render } from "../src/index.js";
7+
import {
8+
createMemo,
9+
createProjection,
10+
createSignal,
11+
Errored,
12+
flush,
13+
For,
14+
Loading,
15+
Show
16+
} from "solid-js";
17+
18+
type Row = { id: number; name: string };
19+
20+
const tick = async () => {
21+
for (let i = 0; i < 4; i++) {
22+
await new Promise(r => setTimeout(r, 0));
23+
flush();
24+
}
25+
};
26+
27+
test.each([true, false])(
28+
"an answer replaces the error a boundary was born with (projection: %s)",
29+
async projection => {
30+
const asks: { key: string; res: (v: Row[]) => void }[] = [];
31+
const ask = (key: string) => new Promise<Row[]>(res => asks.push({ key, res }));
32+
const land = async (key: string, v: Row[]) => {
33+
for (const a of asks.filter(a => a.key === key)) {
34+
asks.splice(asks.indexOf(a), 1);
35+
a.res(v);
36+
}
37+
await tick();
38+
};
39+
const container = document.createElement("div");
40+
const [q, setQ] = createSignal("");
41+
const dispose = render(() => {
42+
const src = createMemo(() => {
43+
const s = q();
44+
if (s === "") return [] as Row[];
45+
if (s.length < 2) throw new Error("too short");
46+
return ask(`search:${s}`);
47+
});
48+
const results: () => Row[] = projection
49+
? (() => {
50+
const p = createProjection(() => src(), [] as Row[], { key: "id" });
51+
return () => p;
52+
})()
53+
: src;
54+
return (
55+
<Show when={q() !== ""}>
56+
<Errored fallback={(e: () => any) => <p>{e().message}</p>}>
57+
<Loading fallback={<p>loading</p>}>
58+
<For each={results()}>{r => <i>{r.name}</i>}</For>
59+
</Loading>
60+
</Errored>
61+
</Show>
62+
);
63+
}, container);
64+
await tick();
65+
expect(container.textContent).toBe("");
66+
setQ("a");
67+
await tick();
68+
expect(container.textContent).toBe("too short");
69+
setQ("ap");
70+
await tick();
71+
expect(container.textContent).toBe("too short");
72+
await land("search:ap", [
73+
{ id: 1, name: "apple" },
74+
{ id: 2, name: "apricot" }
75+
]);
76+
expect(container.textContent).toBe("appleapricot");
77+
dispose();
78+
}
79+
);

0 commit comments

Comments
 (0)