|
| 1 | +/** |
| 2 | + * #4491: a reader that mounts while an unread derived store's derive has a |
| 3 | + * flight up reads the store like its memo (D-30, "in both forms"). |
| 4 | + * |
| 5 | + * `createStore(d => { d.v = m1() }, {})` over `m1 = async(m0)` with |
| 6 | + * `m0 = memo(latest(s) + s)`: `m0` is a verdict reader, so `m1` and the |
| 7 | + * derive are verdict readers through what they read (D-25 / F-6). Nothing |
| 8 | + * reads the store when `m1` re-asks (s: 1 -> 2); a reader of the store then |
| 9 | + * mounts mid-flight. |
| 10 | + * |
| 11 | + * - memo form (the reader reads `m1()`): the mount consumes unanswered async |
| 12 | + * and is held until `m1` answers (F-2): ["r:off", "--mounted", "r:4"]. |
| 13 | + * - store form, before: the derive was served `m1`'s committed value and |
| 14 | + * settled over it (A15's stale reader), so its leaf read as final. The |
| 15 | + * reader showed the old question's answer `r:2` before `r:4`: in the |
| 16 | + * mounting flush itself when the unread derive skips the flight (#4479), |
| 17 | + * at the landing otherwise (the derive's landing pass, a frame reader, |
| 18 | + * read the screen). |
| 19 | + * |
| 20 | + * The forms the #4479 review probed (a key read, `Object.keys`, a JSON |
| 21 | + * walk) must match the memo form, mounted mid-flight with and without a |
| 22 | + * plain write beside the flight (with it, the store form showed `r:5`, an |
| 23 | + * answer to a question between the two, before `r:6`). So must a reader |
| 24 | + * already on screen at the re-ask, with the plain write (before the fix the |
| 25 | + * whole-object readers showed `s:3` beside the old `v` ahead of `r:4`), and |
| 26 | + * a reader mounted after the landing, which shows at once: the hold reaches |
| 27 | + * neither. |
| 28 | + */ |
| 29 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 30 | +import { |
| 31 | + createMemo, |
| 32 | + createRenderEffect, |
| 33 | + createRoot, |
| 34 | + createSignal, |
| 35 | + createStore, |
| 36 | + flush, |
| 37 | + latest, |
| 38 | + resetErrorHalt |
| 39 | +} from "../src/index.js"; |
| 40 | + |
| 41 | +beforeEach(() => vi.useFakeTimers()); |
| 42 | +afterEach(() => { |
| 43 | + vi.useRealTimers(); |
| 44 | + resetErrorHalt(); |
| 45 | +}); |
| 46 | +const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms)); |
| 47 | +const tick = async (ms: number) => { |
| 48 | + await vi.advanceTimersByTimeAsync(ms); |
| 49 | + flush(); |
| 50 | +}; |
| 51 | + |
| 52 | +type Form = "memo" | "storeKey" | "storeKeys" | "storeJSON"; |
| 53 | +const storeForms = ["storeKey", "storeKeys", "storeJSON"] as const; |
| 54 | + |
| 55 | +/** The reader's display of `v`: every form shows the same text. */ |
| 56 | +async function run(form: Form, at: "before" | "during" | "duringWrite" | "afterLanding") { |
| 57 | + const log: string[] = []; |
| 58 | + const [s, setS] = createSignal(1); |
| 59 | + const [on, setOn] = createSignal(at === "before"); |
| 60 | + createRoot(() => { |
| 61 | + const m0 = createMemo(() => latest(s) + s()); |
| 62 | + const m1 = createMemo(async () => { |
| 63 | + const v = m0(); |
| 64 | + await sleep(100); |
| 65 | + return v; |
| 66 | + }); |
| 67 | + let read: () => unknown = () => m1(); |
| 68 | + if (form !== "memo") { |
| 69 | + const [st] = createStore<{ v?: number }>(d => { |
| 70 | + d.v = m1(); |
| 71 | + }, {}); |
| 72 | + read = |
| 73 | + form === "storeKey" |
| 74 | + ? () => st.v |
| 75 | + : form === "storeKeys" |
| 76 | + ? () => (Object.keys(st).join() === "v" ? (st as any).v : "keys?") |
| 77 | + : () => JSON.parse(JSON.stringify(st)).v; |
| 78 | + } |
| 79 | + if (at !== "during") |
| 80 | + createRenderEffect( |
| 81 | + () => `s:${s()}`, |
| 82 | + v => void log.push(v) |
| 83 | + ); |
| 84 | + createRenderEffect( |
| 85 | + () => (on() ? `r:${read()}` : "r:off"), |
| 86 | + v => void log.push(v) |
| 87 | + ); |
| 88 | + }); |
| 89 | + flush(); |
| 90 | + await tick(150); |
| 91 | + setS(2); // m1 re-asks |
| 92 | + flush(); |
| 93 | + log.push("--reask"); |
| 94 | + await tick(20); |
| 95 | + if (at === "during" || at === "duringWrite") { |
| 96 | + setOn(true); // a reader mounts mid-flight; nothing read the store before |
| 97 | + flush(); |
| 98 | + log.push("--mounted"); |
| 99 | + } |
| 100 | + if (at !== "during") { |
| 101 | + await tick(10); |
| 102 | + setS(3); // a plain write beside the flight |
| 103 | + flush(); |
| 104 | + log.push("--plain write"); |
| 105 | + } |
| 106 | + await tick(300); |
| 107 | + if (at === "afterLanding") { |
| 108 | + setOn(true); |
| 109 | + flush(); |
| 110 | + log.push("--mounted"); |
| 111 | + } |
| 112 | + await tick(300); |
| 113 | + return log; |
| 114 | +} |
| 115 | + |
| 116 | +describe("a reader mounted mid-flight reads a derived store like its memo (#4491)", () => { |
| 117 | + test("memo form: the mount is held until m1 answers (F-2)", async () => { |
| 118 | + expect(await run("memo", "during")).toEqual(["r:off", "--reask", "--mounted", "r:4"]); |
| 119 | + }); |
| 120 | + test("memo form, a plain write beside the flight: held with it, re-asked once", async () => { |
| 121 | + expect(await run("memo", "duringWrite")).toEqual([ |
| 122 | + "s:1", |
| 123 | + "r:off", |
| 124 | + "s:2", |
| 125 | + "--reask", |
| 126 | + "--mounted", |
| 127 | + "--plain write", |
| 128 | + "r:6", |
| 129 | + "s:3" |
| 130 | + ]); |
| 131 | + }); |
| 132 | + for (const form of storeForms) |
| 133 | + test(`${form}: held like the memo form, never the old question's r:2 (D-30)`, async () => { |
| 134 | + expect(await run(form, "during")).toEqual(await run("memo", "during")); |
| 135 | + // The write beside the flight shows no answer of a question between |
| 136 | + // the two (`r:5`) either: the memo form's screen, frame for frame. |
| 137 | + expect(await run(form, "duringWrite")).toEqual(await run("memo", "duringWrite")); |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +describe("readers the hold does not reach (#4491)", () => { |
| 142 | + test("memo form, reader on screen at the re-ask; reader mounted after the landing", async () => { |
| 143 | + expect(await run("memo", "before")).toEqual([ |
| 144 | + "s:1", |
| 145 | + "r:2", |
| 146 | + "--reask", |
| 147 | + "--plain write", |
| 148 | + "r:4", |
| 149 | + "s:3", |
| 150 | + "r:6" |
| 151 | + ]); |
| 152 | + expect(await run("memo", "afterLanding")).toEqual([ |
| 153 | + "s:1", |
| 154 | + "r:off", |
| 155 | + "s:2", |
| 156 | + "--reask", |
| 157 | + "s:3", |
| 158 | + "--plain write", |
| 159 | + "r:6", |
| 160 | + "--mounted" |
| 161 | + ]); |
| 162 | + }); |
| 163 | + for (const form of storeForms) |
| 164 | + test(`${form}: as the memo form`, async () => { |
| 165 | + expect(await run(form, "before")).toEqual(await run("memo", "before")); |
| 166 | + expect(await run(form, "afterLanding")).toEqual(await run("memo", "afterLanding")); |
| 167 | + }); |
| 168 | +}); |
0 commit comments