Skip to content

Commit 5f07d04

Browse files
ryansolidclaude
andauthored
fix(signals): re-base an arrangement guess from the rows it wrote (#4441) (#4458)
A keyed arrangement guess re-based over an older truth (`_laneRebase`) wrote the composed rows back into `view.rows`, and the next re-base composed from those. A derive's landing writes key by key (`d.length = n`, then `d[i] = row`), so each partial write re-bases: the first one showed the old row at the truncated length, the re-base appended it as a truth row the guess lacked, and from then on it was part of "the guess". The removed row came back once the refresh landed (D-35, F-11). Each re-base now composes from the guess as the setter wrote it (`view.g`). Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 Co-authored-by: Claude <[email protected]>
1 parent 42924f4 commit 5f07d04

4 files changed

Lines changed: 132 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+
`createOptimisticStore`: a row removed by an accepted action no longer comes back when its refresh lands while another removal is pending; each re-base of an arrangement guess composes from the rows the setter wrote.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,10 @@ GlobalQueue._laneRebase = (el: Signal<any>, truth: unknown, older: boolean) => {
382382
}
383383
const used = new Set<unknown>();
384384
const rows: any[] = [];
385-
const guessed = view.rows as any[];
385+
// From the guess as written, not an earlier re-base's composition: a
386+
// derive's landing writes key by key, and a truth row one partial write
387+
// showed must not stay appended once the landing drops it (#4441).
388+
const guessed = (view.g ??= view.rows as any[]);
386389
for (let i = 0; i < guessed.length; i++) {
387390
const g = guessed[i];
388391
const k = isWrappable(g) ? keyFn(g) : undefined;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ export function sameKey(a: any, b: any): boolean {
283283
* revert against it by row identity — by key when the family has one
284284
* (the server's object for a guessed row is the same row). */
285285
export class LaneView {
286+
/** The rows the setter wrote, kept across re-bases: each composes from
287+
* them over the latest truth (optimistic.ts `_laneRebase`, #4441). */
288+
declare g?: any[];
286289
constructor(
287290
public rows: Record<PropertyKey, any>,
288291
/** The backing the guess is shown over: the committed one at the
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { expect, it } from "vitest";
2+
import {
3+
action,
4+
createLoadingBoundary,
5+
createOptimisticStore,
6+
createRenderEffect,
7+
createRoot,
8+
flush,
9+
mapArray,
10+
refresh
11+
} from "../src/index.js";
12+
13+
// #4441: two actions each remove a row with `splice`. The first is accepted
14+
// and refreshes; its refresh answers (without the row) while the second is
15+
// still pending. The answer is re-based under the second's arrangement guess:
16+
// the row both guesses and the answer removed must not come back.
17+
const tick = async () => {
18+
for (let i = 0; i < 4; i++) {
19+
await new Promise(r => setTimeout(r, 0));
20+
flush();
21+
}
22+
};
23+
24+
for (const boundary of [false, true])
25+
for (const form of ["splice", "replace"] as const)
26+
it(`a removed row stays removed when its refresh lands under another removal (${form}, Loading=${boundary})`, async () => {
27+
const asks: { key: string; res: (v: any) => void }[] = [];
28+
const ask = (key: string) => new Promise<any>(res => asks.push({ key, res }));
29+
const land = async (key: string, v: any) => {
30+
for (const a of asks.filter(a => a.key === key)) {
31+
asks.splice(asks.indexOf(a), 1);
32+
a.res(v);
33+
}
34+
await tick();
35+
};
36+
const frames: string[] = [];
37+
let view: () => string = () => "";
38+
const snap = (l: string) => frames.push(`${l}: ${view()}`);
39+
let closeOne!: (id: number) => any;
40+
let dispose!: () => void;
41+
createRoot(d => {
42+
dispose = d;
43+
const [issues, set] = createOptimisticStore(
44+
async (d: any[]) => {
45+
const r = await ask("fetch");
46+
d.length = r.length;
47+
r.forEach((x: any, i: number) => {
48+
d[i] = x;
49+
});
50+
},
51+
[] as any[],
52+
{ key: "id" }
53+
);
54+
closeOne = action(function* (id: number) {
55+
set((d: any) => {
56+
if (form === "splice") {
57+
const i = d.findIndex((t: any) => t.id === id);
58+
if (i >= 0) d.splice(i, 1);
59+
} else {
60+
const next = d.filter((t: any) => t.id !== id);
61+
d.length = next.length;
62+
next.forEach((x: any, i: number) => {
63+
d[i] = x;
64+
});
65+
}
66+
});
67+
yield ask(`close:${id}`);
68+
refresh(issues);
69+
});
70+
const list = () =>
71+
mapArray(
72+
() => issues,
73+
(it: any) => {
74+
const s = { id: "" };
75+
createRenderEffect(
76+
() => it.id,
77+
v => {
78+
s.id = v;
79+
}
80+
);
81+
return s;
82+
}
83+
);
84+
const b = boundary ? createLoadingBoundary(list, () => "loading") : list;
85+
createRenderEffect(
86+
() => {
87+
const v: any = b();
88+
return typeof v === "function" ? v() : v;
89+
},
90+
(v: any) => {
91+
view = () => (typeof v === "string" ? v : v.map((s: any) => `[#${s.id}]`).join(""));
92+
}
93+
);
94+
});
95+
await tick();
96+
await land("fetch", [{ id: 1 }, { id: 3 }, { id: 4 }]);
97+
snap("loaded");
98+
closeOne(1);
99+
await tick();
100+
snap("close 1");
101+
closeOne(4);
102+
await tick();
103+
snap("close 4");
104+
await land("close:1", {});
105+
snap("close 1 accepted");
106+
await land("fetch", [{ id: 3 }, { id: 4 }]);
107+
snap("refresh answered");
108+
await land("close:4", {});
109+
await land("fetch", [{ id: 3 }]);
110+
snap("close 4 settled");
111+
dispose();
112+
expect(frames).toEqual([
113+
"loaded: [#1][#3][#4]",
114+
"close 1: [#3][#4]",
115+
"close 4: [#3]",
116+
"close 1 accepted: [#3]",
117+
"refresh answered: [#3]",
118+
"close 4 settled: [#3]"
119+
]);
120+
});

0 commit comments

Comments
 (0)