|
| 1 | +/** |
| 2 | + * #4648 (review of #4667). A pass that is the hold's own work (D-25: it reads |
| 3 | + * the hold's flight) re-runs in the flush that lands it. Its untracked probe |
| 4 | + * of the held write then answers for the landed world: the hold lands at |
| 5 | + * this seam, so the probe reads the staging and the pass is final in the |
| 6 | + * landing frame. The render effect and the user effect both show |
| 7 | + * `{0,0,false}` then `{5,5,false}` — never the stale pass first (F-1). |
| 8 | + * |
| 9 | + * If a node computed later in that flush blocks the hold again (a new |
| 10 | + * flight starts at the landing), the hold is up again: the pass re-runs on |
| 11 | + * the committed value — `inner: 0` while held (ruling on #4662) — and the |
| 12 | + * screen keeps the old frame until the hold that remains lands, with no |
| 13 | + * value shown and then retracted, and no `p: true` beside `v: 5` (D-23). |
| 14 | + * |
| 15 | + * Every case runs in both read orders. |
| 16 | + */ |
| 17 | +import { describe, expect, it } from "vitest"; |
| 18 | +import { |
| 19 | + createEffect, |
| 20 | + createMemo, |
| 21 | + createRenderEffect, |
| 22 | + createSignal, |
| 23 | + flush, |
| 24 | + isPending, |
| 25 | + untrack |
| 26 | +} from "../src/index.js"; |
| 27 | + |
| 28 | +type Row = { v: number; inner: unknown; p: unknown }; |
| 29 | +type Order = "probe after read" | "probe before read"; |
| 30 | +const orders: Order[] = ["probe after read", "probe before read"]; |
| 31 | + |
| 32 | +const tick = async () => { |
| 33 | + for (let i = 0; i < 4; i++) { |
| 34 | + await new Promise(r => setTimeout(r)); |
| 35 | + flush(); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +const initial = { v: 0, inner: 0, p: false }; |
| 40 | +const landed = { v: 5, inner: 5, p: false }; |
| 41 | + |
| 42 | +function probePass(order: Order, cond: () => number, after: () => void, log: Row[]) { |
| 43 | + return createMemo(() => { |
| 44 | + let v = 0, |
| 45 | + inner: unknown, |
| 46 | + p: unknown; |
| 47 | + const probeIt = () => (p = untrack(() => isPending(() => (inner = cond())))); |
| 48 | + if (order === "probe before read") probeIt(); |
| 49 | + v = cond(); |
| 50 | + if (order === "probe after read") probeIt(); |
| 51 | + after(); |
| 52 | + const r = { v, inner, p }; |
| 53 | + log.push(r); |
| 54 | + return r; |
| 55 | + }); |
| 56 | +} |
| 57 | + |
| 58 | +describe.each(orders)("a pass that reads the hold's flight (%s)", order => { |
| 59 | + for (const hold of [false, true]) |
| 60 | + it(`lands in one frame (${hold ? "held by its own flight" : "no hold"})`, async () => { |
| 61 | + const screen: Row[] = []; |
| 62 | + const effects: Row[] = []; |
| 63 | + const log: Row[] = []; |
| 64 | + let res!: (v: number) => void; |
| 65 | + const [open, setOpen] = createSignal(0); |
| 66 | + const cond = createMemo(() => open()); |
| 67 | + const a = createMemo(async () => { |
| 68 | + const v = open(); |
| 69 | + if (v && hold) await new Promise<number>(r => (res = r)); |
| 70 | + return v; |
| 71 | + }); |
| 72 | + const pass = probePass(order, cond, () => void (hold && a()), log); |
| 73 | + createRenderEffect(pass, r => void screen.push(r)); |
| 74 | + createEffect(pass, r => void effects.push(r)); |
| 75 | + flush(); |
| 76 | + await tick(); |
| 77 | + setOpen(5); |
| 78 | + flush(); |
| 79 | + if (hold) { |
| 80 | + expect(screen).toEqual([initial]); |
| 81 | + res(5); |
| 82 | + } |
| 83 | + await tick(); |
| 84 | + expect(screen).toEqual([initial, landed]); |
| 85 | + expect(effects).toEqual([initial, landed]); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe.each(["same pass", "sibling", "deep sibling"] as const)( |
| 90 | + "a later node blocks the hold again in the landing flush (%s)", |
| 91 | + where => { |
| 92 | + describe.each(orders)("%s", order => { |
| 93 | + it("keeps the old frame, reads the committed value while held, and lands once", async () => { |
| 94 | + const screen: Row[] = []; |
| 95 | + const effects: Row[] = []; |
| 96 | + const log: Row[] = []; |
| 97 | + let resA!: (v: number) => void, resB!: (v: number) => void; |
| 98 | + const [open, setOpen] = createSignal(0); |
| 99 | + const cond = createMemo(() => open()); |
| 100 | + const a = createMemo(async () => { |
| 101 | + const v = open(); |
| 102 | + if (v) await new Promise<number>(r => (resA = r)); |
| 103 | + return v; |
| 104 | + }); |
| 105 | + // `deep sibling`: computed after the pass in the landing flush, so |
| 106 | + // the pass's probe ran while the hold was not yet blocked again. |
| 107 | + const m1 = createMemo(() => a()); |
| 108 | + const m2 = createMemo(() => m1()); |
| 109 | + const m3 = createMemo(() => m2()); |
| 110 | + const b = createMemo(async () => { |
| 111 | + const v = where === "deep sibling" ? m3() : a(); |
| 112 | + if (v) await new Promise<number>(r => (resB = r)); |
| 113 | + return v; |
| 114 | + }); |
| 115 | + const pass = probePass( |
| 116 | + order, |
| 117 | + cond, |
| 118 | + () => { |
| 119 | + a(); |
| 120 | + if (where === "same pass") b(); |
| 121 | + }, |
| 122 | + log |
| 123 | + ); |
| 124 | + createRenderEffect(pass, r => void screen.push(r)); |
| 125 | + createEffect(pass, r => void effects.push(r)); |
| 126 | + if (where !== "same pass") createRenderEffect(b, () => {}); |
| 127 | + flush(); |
| 128 | + await tick(); |
| 129 | + expect(screen).toEqual([initial]); |
| 130 | + log.length = 0; |
| 131 | + setOpen(5); |
| 132 | + flush(); |
| 133 | + resA(5); |
| 134 | + await tick(); |
| 135 | + // `b`'s new flight holds the change: nothing shown, and the held |
| 136 | + // pass's last run read the committed value (D-23: final, `p: false`). |
| 137 | + expect(screen).toEqual([initial]); |
| 138 | + expect(effects).toEqual([initial]); |
| 139 | + if (log.length) expect(log.at(-1)).toEqual({ v: 5, inner: 0, p: false }); |
| 140 | + resB(5); |
| 141 | + await tick(); |
| 142 | + expect(screen).toEqual([initial, landed]); |
| 143 | + expect(effects).toEqual([initial, landed]); |
| 144 | + }); |
| 145 | + }); |
| 146 | + } |
| 147 | +); |
0 commit comments