|
| 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