|
| 1 | +/** |
| 2 | + * #4629 (the memo half): an async list `issues`, a `createOptimistic` log of |
| 3 | + * assignments overlaid on it by a memo. Action 1 is accepted and its |
| 4 | + * `refresh(issues)` is in flight when action 2 guesses into the same log. |
| 5 | + * |
| 6 | + * `issues` is not derived from the guess, so F-6's lane hold does not apply: |
| 7 | + * what lane work displays is the screen plus the lane's own values, and a |
| 8 | + * plain read in lane work of a node the parent holds is the screen's value |
| 9 | + * (D-26, clarified 2026-10-28). Action 1's re-ask is a `refresh()`, which |
| 10 | + * re-asks the same question and holds nothing new (D-34, D-31). The two |
| 11 | + * guesses share the log slot, so they entangle (D-33): the log reverts only |
| 12 | + * when the last of them settles, and a rejected second action reverts only |
| 13 | + * its own guess. |
| 14 | + */ |
| 15 | +import { describe, expect, it } from "vitest"; |
| 16 | +import { |
| 17 | + action, |
| 18 | + createMemo, |
| 19 | + createOptimistic, |
| 20 | + createRenderEffect, |
| 21 | + createRoot, |
| 22 | + createSignal, |
| 23 | + flush, |
| 24 | + mapArray, |
| 25 | + refresh |
| 26 | +} from "../src/index.js"; |
| 27 | + |
| 28 | +type Row = { id: number; a: string }; |
| 29 | + |
| 30 | +const tick = async () => { |
| 31 | + for (let i = 0; i < 4; i++) { |
| 32 | + await new Promise(r => setTimeout(r, 0)); |
| 33 | + flush(); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +type Read = "issuesFirst" | "guessFirst" | "viaMemo" | "viaMemoFirst"; |
| 38 | + |
| 39 | +function run(read: Read = "issuesFirst") { |
| 40 | + const asks: { key: string; res: (v: any) => void; rej: (e: any) => void }[] = []; |
| 41 | + const ask = (key: string) => new Promise<any>((res, rej) => asks.push({ key, res, rej })); |
| 42 | + const settle = async (key: string, v: any, fail = false) => { |
| 43 | + const i = asks.findIndex(a => a.key === key); |
| 44 | + const a = asks.splice(i, 1)[0]; |
| 45 | + fail ? a.rej(v) : a.res(v); |
| 46 | + await tick(); |
| 47 | + }; |
| 48 | + const server: Row[] = [ |
| 49 | + { id: 3, a: "-" }, |
| 50 | + { id: 4, a: "dana" } |
| 51 | + ]; |
| 52 | + let screen = ""; |
| 53 | + let assign!: (id: number) => any; |
| 54 | + createRoot(() => { |
| 55 | + const issues = createMemo<Row[]>(() => ask("issues")); |
| 56 | + const [truth] = createSignal<Row[]>(() => []); |
| 57 | + const [assigning, guess] = createOptimistic<Row[]>(() => truth()); |
| 58 | + // The guess read straight, or through a memo derived from it (the held |
| 59 | + // pass is then a grandchild of the guessed node). |
| 60 | + const byId = createMemo(() => { |
| 61 | + const m: Record<number, string> = {}; |
| 62 | + for (const x of assigning()) m[x.id] = x.a; |
| 63 | + return m; |
| 64 | + }); |
| 65 | + const guessed = () => { |
| 66 | + if (read === "viaMemo" || read === "viaMemoFirst") return byId(); |
| 67 | + const m: Record<number, string> = {}; |
| 68 | + for (const x of assigning()) m[x.id] = x.a; |
| 69 | + return m; |
| 70 | + }; |
| 71 | + const overlay = createMemo(() => { |
| 72 | + const first = read === "guessFirst" || read === "viaMemoFirst" ? guessed() : undefined; |
| 73 | + return issues().map(i => { |
| 74 | + const a = (first ?? guessed())[i.id]; |
| 75 | + return a ? { ...i, a } : i; |
| 76 | + }); |
| 77 | + }); |
| 78 | + const rows = mapArray(overlay, (r: Row) => () => `[#${r.id} ${r.a}]`); |
| 79 | + createRenderEffect( |
| 80 | + () => |
| 81 | + rows() |
| 82 | + .map(f => f()) |
| 83 | + .join(""), |
| 84 | + v => void (screen = v) |
| 85 | + ); |
| 86 | + assign = action(function* (id: number) { |
| 87 | + guess(xs => [...xs, { id, a: "guest" }]); |
| 88 | + try { |
| 89 | + yield ask(`assign:${id}`); |
| 90 | + } catch { |
| 91 | + return; |
| 92 | + } |
| 93 | + refresh(issues); |
| 94 | + }); |
| 95 | + }); |
| 96 | + return { |
| 97 | + get screen() { |
| 98 | + return screen; |
| 99 | + }, |
| 100 | + settle, |
| 101 | + server, |
| 102 | + assign: (id: number) => assign(id), |
| 103 | + snap: () => server.map(x => ({ ...x })) |
| 104 | + }; |
| 105 | +} |
| 106 | + |
| 107 | +async function toSecondGuess(read?: Read) { |
| 108 | + const t = run(read); |
| 109 | + flush(); |
| 110 | + await tick(); |
| 111 | + await t.settle("issues", t.snap()); |
| 112 | + expect(t.screen).toBe("[#3 -][#4 dana]"); |
| 113 | + t.assign(3); |
| 114 | + await tick(); |
| 115 | + expect(t.screen).toBe("[#3 guest][#4 dana]"); |
| 116 | + t.server[0].a = "guest"; |
| 117 | + await t.settle("assign:3", {}); |
| 118 | + // action 1's refresh(issues) is in flight |
| 119 | + expect(t.screen).toBe("[#3 guest][#4 dana]"); |
| 120 | + t.assign(4); |
| 121 | + await tick(); |
| 122 | + return t; |
| 123 | +} |
| 124 | + |
| 125 | +describe("#4629: a second guess into a log beside the first action's re-ask (memo)", () => { |
| 126 | + it("the guess shows at once, beside the screen's issues (D-26), and stays", async () => { |
| 127 | + const t = await toSecondGuess(); |
| 128 | + // (4) |
| 129 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 130 | + await t.settle("issues", t.snap()); |
| 131 | + // (5) action 1's re-ask lands; action 2's guess still stands |
| 132 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 133 | + t.server[1].a = "guest"; |
| 134 | + await t.settle("assign:4", {}); |
| 135 | + await t.settle("issues", t.snap()); |
| 136 | + // (6) both actions settled on the truth |
| 137 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 138 | + }); |
| 139 | + |
| 140 | + // D-28: whether a read throws is the read's own — not the order of the |
| 141 | + // reads around it. Reading the log before `issues` showed the guess at |
| 142 | + // once already. |
| 143 | + it("either read order, read straight or through a memo: the guess shows at once", async () => { |
| 144 | + for (const read of ["issuesFirst", "guessFirst", "viaMemo", "viaMemoFirst"] as const) { |
| 145 | + const t = await toSecondGuess(read); |
| 146 | + expect(t.screen, read).toBe("[#3 guest][#4 guest]"); |
| 147 | + await t.settle("issues", t.snap()); |
| 148 | + expect(t.screen, read).toBe("[#3 guest][#4 guest]"); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + it("through a memo, a rejected second action reverts its row", async () => { |
| 153 | + const t = await toSecondGuess("viaMemo"); |
| 154 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 155 | + await t.settle("issues", t.snap()); |
| 156 | + await t.settle("assign:4", new Error("no"), true); |
| 157 | + expect(t.screen).toBe("[#3 guest][#4 dana]"); |
| 158 | + }); |
| 159 | + |
| 160 | + it("a rejected second action reverts its row", async () => { |
| 161 | + const t = await toSecondGuess(); |
| 162 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 163 | + await t.settle("issues", t.snap()); |
| 164 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 165 | + await t.settle("assign:4", new Error("no"), true); |
| 166 | + expect(t.screen).toBe("[#3 guest][#4 dana]"); |
| 167 | + }); |
| 168 | + |
| 169 | + // D-33: the two guesses share the log slot, so the slot reverts only when |
| 170 | + // the last of the two actions settles — action 1 is out until its re-ask |
| 171 | + // lands (D-35: a plain load the action asked for is still in flight). |
| 172 | + it("a second action rejected before action 1's re-ask lands reverts with it", async () => { |
| 173 | + const t = await toSecondGuess(); |
| 174 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 175 | + await t.settle("assign:4", new Error("no"), true); |
| 176 | + expect(t.screen).toBe("[#3 guest][#4 guest]"); |
| 177 | + await t.settle("issues", t.snap()); |
| 178 | + expect(t.screen).toBe("[#3 guest][#4 dana]"); |
| 179 | + }); |
| 180 | +}); |
0 commit comments