Skip to content

Commit 22cc9e4

Browse files
fix(signals): retain query observation during optimistic rerenders
Keep an existing reader linked to a pending store-family derive when an optimistic guess reruns the reader. Add shared-binding signals and web regressions and a patch changeset. Addresses #4536.
1 parent 215c365 commit 22cc9e4

4 files changed

Lines changed: 219 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+
Keep observing a pending store query when a later optimistic guess re-runs an existing reader, so shared bindings do not show the new query's inputs beside rows from the previous answer.

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,8 +1885,17 @@ function pullFamily(target: StoreTarget): void {
18851885
// verdict window still asks it (`isPending` is true while the flight is
18861886
// up). The family's, not the target's: the edit stands in for the one
18871887
// flight the family has — not over a new question they do not answer
1888-
// (optimistic.ts `stands`, #4336).
1889-
if (verdict === null && fw._statusFlags & STATUS_PENDING && optHooks?.stands(target)) return;
1888+
// (optimistic.ts `stands`, #4336). A reader already observing the flight
1889+
// keeps that observation: dropping its link here can release a new
1890+
// question's held inputs beside a guess over the previous answer.
1891+
// The ordinary read below serves its committed frame or keeps it pending.
1892+
if (
1893+
verdict === null &&
1894+
fw._statusFlags & STATUS_PENDING &&
1895+
optHooks?.stands(target) &&
1896+
!(c?._deps && linkedTo(c, fw))
1897+
)
1898+
return;
18901899
// A pass the pull throws a staged error (D-41) is linked to the derive,
18911900
// untracked too — `handleAsync` asking a returned proxy whether it is a
18921901
// flight: the pass carries the error, and the re-ask or answer that
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { expect, test } from "vitest";
2+
import {
3+
action,
4+
createMemo,
5+
createOptimisticStore,
6+
createRenderEffect,
7+
createRoot,
8+
createSignal,
9+
flush,
10+
refresh
11+
} from "../src/index.js";
12+
13+
const tick = async () => {
14+
for (let i = 0; i < 4; i++) {
15+
await new Promise(r => setTimeout(r, 0));
16+
flush();
17+
}
18+
};
19+
20+
for (const order of ["question first", "list first"] as const)
21+
for (const mode of ["inline", "memo"] as const)
22+
test(`a shared effect keeps the question and its guessed rows consistent (${order}, ${mode})`, async () => {
23+
type Issue = { id: number };
24+
const asks: { key: string; resolve: (value: any) => void }[] = [];
25+
const ask = (key: string) => new Promise<any>(resolve => asks.push({ key, resolve }));
26+
const settle = async (key: string, value: any) => {
27+
const i = asks.findIndex(a => a.key === key);
28+
expect(i).toBeGreaterThanOrEqual(0);
29+
asks.splice(i, 1)[0].resolve(value);
30+
await tick();
31+
};
32+
const frames: string[] = [];
33+
let setFilter!: (value: string) => void;
34+
let closeOne!: (id: number) => unknown;
35+
let dispose!: () => void;
36+
createRoot(d => {
37+
dispose = d;
38+
const [filter, setFilterValue] = createSignal("open");
39+
setFilter = setFilterValue;
40+
const [issues, set] = createOptimisticStore<Issue[]>(() => ask(`issues:${filter()}`), [], {
41+
key: "id"
42+
});
43+
closeOne = action(function* (id: number) {
44+
set(list => {
45+
const i = list.findIndex(row => row.id === id);
46+
if (i >= 0) list.splice(i, 1);
47+
});
48+
yield ask(`close:${id}`);
49+
refresh(issues);
50+
});
51+
const screen = () => {
52+
let question: string;
53+
let rows: string;
54+
if (order === "question first") {
55+
question = filter();
56+
rows = issues.map(row => `[#${row.id}]`).join("");
57+
} else {
58+
rows = issues.map(row => `[#${row.id}]`).join("");
59+
question = filter();
60+
}
61+
return `${question}: ${rows}`;
62+
};
63+
createRenderEffect(
64+
mode === "memo" ? createMemo(screen) : screen,
65+
value => void frames.push(value)
66+
);
67+
});
68+
try {
69+
await tick();
70+
await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]);
71+
expect(frames.at(-1)).toBe("open: [#1][#3][#4]");
72+
setFilter("closed");
73+
await tick();
74+
expect(frames.at(-1)).toBe("open: [#1][#3][#4]");
75+
closeOne(1);
76+
await tick();
77+
// The guess can show over the old answer or wait with the pending read.
78+
expect(["open: [#3][#4]", "open: [#1][#3][#4]"]).toContain(frames.at(-1));
79+
await settle("issues:closed", [{ id: 1 }, { id: 2 }]);
80+
expect(frames.at(-1)).toBe("closed: [#2]");
81+
await settle("close:1", {});
82+
await settle("issues:closed", [{ id: 2 }]);
83+
expect(frames.at(-1)).toBe("closed: [#2]");
84+
expect(frames.filter(frame => frame.startsWith("closed") && /#3|#4/.test(frame))).toEqual(
85+
[]
86+
);
87+
} finally {
88+
dispose();
89+
}
90+
});
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
/**
6+
* A shared render effect must not pair the held filter change with a guess
7+
* over the previous answer. Separate bindings are covered by #4525.
8+
*/
9+
import { expect, test } from "vitest";
10+
import {
11+
Loading,
12+
action,
13+
createMemo,
14+
createOptimisticStore,
15+
createSignal,
16+
flush,
17+
refresh
18+
} from "solid-js";
19+
import { render } from "../src/index.js";
20+
21+
const tick = async () => {
22+
for (let i = 0; i < 4; i++) {
23+
await new Promise(r => setTimeout(r, 0));
24+
flush();
25+
}
26+
};
27+
28+
for (const on of [true, false])
29+
for (const order of ["question first", "list first"] as const)
30+
for (const mode of ["inline", "memo"] as const)
31+
test(`#4536: a single render effect keeps the question and optimistic rows in one world (on=${on}, ${order}, ${mode})`, async () => {
32+
const asks: { key: string; res: (v: any) => void }[] = [];
33+
const ask = (key: string) => new Promise<any>(res => asks.push({ key, res }));
34+
const settle = async (key: string, v: any) => {
35+
const i = asks.findIndex(a => a.key === key);
36+
expect(i).toBeGreaterThanOrEqual(0);
37+
asks.splice(i, 1)[0].res(v);
38+
await tick();
39+
};
40+
const container = document.body.appendChild(document.createElement("div"));
41+
// Every DOM state the screen passes through.
42+
const seen: string[] = [];
43+
const observer = new MutationObserver(() => seen.push(container.textContent!));
44+
observer.observe(container, { childList: true, subtree: true, characterData: true });
45+
let setFilter!: (f: string) => void;
46+
let closeOne!: (id: number) => any;
47+
const dispose = render(() => {
48+
const [filter, sf] = createSignal("open");
49+
setFilter = sf;
50+
const [issues, set] = createOptimisticStore<any[]>(() => ask(`issues:${filter()}`), [], {
51+
key: "id"
52+
});
53+
closeOne = action(function* (id: number) {
54+
set(list => {
55+
const i = list.findIndex(t => t.id === id);
56+
if (i >= 0) list.splice(i, 1);
57+
});
58+
yield ask(`close:${id}`);
59+
refresh(issues);
60+
});
61+
const screen = () => {
62+
let question: string;
63+
let rows: string;
64+
if (order === "question first") {
65+
question = filter();
66+
rows = issues.map(it => `[#${it.id}]`).join("");
67+
} else {
68+
rows = issues.map(it => `[#${it.id}]`).join("");
69+
question = filter();
70+
}
71+
return `${question}: ${rows}`;
72+
};
73+
const body = () => {
74+
const value = mode === "memo" ? createMemo(screen) : screen;
75+
return <p>{value()}</p>;
76+
};
77+
return on ? (
78+
<Loading on={filter()} fallback={<p>loading</p>}>
79+
{body()}
80+
</Loading>
81+
) : (
82+
<Loading fallback={<p>loading</p>}>{body()}</Loading>
83+
);
84+
}, container);
85+
try {
86+
await tick();
87+
await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]);
88+
expect(container.textContent).toBe("open: [#1][#3][#4]");
89+
setFilter("closed");
90+
await tick();
91+
expect(container.textContent).toBe(on ? "loading" : "open: [#1][#3][#4]");
92+
closeOne(1);
93+
await tick();
94+
// A reset boundary stays in fallback; otherwise the guess may wait
95+
// with the pending read or show over the held open answer.
96+
expect(on ? ["loading"] : ["open: [#3][#4]", "open: [#1][#3][#4]"]).toContain(
97+
container.textContent
98+
);
99+
await settle("issues:closed", [{ id: 1 }, { id: 2 }]);
100+
expect(container.textContent).toBe("closed: [#2]");
101+
await settle("close:1", {});
102+
expect(container.textContent).toBe("closed: [#2]");
103+
await settle("issues:closed", [{ id: 2 }]);
104+
expect(container.textContent).toBe("closed: [#2]");
105+
expect(asks).toEqual([]);
106+
// Never a row of the open list beside `closed` (F-1).
107+
expect(seen.filter(s => s.startsWith("closed") && /#3|#4/.test(s))).toEqual([]);
108+
} finally {
109+
observer.disconnect();
110+
dispose();
111+
container.remove();
112+
}
113+
});

0 commit comments

Comments
 (0)