Skip to content

Commit 9a71cb5

Browse files
committed
fix(signals): an optimistic reorder of keyless rows reaches <For> (#4344)
A setter's returned array is written by index. On rows without a key, the container's arrangement guess was judged equal to the committed rows, because two keyless rows at one index counted as the same row (#3898). The guess was dropped before the seam, so `<For>` (which reads the container) never re-ran, while direct reads saw the per-index guesses. A keyless row has only its contents: at the same index it is the same row when the store would reconcile it unchanged (the #3898 echo still confirms), and another row otherwise. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 56a982c commit 9a71cb5

4 files changed

Lines changed: 232 additions & 7 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+
`createOptimisticStore`: a reorder inside an action of rows without an `id` now shows in `<For>` (keyed and non-keyed) at once, as it already did in direct reads, and reverts on screen at the settle. Rows without a key are compared by their contents at each index: a row with the same contents is the same row (a server echo still confirms, #3898), and a row with other contents is a change of arrangement (#4344).

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ function rowSame(a: any, b: any, keyFn: ((item: any) => any) | null): boolean {
211211
if (keyFn === null || !isWrappable(a) || !isWrappable(b)) return false;
212212
const ka = keyFn(unwrapValue(a));
213213
const kb = keyFn(unwrapValue(b));
214-
// Keyless rows are identified by position — reconcile's fallback when the
215-
// key is missing. The caller already paired these by index, so two keyless
216-
// rows are the same row and the landing confirms the arrangement instead of
217-
// dissolving it. Dissolving held an external echo under the action, and a
218-
// mainline `until` then timed out on a source that already contained it
219-
// (#3898). Keyed rows still match only on the key.
220-
if (ka === undefined && kb === undefined) return true;
214+
// Keyless rows have only their contents. The caller already paired these
215+
// by index: the same contents are the same row, so a landing's echo
216+
// confirms the arrangement instead of dissolving it (dissolving held the
217+
// echo under the action, and a mainline `until` timed out, #3898). Other
218+
// contents are another row at the index — a reorder the list must show
219+
// (#4344). Keyed rows still match only on the key.
220+
if (ka === undefined && kb === undefined) return GlobalQueue._echoes!(a, b);
221221
return ka !== undefined && sameKey(ka, kb);
222222
}
223223

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* #4344 — an optimistic reorder inside an action, on rows without an `id`,
3+
* showed to direct reads but not to a list over the store (`mapArray`, what
4+
* `<For>` runs). The setter's returned array is written by index: each slot
5+
* gets the returned row. The container's arrangement guess was judged equal
6+
* to the committed rows, because two keyless rows at the same index counted
7+
* as the same row (#3898). The guess was dropped, so the list never re-ran.
8+
* A keyless row has only its contents. At the same index it is the same row
9+
* when its contents match (the #3898 echo), and a different row otherwise.
10+
*/
11+
import { describe, expect, test } from "vitest";
12+
import {
13+
action,
14+
createOptimisticStore,
15+
createRenderEffect,
16+
createRoot,
17+
createSignal,
18+
flush,
19+
mapArray
20+
} from "../src/index.js";
21+
22+
type Row = { id?: string; name: string };
23+
const tick = () => new Promise(r => setTimeout(r, 0));
24+
25+
function setup(keyed: boolean, withId: boolean, derived: boolean) {
26+
const row = (n: string): Row => (withId ? { id: n, name: n } : { name: n });
27+
const [source, setSource] = createSignal<Row[]>([row("a"), row("b"), row("c")]);
28+
let items!: Row[];
29+
let setItems!: (fn: (d: Row[]) => Row[] | void) => void;
30+
let screen = "";
31+
let dispose!: () => void;
32+
createRoot(d => {
33+
dispose = d;
34+
[items, setItems] = derived
35+
? (createOptimisticStore<Row[]>(() => source().map(r => ({ ...r })), []) as any)
36+
: createOptimisticStore<Row[]>([row("a"), row("b"), row("c")]);
37+
const list = keyed
38+
? mapArray(
39+
() => items,
40+
(r: Row) => () => r.name
41+
)
42+
: mapArray(
43+
() => items,
44+
(r: () => Row) => () => r().name,
45+
{ keyed: false }
46+
);
47+
createRenderEffect(
48+
() =>
49+
list()
50+
.map(f => f())
51+
.join(","),
52+
v => {
53+
screen = v;
54+
}
55+
);
56+
});
57+
flush();
58+
return {
59+
row,
60+
setSource,
61+
get items() {
62+
return items.map(r => r.name).join(",");
63+
},
64+
get screen() {
65+
return screen;
66+
},
67+
setItems,
68+
dispose
69+
};
70+
}
71+
72+
for (const derived of [false, true])
73+
for (const withId of [false, true])
74+
for (const keyed of [true, false])
75+
describe(`${derived ? "derived" : "plain"} store, keyed: ${keyed}, rows have id: ${withId}`, () => {
76+
test("a returned reorder shows to the list at once and reverts at the settle", async () => {
77+
const s = setup(keyed, withId, derived);
78+
await tick();
79+
flush();
80+
expect(s.screen).toBe("a,b,c");
81+
let release!: () => void;
82+
const server = new Promise<void>(r => (release = r));
83+
const done = action(function* () {
84+
s.setItems(() => [s.row("c"), s.row("b"), s.row("a")]);
85+
yield server;
86+
})();
87+
flush();
88+
expect(s.items).toBe("c,b,a");
89+
expect(s.screen).toBe("c,b,a");
90+
await tick();
91+
flush();
92+
expect(s.items).toBe("c,b,a");
93+
expect(s.screen).toBe("c,b,a");
94+
release();
95+
await done;
96+
flush();
97+
// Nothing wrote the truth: the guess reverts.
98+
expect(s.items).toBe("a,b,c");
99+
expect(s.screen).toBe("a,b,c");
100+
s.dispose();
101+
});
102+
103+
test("a swap written in the draft shows to the list at once and reverts", async () => {
104+
const s = setup(keyed, withId, derived);
105+
await tick();
106+
flush();
107+
let release!: () => void;
108+
const server = new Promise<void>(r => (release = r));
109+
const done = action(function* () {
110+
s.setItems(d => {
111+
const x = d[0];
112+
d[0] = d[2];
113+
d[2] = x;
114+
});
115+
yield server;
116+
})();
117+
flush();
118+
expect(s.items).toBe("c,b,a");
119+
expect(s.screen).toBe("c,b,a");
120+
release();
121+
await done;
122+
flush();
123+
expect(s.items).toBe("a,b,c");
124+
expect(s.screen).toBe("a,b,c");
125+
s.dispose();
126+
});
127+
128+
if (derived)
129+
test("the truth the action writes lands the reorder at the settle", async () => {
130+
const s = setup(keyed, withId, derived);
131+
await tick();
132+
flush();
133+
let release!: () => void;
134+
const server = new Promise<void>(r => (release = r));
135+
const done = action(function* () {
136+
s.setItems(() => [s.row("c"), s.row("b"), s.row("a")]);
137+
yield server;
138+
s.setSource([s.row("c"), s.row("b"), s.row("a")]);
139+
})();
140+
flush();
141+
expect(s.items).toBe("c,b,a");
142+
expect(s.screen).toBe("c,b,a");
143+
release();
144+
await done;
145+
flush();
146+
await tick();
147+
flush();
148+
expect(s.items).toBe("c,b,a");
149+
expect(s.screen).toBe("c,b,a");
150+
s.dispose();
151+
});
152+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*
5+
* #4344 — an optimistic reorder inside an action, on rows without an `id`:
6+
* direct store reads showed `c,b,a` while `<For>` (keyed and non-keyed) kept
7+
* rendering `a,b,c`. The container's arrangement guess was judged equal to
8+
* the committed rows (two keyless rows at one index counted as one row), so
9+
* `<For>` never re-ran. With an `id` the rows differ by key and it did.
10+
* Direct reads and `<For>` must agree, at the guess and after the settle.
11+
*/
12+
import { describe, expect, test } from "vitest";
13+
import { action, createOptimisticStore, flush, For } from "solid-js";
14+
import { render } from "../src/index.js";
15+
16+
const tick = () => new Promise(r => setTimeout(r, 0));
17+
18+
type Row = { id?: string; name: string };
19+
20+
for (const withId of [false, true])
21+
for (const keyed of [true, false])
22+
describe(`<For keyed={${keyed}}>, rows have id: ${withId}`, () => {
23+
test("a reorder inside an action renders at once and reverts at the settle", async () => {
24+
const row = (n: string): Row => (withId ? { id: n, name: n } : { name: n });
25+
const el = document.createElement("div");
26+
let release!: () => void;
27+
const server = new Promise<void>(r => (release = r));
28+
let items!: Row[];
29+
let reorder!: () => Promise<void>;
30+
const dispose = render(() => {
31+
const [store, setItems] = createOptimisticStore<Row[]>([row("a"), row("b"), row("c")]);
32+
items = store;
33+
reorder = action(function* () {
34+
setItems(() => [row("c"), row("b"), row("a")]);
35+
yield server;
36+
});
37+
return (
38+
<ul>
39+
{keyed ? (
40+
<For each={store}>{r => <li>{r.name}</li>}</For>
41+
) : (
42+
<For each={store} keyed={false}>
43+
{r => <li>{r().name}</li>}
44+
</For>
45+
)}
46+
</ul>
47+
);
48+
}, el);
49+
const rows = () => el.innerHTML;
50+
expect(rows()).toBe("<ul><li>a</li><li>b</li><li>c</li></ul>");
51+
const done = reorder();
52+
flush();
53+
// The guess shows at once (F-11): the same frame as the direct read.
54+
expect(items.map(r => r.name).join(",")).toBe("c,b,a");
55+
expect(rows()).toBe("<ul><li>c</li><li>b</li><li>a</li></ul>");
56+
await tick();
57+
flush();
58+
expect(items.map(r => r.name).join(",")).toBe("c,b,a");
59+
expect(rows()).toBe("<ul><li>c</li><li>b</li><li>a</li></ul>");
60+
release();
61+
await done;
62+
flush();
63+
// Nothing wrote the truth: the guess reverts, on screen too.
64+
expect(items.map(r => r.name).join(",")).toBe("a,b,c");
65+
expect(rows()).toBe("<ul><li>a</li><li>b</li><li>c</li></ul>");
66+
dispose();
67+
});
68+
});

0 commit comments

Comments
 (0)