|
| 1 | +/** |
| 2 | + * #4429 — a memo's pass pulled inside a verdict window is its own (A31), |
| 3 | + * whoever woke it. |
| 4 | + * |
| 5 | + * `verdictValue` pulls a dirty memo with the dispatch closed |
| 6 | + * (`setVerdict(null)`), so the memo's plain reads are its own. It left the |
| 7 | + * probe's state set, though: `probing` stayed on for the pulled pass, so a |
| 8 | + * `latest()` the memo calls itself re-entered the window as the outer probe: |
| 9 | + * |
| 10 | + * - `latest()` of a node born held (no committed value, staged) took the |
| 11 | + * probe's `joinPass` arm: the pulled memo joined the hold, and a memo it |
| 12 | + * created during that pass was born held (D-27) — where the same pass |
| 13 | + * pulled by a plain read leaves it mainline (F-vworld: a verdict read |
| 14 | + * raises no world); |
| 15 | + * - `probeFound` was set from inside the pulled pass (a flight its |
| 16 | + * `latest()` read), so the outer `isPending` answered for the memo's |
| 17 | + * reads rather than for the memo. |
| 18 | + * |
| 19 | + * Each case compares the pull inside the window with the same pass woken by |
| 20 | + * a plain read: the memo's pass, its value and what it creates are the same. |
| 21 | + */ |
| 22 | +import { describe, expect, it } from "vitest"; |
| 23 | +import { |
| 24 | + action, |
| 25 | + createMemo, |
| 26 | + createRenderEffect, |
| 27 | + createRoot, |
| 28 | + createSignal, |
| 29 | + flush, |
| 30 | + isPending, |
| 31 | + latest, |
| 32 | + untrack |
| 33 | +} from "../src/index.js"; |
| 34 | + |
| 35 | +const peek = (fn: () => unknown) => { |
| 36 | + try { |
| 37 | + return untrack(fn); |
| 38 | + } catch (e) { |
| 39 | + return (e as Error).constructor.name; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +const settle = async () => { |
| 44 | + await Promise.resolve(); |
| 45 | + await Promise.resolve(); |
| 46 | + flush(); |
| 47 | +}; |
| 48 | + |
| 49 | +describe("#4429: a memo pulled inside a verdict window computes as its own pass", () => { |
| 50 | + for (const pull of ["isPending", "plain"] as const) |
| 51 | + it(`latest() of a born-held node in the pulled pass joins nothing (pulled ${pull})`, async () => { |
| 52 | + const [x, setX] = createSignal(0); |
| 53 | + const [s, setS] = createSignal(0); |
| 54 | + const [on, setOn] = createSignal(false); |
| 55 | + let born: (() => number) | null = null; |
| 56 | + let child!: () => number; |
| 57 | + const m1 = createRoot(() => { |
| 58 | + createRenderEffect(x, () => {}); |
| 59 | + // Deeper than the reader below: the reader's pass pulls it. |
| 60 | + const sa = createMemo(() => s()); |
| 61 | + const m1 = createMemo(() => { |
| 62 | + const v = sa(); |
| 63 | + const l = born ? latest(born) : -1; |
| 64 | + // Reads nothing held: born held only if its creator joined. |
| 65 | + child = createMemo(() => v * 100); |
| 66 | + return l * 10 + v; |
| 67 | + }); |
| 68 | + createRenderEffect(m1, () => {}); |
| 69 | + createRenderEffect( |
| 70 | + () => on() && (pull === "isPending" ? isPending(() => m1()) : m1()), |
| 71 | + () => {} |
| 72 | + ); |
| 73 | + return m1; |
| 74 | + }); |
| 75 | + flush(); |
| 76 | + let release!: () => void; |
| 77 | + action(function* () { |
| 78 | + setX(1); |
| 79 | + yield new Promise<void>(r => (release = r)); |
| 80 | + })(); |
| 81 | + flush(); |
| 82 | + |
| 83 | + // Born held: no committed value, its staging (1) its only value. |
| 84 | + createRoot(() => { |
| 85 | + born = createMemo(() => x()); |
| 86 | + }); |
| 87 | + expect(peek(born!)).toBe("NotReadyError"); |
| 88 | + setS(1); |
| 89 | + setOn(true); |
| 90 | + flush(); |
| 91 | + // The pulled pass read `latest(born)` = 1 (D-22) and made its child |
| 92 | + // from the screen's s: mainline — not the hold's. |
| 93 | + expect(peek(m1)).toBe(11); |
| 94 | + expect(peek(child)).toBe(100); |
| 95 | + release(); |
| 96 | + await settle(); |
| 97 | + expect(peek(m1)).toBe(11); |
| 98 | + expect(peek(child)).toBe(100); |
| 99 | + }); |
| 100 | + |
| 101 | + for (const pull of ["isPending", "plain"] as const) |
| 102 | + it(`a flight the pulled pass's latest() reads is not the probe's answer (pulled ${pull})`, async () => { |
| 103 | + const [q, setQ] = createSignal(1); |
| 104 | + const [s, setS] = createSignal(0); |
| 105 | + const [on, setOn] = createSignal(false); |
| 106 | + let resolve!: () => void; |
| 107 | + const seen: unknown[] = []; |
| 108 | + const m1 = createRoot(() => { |
| 109 | + const a = createMemo(async () => { |
| 110 | + const v = q(); |
| 111 | + if (v > 1) await new Promise<void>(r => (resolve = r)); |
| 112 | + return v; |
| 113 | + }); |
| 114 | + createRenderEffect(a, () => {}); |
| 115 | + const sa = createMemo(() => s()); |
| 116 | + const m1 = createMemo(() => sa() + latest(a) * 10); |
| 117 | + createRenderEffect(m1, () => {}); |
| 118 | + createRenderEffect( |
| 119 | + () => on() && (pull === "isPending" ? isPending(() => m1()) : (m1(), isPending(m1))), |
| 120 | + v => { |
| 121 | + seen.push(v); |
| 122 | + } |
| 123 | + ); |
| 124 | + return m1; |
| 125 | + }); |
| 126 | + await settle(); |
| 127 | + expect(peek(m1)).toBe(10); |
| 128 | + setQ(2); |
| 129 | + flush(); |
| 130 | + // a is in flight over its committed 1. |
| 131 | + setS(1); |
| 132 | + setOn(true); |
| 133 | + flush(); |
| 134 | + // m1 is a settled sync memo of `latest(a)` — the committed 1 while the |
| 135 | + // flight is up: nothing m1 shows is waiting on it. |
| 136 | + expect(peek(m1)).toBe(11); |
| 137 | + expect(seen).toEqual([false, false]); |
| 138 | + resolve(); |
| 139 | + await settle(); |
| 140 | + expect(peek(m1)).toBe(21); |
| 141 | + }); |
| 142 | +}); |
0 commit comments