|
| 1 | +/** |
| 2 | + * #4504: a memo between a render effect and an async memo in flight, read for |
| 3 | + * the first time by a mount made mid-flight. |
| 4 | + * |
| 5 | + * `m0` is the async memo's input; `m1 = createMemo(async () => m0())` takes |
| 6 | + * 100 ms. A plain write re-asks `m1` while nothing reads it, and 20 ms into |
| 7 | + * the flight a render effect mounts and reads `mb`, a memo over `m1` |
| 8 | + * (`() => m1()`, `() => m1() * 10`) that nothing has read before. |
| 9 | + * |
| 10 | + * With `m0 = createMemo(() => latest(s) + s())` (a verdict reader: it called |
| 11 | + * `latest`) `m1` and `mb` carried that posture by what they read (D-25: a |
| 12 | + * memo's posture is what it reads). The mount's pass of `mb` read `m1` |
| 13 | + * plainly — `mb` was pending, so the pass is not a verdict reader (D-25, |
| 14 | + * #3884) — and threw, so the reader held the mount (D-2, F-2). But the read |
| 15 | + * of the pending `m1` still stamped the posture on `mb` for its next pass, |
| 16 | + * so at the landing `mb` re-ran as a verdict reader and was served `m1`'s |
| 17 | + * committed value (the screen): `r:2` published at the landing, then `r:4` |
| 18 | + * at the commit — a held reader shown a value older than the one it waited |
| 19 | + * for (F-2, F-1). |
| 20 | + * |
| 21 | + * Ruled: a plain read in a mount is not display-ahead (D-27); the reader of |
| 22 | + * a derivation with no value to show for the re-ask is born held (D-2) and |
| 23 | + * shows the re-ask's answer (D-22, F-9) — the screen of the direct read |
| 24 | + * (`r:4`) and of a plain input. A pending read yields no value, so it hands |
| 25 | + * on no posture (D-25: a node whose flight is up is the hold's work, not a |
| 26 | + * verdict reader). A memo that is a verdict reader already keeps its |
| 27 | + * posture when it is served the screen of a flight: a prior on-screen |
| 28 | + * reader of `mb` sees what it saw before this fix. |
| 29 | + */ |
| 30 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 31 | +import { |
| 32 | + createEffect, |
| 33 | + createMemo, |
| 34 | + createRenderEffect, |
| 35 | + createRoot, |
| 36 | + createSignal, |
| 37 | + flush, |
| 38 | + isPending, |
| 39 | + latest, |
| 40 | + resetErrorHalt |
| 41 | +} from "../src/index.js"; |
| 42 | + |
| 43 | +beforeEach(() => vi.useFakeTimers()); |
| 44 | +afterEach(() => { |
| 45 | + vi.useRealTimers(); |
| 46 | + resetErrorHalt(); |
| 47 | +}); |
| 48 | + |
| 49 | +const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms)); |
| 50 | +const tick = async (ms: number) => { |
| 51 | + await vi.advanceTimersByTimeAsync(ms); |
| 52 | + flush(); |
| 53 | +}; |
| 54 | + |
| 55 | +type Input = "verdict" | "plain"; |
| 56 | +type Between = "identity" | "times10" | "chain"; |
| 57 | +type Made = "before" | "mid-flight"; |
| 58 | +type Prior = "none" | "render effect" | "user effect"; |
| 59 | + |
| 60 | +const between: Record<Between, (m1: () => number) => () => number> = { |
| 61 | + identity: m1 => createMemo(() => m1()), |
| 62 | + times10: m1 => createMemo(() => m1() * 10), |
| 63 | + chain: m1 => { |
| 64 | + const inner = createMemo(() => m1() * 10); |
| 65 | + return createMemo(() => inner() + 1); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +async function run(input: Input, shape: Between, made: Made, prior: Prior) { |
| 70 | + const log: string[] = []; |
| 71 | + const [s, setS] = createSignal(1); |
| 72 | + const [on, setOn] = createSignal(prior === "render effect"); |
| 73 | + let m1!: () => number; |
| 74 | + let mb!: () => number; |
| 75 | + createRoot(() => { |
| 76 | + const m0 = createMemo(() => (input === "plain" ? s() * 2 : latest(s) + s())); |
| 77 | + m1 = createMemo(async () => { |
| 78 | + const v = m0(); |
| 79 | + await sleep(100); |
| 80 | + return v; |
| 81 | + }); |
| 82 | + if (made === "before") mb = between[shape](m1); |
| 83 | + }); |
| 84 | + if (prior === "user effect") |
| 85 | + createRoot(() => |
| 86 | + createEffect( |
| 87 | + () => mb(), |
| 88 | + () => {} |
| 89 | + ) |
| 90 | + ); |
| 91 | + createRoot(() => |
| 92 | + createRenderEffect( |
| 93 | + () => (on() ? `r:${mb()}` : "r:off"), |
| 94 | + v => void log.push(v) |
| 95 | + ) |
| 96 | + ); |
| 97 | + flush(); |
| 98 | + await tick(150); |
| 99 | + setS(2); |
| 100 | + flush(); |
| 101 | + await tick(20); |
| 102 | + if (made === "mid-flight") createRoot(() => (mb = between[shape](m1))); |
| 103 | + setOn(true); |
| 104 | + flush(); |
| 105 | + log.push("--mounted"); |
| 106 | + await tick(300); |
| 107 | + return log; |
| 108 | +} |
| 109 | + |
| 110 | +const screen = (shape: Between, v: number) => |
| 111 | + `r:${shape === "identity" ? v : shape === "times10" ? v * 10 : v * 10 + 1}`; |
| 112 | + |
| 113 | +describe("#4504: a memo over a re-asked async memo, first read by a mount mid-flight", () => { |
| 114 | + it("control: the mount reading the async memo directly is held and shows the re-ask's answer", async () => { |
| 115 | + const log: string[] = []; |
| 116 | + const [s, setS] = createSignal(1); |
| 117 | + const [on, setOn] = createSignal(false); |
| 118 | + createRoot(() => { |
| 119 | + const m0 = createMemo(() => latest(s) + s()); |
| 120 | + const m1 = createMemo(async () => { |
| 121 | + const v = m0(); |
| 122 | + await sleep(100); |
| 123 | + return v; |
| 124 | + }); |
| 125 | + createRenderEffect( |
| 126 | + () => (on() ? `r:${m1()}` : "r:off"), |
| 127 | + v => void log.push(v) |
| 128 | + ); |
| 129 | + }); |
| 130 | + flush(); |
| 131 | + await tick(150); |
| 132 | + setS(2); |
| 133 | + flush(); |
| 134 | + await tick(20); |
| 135 | + setOn(true); |
| 136 | + flush(); |
| 137 | + log.push("--mounted"); |
| 138 | + await tick(300); |
| 139 | + expect(log).toEqual(["r:off", "--mounted", "r:4"]); |
| 140 | + }); |
| 141 | + |
| 142 | + for (const input of ["verdict", "plain"] as const) |
| 143 | + for (const shape of ["identity", "times10", "chain"] as const) |
| 144 | + for (const made of ["before", "mid-flight"] as const) |
| 145 | + it(`${input} input, ${shape} memo made ${made === "before" ? "before the re-ask" : "mid-flight"}: the reader is born held and shows the re-ask's answer`, async () => { |
| 146 | + expect(await run(input, shape, made, "none")).toEqual([ |
| 147 | + "r:off", |
| 148 | + "--mounted", |
| 149 | + screen(shape, 4) |
| 150 | + ]); |
| 151 | + }); |
| 152 | + |
| 153 | + // #4474's equivalence: a reader that holds nothing does not change the |
| 154 | + // screen (laziness is never observable, D-22). |
| 155 | + for (const input of ["verdict", "plain"] as const) |
| 156 | + it(`${input} input, a prior user effect reading the memo: the same screen as with no reader`, async () => { |
| 157 | + expect(await run(input, "times10", "before", "user effect")).toEqual([ |
| 158 | + "r:off", |
| 159 | + "--mounted", |
| 160 | + "r:40" |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + // Unchanged by #4504: the memo already had a value and an on-screen reader |
| 165 | + // when the write re-asked the async memo. With the verdict input that |
| 166 | + // reader shows the screen of the flight (D-25: the memo is a verdict |
| 167 | + // reader by what it reads), the verdict lane's reveal of `latest(s) + s()` |
| 168 | + // over the held write (`3`, D-26), then the commit. |
| 169 | + it("verdict input, the memo already on screen before the re-ask: unchanged", async () => { |
| 170 | + expect(await run("verdict", "times10", "before", "render effect")).toEqual([ |
| 171 | + "r:20", |
| 172 | + "--mounted", |
| 173 | + "r:30", |
| 174 | + "r:40" |
| 175 | + ]); |
| 176 | + expect(await run("verdict", "identity", "before", "render effect")).toEqual([ |
| 177 | + "r:2", |
| 178 | + "--mounted", |
| 179 | + "r:3", |
| 180 | + "r:4" |
| 181 | + ]); |
| 182 | + }); |
| 183 | + it("plain input, the memo already on screen before the re-ask: unchanged", async () => { |
| 184 | + expect(await run("plain", "times10", "before", "render effect")).toEqual([ |
| 185 | + "r:20", |
| 186 | + "--mounted", |
| 187 | + "r:40" |
| 188 | + ]); |
| 189 | + }); |
| 190 | + |
| 191 | + // D-22 / D-23: the memo has produced a value (the async memo's landed |
| 192 | + // answer, through it) and is pending on the re-ask, at any depth — read |
| 193 | + // unowned after the plain mount, and by a verdict leaf mounted mid-flight. |
| 194 | + for (const input of ["verdict", "plain"] as const) |
| 195 | + for (const mount of ["plain reader", "verdict leaf"] as const) |
| 196 | + it(`${input} input, ${mount} mounted mid-flight: latest/isPending of the memo`, async () => { |
| 197 | + const log: string[] = []; |
| 198 | + const [s, setS] = createSignal(1); |
| 199 | + const [on, setOn] = createSignal(false); |
| 200 | + let mb!: () => number; |
| 201 | + createRoot(() => { |
| 202 | + const m0 = createMemo(() => (input === "plain" ? s() * 2 : latest(s) + s())); |
| 203 | + const m1 = createMemo(async () => { |
| 204 | + const v = m0(); |
| 205 | + await sleep(100); |
| 206 | + return v; |
| 207 | + }); |
| 208 | + mb = createMemo(() => m1() * 10); |
| 209 | + createRenderEffect( |
| 210 | + () => |
| 211 | + !on() |
| 212 | + ? "off" |
| 213 | + : mount === "plain reader" |
| 214 | + ? `r:${mb()}` |
| 215 | + : `l:${latest(mb)} p:${isPending(mb)}`, |
| 216 | + v => void log.push(v) |
| 217 | + ); |
| 218 | + }); |
| 219 | + flush(); |
| 220 | + await tick(150); |
| 221 | + setS(2); |
| 222 | + flush(); |
| 223 | + await tick(20); |
| 224 | + setOn(true); |
| 225 | + flush(); |
| 226 | + log.push("--mounted"); |
| 227 | + expect([latest(mb), isPending(mb)]).toEqual([20, true]); |
| 228 | + await tick(300); |
| 229 | + expect([latest(mb), isPending(mb)]).toEqual([40, false]); |
| 230 | + expect(log).toEqual( |
| 231 | + mount === "plain reader" |
| 232 | + ? ["off", "--mounted", "r:40"] |
| 233 | + : ["off", "l:20 p:true", "--mounted", "l:40 p:false"] |
| 234 | + ); |
| 235 | + }); |
| 236 | +}); |
0 commit comments