|
| 1 | +import { describe, expect, test } from "vitest"; |
| 2 | +import { |
| 3 | + createLoadingBoundary, |
| 4 | + createMemo, |
| 5 | + createRenderEffect, |
| 6 | + createRoot, |
| 7 | + createSignal, |
| 8 | + flush, |
| 9 | + isPending |
| 10 | +} from "../src/index.js"; |
| 11 | + |
| 12 | +const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms)); |
| 13 | + |
| 14 | +// #3528: a boundary whose `on` accessor consults `isPending(dep)` — directly or |
| 15 | +// through a memo — over two async memos. `on` is evaluated from `notify`, inside |
| 16 | +// the pending memo's own pass; the memo of `isPending(m2)` is marked pending by |
| 17 | +// that propagation, and reading it there (untracked, pending) recorded the |
| 18 | +// untracked-pending re-run link on `context` — which was `m2`. `m2` then |
| 19 | +// depended on a memo that depends on `m2`: every pending mark re-derived `m2` |
| 20 | +// (the A30 kept-tail rule), which went pending again, forever, in one flush. |
| 21 | +// A click must produce exactly one new flight of the written memo, with the |
| 22 | +// new input. |
| 23 | +type OnMode = "always" | "memo-isPending" | "fn-isPending"; |
| 24 | + |
| 25 | +function build(onMode: OnMode, boundaries: 1 | 2) { |
| 26 | + const [count, setCount] = createSignal(0); |
| 27 | + const [count2, setCount2] = createSignal(0); |
| 28 | + const runs: string[] = []; |
| 29 | + const log: string[] = []; |
| 30 | + let dispose!: () => void; |
| 31 | + createRoot(d => { |
| 32 | + dispose = d; |
| 33 | + const m2 = createMemo(async () => { |
| 34 | + const c = count(); |
| 35 | + runs.push(`m2(c=${c})`); |
| 36 | + if (runs.length > 40) throw new Error("loop guard"); |
| 37 | + await sleep(30); |
| 38 | + return c * 2; |
| 39 | + }); |
| 40 | + const m3 = createMemo(async () => { |
| 41 | + const c = count2(); |
| 42 | + runs.push(`m3(c=${c})`); |
| 43 | + if (runs.length > 40) throw new Error("loop guard"); |
| 44 | + await sleep(30); |
| 45 | + return c * 3; |
| 46 | + }); |
| 47 | + const make = (dep: () => unknown, tag: string) => { |
| 48 | + let token = {}; |
| 49 | + let lastOn: object | null = null; |
| 50 | + const shouldReset = |
| 51 | + onMode === "memo-isPending" |
| 52 | + ? createMemo(() => isPending(dep)) |
| 53 | + : onMode === "fn-isPending" |
| 54 | + ? () => isPending(dep) |
| 55 | + : () => true; |
| 56 | + const view = createLoadingBoundary( |
| 57 | + () => `${m2()} ${m3()}`, |
| 58 | + () => "Loading", |
| 59 | + { |
| 60 | + on() { |
| 61 | + if (onMode === "fn-isPending") return shouldReset() ? (lastOn = {}) : lastOn; |
| 62 | + if (shouldReset()) token = {}; |
| 63 | + return token; |
| 64 | + } |
| 65 | + } |
| 66 | + ); |
| 67 | + createRenderEffect(view, v => void log.push(`${tag}=${v}`)); |
| 68 | + }; |
| 69 | + make(m2, "A"); |
| 70 | + if (boundaries === 2) make(m3, "B"); |
| 71 | + createRenderEffect(count, v => void log.push(`count=${v}`)); |
| 72 | + }); |
| 73 | + flush(); |
| 74 | + return { |
| 75 | + runs, |
| 76 | + log, |
| 77 | + setCount, |
| 78 | + setCount2, |
| 79 | + dispose, |
| 80 | + async settle() { |
| 81 | + for (let t = 0; t < 12; t++) { |
| 82 | + await sleep(10); |
| 83 | + flush(); |
| 84 | + } |
| 85 | + }, |
| 86 | + mark(label: string) { |
| 87 | + log.push(label); |
| 88 | + runs.push(label); |
| 89 | + }, |
| 90 | + since(label: string) { |
| 91 | + return { |
| 92 | + log: log.slice(log.indexOf(label) + 1), |
| 93 | + runs: runs.slice(runs.indexOf(label) + 1) |
| 94 | + }; |
| 95 | + } |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +describe("#3528 isPending consulted from a Loading boundary's `on`", () => { |
| 100 | + test.each(["always", "fn-isPending", "memo-isPending"] as const)( |
| 101 | + "on=%s, one boundary: one click is one flight, and the boundary resets", |
| 102 | + async onMode => { |
| 103 | + const h = build(onMode, 1); |
| 104 | + await h.settle(); |
| 105 | + h.mark("--click--"); |
| 106 | + h.setCount(1); |
| 107 | + flush(); |
| 108 | + await h.settle(); |
| 109 | + h.dispose(); |
| 110 | + const r = h.since("--click--"); |
| 111 | + expect(r.runs).toEqual(["m2(c=1)"]); |
| 112 | + // The reset frees the boundary's reader from the hold (A33): the count |
| 113 | + // publishes with the fallback, and the content reveals when m2 lands. |
| 114 | + expect(r.log).toEqual(["count=1", "A=Loading", "A=2 0"]); |
| 115 | + } |
| 116 | + ); |
| 117 | + |
| 118 | + test.each(["fn-isPending", "memo-isPending"] as const)( |
| 119 | + "on=%s, two boundaries reading both memos: one click is one flight", |
| 120 | + async onMode => { |
| 121 | + const h = build(onMode, 2); |
| 122 | + await h.settle(); |
| 123 | + h.mark("--click--"); |
| 124 | + h.setCount(1); |
| 125 | + flush(); |
| 126 | + await h.settle(); |
| 127 | + h.dispose(); |
| 128 | + const r = h.since("--click--"); |
| 129 | + expect(r.runs).toEqual(["m2(c=1)"]); |
| 130 | + // B reads m2 too and is not keyed on it: an outside reader of the |
| 131 | + // flight, so the frame stays held until m2 lands (A33) — one reveal. |
| 132 | + expect(r.log).toHaveLength(3); |
| 133 | + expect(r.log).toEqual(expect.arrayContaining(["count=1", "A=2 0", "B=2 0"])); |
| 134 | + } |
| 135 | + ); |
| 136 | + |
| 137 | + test("the report's shape: two boundaries, both buttons clicked back to back", async () => { |
| 138 | + const h = build("memo-isPending", 2); |
| 139 | + await h.settle(); |
| 140 | + h.mark("--clicks--"); |
| 141 | + h.setCount(1); |
| 142 | + h.setCount2(1); |
| 143 | + flush(); |
| 144 | + await h.settle(); |
| 145 | + h.dispose(); |
| 146 | + const r = h.since("--clicks--"); |
| 147 | + expect(r.runs.sort()).toEqual(["m2(c=1)", "m3(c=1)"]); |
| 148 | + expect(r.log.filter(l => l.startsWith("A=")).at(-1)).toBe("A=2 3"); |
| 149 | + expect(r.log.filter(l => l.startsWith("B=")).at(-1)).toBe("B=2 3"); |
| 150 | + }); |
| 151 | +}); |
0 commit comments