Skip to content

Commit d9aa8f7

Browse files
ryansolidclaude
andauthored
fix(signals): a guess written while a new question is in flight re-bases over its answer (#4525) (#4534)
A keyed optimistic store's derive asks a new question (a plain `setFilter("closed")`), and before it answers an action guesses on the list (closes #1). The guess is composed over the old answer, so its arrangement carries the open list's rows (#3, #4). The closed answer is a mainline landing, current by provenance, so it folded beneath the guess (D-34) and the guess's rows stayed. Under `<Loading on={filter()}>` the boundary had already taken the filter change (D-14), so the region left its fallback showing `closed` beside the open list minus the guess until the action's refresh landed (F-1). The question was asked before the guess, so its answer is the older question for it (D-35): an arrangement guess written while its derive is pending on a new question (not a `refresh()`) is marked (`g` null) and that answer re-bases it by row key (`_laneRebase`, the #4482 composition): the guess's own edit over the closed answer, `closed: [#2]`, held beneath the guess. The first re-base consumes the mark, so later landings fold as before. Without `on`, the same frame now shows `closed: [#2]` too instead of holding `open: [#3][#4]` until the refresh. Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 Co-authored-by: Claude <[email protected]>
1 parent e44f824 commit d9aa8f7

4 files changed

Lines changed: 139 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 optimistic store's arrangement guess written while its derive asks a new question (an input changed, not a `refresh()`) is re-based over that question's answer, like an older truth (#4525). Before, the answer folded beneath the guess, so the rows the guess had only carried over from the old answer stayed on screen; under `<Loading on={filter()}>` the region left its fallback showing the new input beside the old list minus the guess until the action's refresh landed.

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
CONFIG_SLOT_NODE,
3131
EFFECT_RENDER,
3232
NOT_PENDING,
33+
STATUS_PENDING,
3334
REACTIVE_REASK
3435
} from "../core/constants.js";
3536
import { ext, isEqual, joinPassAt, read as readNode, untrack } from "../core/core.js";
@@ -187,7 +188,15 @@ export function notifyOptimisticWrites(
187188
if (k !== undefined && (!kept.has(k) || truth.has(r))) removed.add(k);
188189
}
189190
}
190-
optimisticWrite(getContainerNode(t), new LaneView(rows, base, removed) as any);
191+
const view = new LaneView(rows, base, removed);
192+
// Written while the derive asks a new question (an input changed, not a
193+
// `refresh()`): the guess's base answers the old one, so the rows it only
194+
// carried over are that answer's — the new answer re-bases it like an
195+
// older truth instead of folding beneath it (`_laneRebase`, F-1, #4525).
196+
const d = t._fam!.node;
197+
if (removed && d?._statusFlags! & STATUS_PENDING && !(d!._flags & REACTIVE_REASK))
198+
view.g = null;
199+
optimisticWrite(getContainerNode(t), view as any);
191200
}
192201
if (t.dk !== null) optimisticWrite(t.dk, {} as any);
193202
markOverlaid(t);
@@ -364,19 +373,23 @@ GlobalQueue._slotTake = (n, back) => {
364373
* node is born for it as the lane's derivation), on `length` only when the
365374
* committed length differs, and as the `LaneView`'s base for structural
366375
* reads. Unkeyed families compose
367-
* positionally — the guess stands as written (the documented residue). */
376+
* positionally — the guess stands as written (the documented residue).
377+
* A current truth re-bases, the same way, a guess written while its
378+
* question was being asked (`g` null, #4525): asked before the guess, it is
379+
* the older question; its rows are held beneath the guess. True: the lane
380+
* holds the landing (`laneWrite`). */
368381
GlobalQueue._laneRebase = (el: Signal<any>, truth: unknown, older: boolean) => {
369-
if (!older) return fold(el, truth);
370382
const t = (el as any)._host as StoreTarget | undefined;
371-
if (t === undefined || (el as any)._key !== $TRACK) return;
383+
if (!older && (t?.k?._x?._lane as LaneView)?.g !== null) return fold(el, truth);
384+
if (!t || (el as any)._key !== $TRACK) return true;
372385
const view = el._x!._lane;
373386
if (!(view instanceof LaneView)) return;
374387
if (!Array.isArray(truth) || !Array.isArray(view.rows)) {
375388
view.base = truth as Record<PropertyKey, any>;
376389
return;
377390
}
378-
const keyFn = t._fam?.key ?? null;
379-
if (keyFn === null) return;
391+
const keyFn = t._fam?.key;
392+
if (!keyFn) return;
380393
const byKey = new Map<unknown, any>();
381394
for (let i = 0; i < truth.length; i++) {
382395
const r = truth[i];
@@ -429,6 +442,7 @@ GlobalQueue._laneRebase = (el: Signal<any>, truth: unknown, older: boolean) => {
429442
if (len !== undefined && (len._config & CONFIG_OVERRIDE || base.length !== rows.length))
430443
laneSet(len, rows.length, lane);
431444
}
445+
return true;
432446
};
433447

434448
/** D-34 for a keyed arrangement guess: rows by key, not positions. A

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ export function sameKey(a: any, b: any): boolean {
289289
* (the server's object for a guessed row is the same row). */
290290
export class LaneView {
291291
/** The rows the setter wrote, kept across re-bases: each composes from
292-
* them over the latest truth (optimistic.ts `_laneRebase`, #4441). */
293-
declare g?: any[];
292+
* them over the latest truth (optimistic.ts `_laneRebase`, #4441).
293+
* `null` until the first re-base on a guess written while the derive
294+
* asked a new question: that question's answer re-bases it (#4525). */
295+
declare g?: any[] | null;
294296
constructor(
295297
public rows: Record<PropertyKey, any>,
296298
/** The backing the guess is shown over: the committed one at the
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
/**
6+
* #4525 (ReactiveScript timing fuzzer, issue tracker apps): a keyed
7+
* `createOptimisticStore` list whose question reads `filter()`. The filter
8+
* changes by a plain write, and before the new question answers an action
9+
* guesses on the list (closes #1), yields and then refreshes it.
10+
*
11+
* The guess was written over the open list while the closed question was in
12+
* flight: the rows it only carried over (#3, #4) are the open answer's, not
13+
* the guess's. The closed answer landed as a current truth and folded
14+
* beneath the guess (D-34), so with `Loading on={filter()}` — whose fallback
15+
* had already taken the filter change (D-14) — the region left its fallback
16+
* showing `closed` beside the open list minus the guess (F-1) until the
17+
* action's refresh landed.
18+
*
19+
* Ruled: the answer to a question asked before the guess re-bases it like
20+
* an older truth (D-35, `_laneRebase`): the guess's own edit, #1 removed,
21+
* over the closed answer, so the screen is `closed: [#2]` (the issue's second
22+
* acceptable frame), with or without `on`; never a row of the open list
23+
* beside `closed`.
24+
*/
25+
import { expect, test } from "vitest";
26+
import {
27+
For,
28+
Loading,
29+
action,
30+
createOptimisticStore,
31+
createSignal,
32+
flush,
33+
refresh
34+
} from "solid-js";
35+
import { render } from "../src/index.js";
36+
37+
const tick = async () => {
38+
for (let i = 0; i < 4; i++) {
39+
await new Promise(r => setTimeout(r, 0));
40+
flush();
41+
}
42+
};
43+
44+
for (const on of [true, false])
45+
test(`#4525: a guess written while a new question is in flight re-bases over its answer (on=${on})`, async () => {
46+
const asks: { key: string; res: (v: any) => void }[] = [];
47+
const ask = (key: string) => new Promise<any>(res => asks.push({ key, res }));
48+
const settle = async (key: string, v: any) => {
49+
const i = asks.findIndex(a => a.key === key);
50+
asks.splice(i, 1)[0].res(v);
51+
await tick();
52+
};
53+
const container = document.body.appendChild(document.createElement("div"));
54+
// Every DOM state the screen passes through.
55+
const seen: string[] = [];
56+
const observer = new MutationObserver(() => seen.push(container.textContent!));
57+
observer.observe(container, { childList: true, subtree: true, characterData: true });
58+
let setFilter!: (f: string) => void;
59+
let closeOne!: (id: number) => any;
60+
const dispose = render(() => {
61+
const [filter, sf] = createSignal("open");
62+
setFilter = sf;
63+
const [issues, set] = createOptimisticStore<any[]>(() => ask(`issues:${filter()}`), [], {
64+
key: "id"
65+
});
66+
closeOne = action(function* (id: number) {
67+
set(list => {
68+
const i = list.findIndex(t => t.id === id);
69+
if (i >= 0) list.splice(i, 1);
70+
});
71+
yield ask(`close:${id}`);
72+
refresh(issues);
73+
});
74+
const body = () => (
75+
<p>
76+
{filter()}: <For each={issues}>{(it: any) => <i>[#{it.id}]</i>}</For>
77+
</p>
78+
);
79+
return on ? (
80+
<Loading on={filter()} fallback={<p>loading</p>}>
81+
{body()}
82+
</Loading>
83+
) : (
84+
<Loading fallback={<p>loading</p>}>{body()}</Loading>
85+
);
86+
}, container);
87+
try {
88+
await tick();
89+
await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]);
90+
expect(container.textContent).toBe("open: [#1][#3][#4]");
91+
setFilter("closed");
92+
await tick();
93+
closeOne(1);
94+
await tick();
95+
expect(container.textContent).toBe(on ? "loading" : "open: [#3][#4]");
96+
await settle("issues:closed", [{ id: 1 }, { id: 2 }]);
97+
expect(container.textContent).toBe("closed: [#2]");
98+
await settle("close:1", {});
99+
expect(container.textContent).toBe("closed: [#2]");
100+
await settle("issues:closed", [{ id: 2 }]);
101+
expect(container.textContent).toBe("closed: [#2]");
102+
expect(asks).toEqual([]);
103+
// Never a row of the open list beside `closed` (F-1).
104+
expect(seen.filter(s => s.startsWith("closed") && /#3|#4/.test(s))).toEqual([]);
105+
} finally {
106+
observer.disconnect();
107+
dispose();
108+
container.remove();
109+
}
110+
});

0 commit comments

Comments
 (0)