Skip to content

Commit 4a4803c

Browse files
ryansolidclaude
andauthored
fix(signals): a confirming truth under a folded guess re-derives latest() readers (#4529)
Once a correction folds the truth beneath a keyed arrangement guess (D-34), `latest()` serves that truth (`foldedRead`). A later landing that confirmed the guess called `_confirmed` but notified nobody, so a derive that had read the earlier truth through `latest()` never re-ran; at the lane's end the commit equalled the guess the screen showed and nothing re-ran it either. A derived store over `latest(() => rows)` kept a confirmed row's key out for good. Every truth landing on a folded lane now notifies its readers. Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 Co-authored-by: Claude <[email protected]>
1 parent 46ab2dd commit 4a4803c

3 files changed

Lines changed: 108 additions & 3 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 derived store (or memo) reading a keyed optimistic store through `latest()` now picks up a confirmed row after a correction folded the truth beneath the guess: a confirming truth landing re-derives the `latest()` readers too.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,10 +446,13 @@ function fold(el: Signal<any>, truth: unknown): boolean {
446446
if (!l._shown || !(k?._config! & CONFIG_OVERRIDE) || !(k!._x!._lane as LaneView)?.removed)
447447
return false;
448448
const guess = el._x!._lane;
449-
if ((!el._equals || !el._equals(guess, truth)) && !GlobalQueue._echoes!(guess, truth)) {
449+
if ((!el._equals || !el._equals(guess, truth)) && !GlobalQueue._echoes!(guess, truth))
450450
l.folded = foldedRead;
451-
insertSubs(el);
452-
} else GlobalQueue._confirmed?.(l);
451+
else GlobalQueue._confirmed?.(l);
452+
// A folded lane's `latest()` serves the truth beneath the guess
453+
// (`foldedRead`): every truth landing there is news to its readers, a
454+
// confirming one too — they re-derive from it (D-34, F-6).
455+
if (l.folded) insertSubs(el);
453456
joinFuture(resolveTx(l._parent ?? l));
454457
return true;
455458
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* A derived store whose derive reads a keyed optimistic store through
3+
* `latest()` picks up every confirmed row's key once the actions settle.
4+
*
5+
* Two overlapping pushes put `b` and `c` in flight; the source echoing only
6+
* `b` folds the truth beneath the guess (D-34), so `latest()` serves the
7+
* truth from there (`foldedRead`). A third push `e` starts after that
8+
* correction. When the source confirms `c` and `e`, the new truth equals the
9+
* guess — a confirming landing — but it is still a new truth beneath the
10+
* folded guess: the `latest()` readers must re-derive from it (D-34, F-6).
11+
* Before the fix they were never notified, and at the lane's end the commit
12+
* matched the guess the screen showed, so nothing re-ran them either: the
13+
* derive stayed on `{ a, b, c }` with `e` missing for good, while the same
14+
* derive reading the rows plainly had all four keys.
15+
*/
16+
import { describe, expect, it } from "vitest";
17+
import {
18+
action,
19+
createMemo,
20+
createOptimisticStore,
21+
createRenderEffect,
22+
createRoot,
23+
createSignal,
24+
createStore,
25+
flush,
26+
latest,
27+
until
28+
} from "../../src/index.js";
29+
30+
type Row = { id: string; title: string };
31+
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
32+
const row = (id: string): Row => ({ id, title: id });
33+
34+
describe("a latest() derive over a folded optimistic store sees the confirming truth", () => {
35+
for (const read of ["plain", "latest", "memo-latest"] as const)
36+
it(`derive reads the rows ${read === "plain" ? "plainly" : `through ${read}`}`, async () => {
37+
const [source, setSource] = createSignal<Row[]>([row("a")]);
38+
let opt!: Row[];
39+
let set!: (fn: (d: Row[]) => void) => void;
40+
let der!: Record<string, string>;
41+
const dispose = createRoot(dispose => {
42+
const [s, ss] = createOptimisticStore<Row[]>(() => source(), []);
43+
opt = s;
44+
set = fn => ss(d => fn(d));
45+
const m = createMemo(() => latest(() => s.map(r => r.id)));
46+
const [d] = createStore<Record<string, string>>(dd => {
47+
const ids =
48+
read === "plain"
49+
? s.map(r => r.id)
50+
: read === "latest"
51+
? latest(() => s.map(r => r.id))
52+
: m();
53+
for (const id of ids) dd[id] = "v" + id;
54+
}, {});
55+
der = d;
56+
// A reader of the last row's key, so a leaf is born for each new row.
57+
createRenderEffect(
58+
() => {
59+
const ids = s.map(r => r.id);
60+
return der[ids[ids.length - 1]];
61+
},
62+
() => {}
63+
);
64+
return dispose;
65+
});
66+
flush();
67+
const push = (id: string) =>
68+
action(function* () {
69+
set(d => void d.push(row(id)));
70+
yield sleep(5);
71+
yield until(() => opt.some(r => r.id === id));
72+
})();
73+
74+
const p1 = push("b");
75+
flush();
76+
await sleep(2);
77+
const p2 = push("c");
78+
flush();
79+
await sleep(10);
80+
setSource([row("a"), row("b")]); // echoes the first push only: the truth folds (D-34)
81+
flush();
82+
const p3 = push("e");
83+
flush();
84+
await p1;
85+
setSource([row("a"), row("b"), row("c"), row("e")]); // confirms c and e
86+
flush();
87+
await p2;
88+
await p3;
89+
flush();
90+
await sleep(5);
91+
flush();
92+
93+
expect(opt.map(r => r.id)).toEqual(["a", "b", "c", "e"]);
94+
expect({ ...der }).toEqual({ a: "va", b: "vb", c: "vc", e: "ve" });
95+
dispose();
96+
});
97+
});

0 commit comments

Comments
 (0)