|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { NOT_PENDING } from "../src/core/constants.js"; |
| 3 | +import { $TARGET } from "../src/store/store.js"; |
| 4 | +import { |
| 5 | + DEV, |
| 6 | + createErrorBoundary, |
| 7 | + createLoadingBoundary, |
| 8 | + createMemo, |
| 9 | + createRenderEffect, |
| 10 | + createRoot, |
| 11 | + createSignal, |
| 12 | + createStore, |
| 13 | + flush, |
| 14 | + isPending, |
| 15 | + latest |
| 16 | +} from "../src/index.js"; |
| 17 | + |
| 18 | +// async-gen campaign 1 (seed 3289), E3 store twin, minimized case 34: |
| 19 | +// [s0=0 m0=async(s0+s0) m1=memo(latest(m0)+s0 !%4=1)] |
| 20 | +// <Show when=isPending(m0) fb><span split[m1()]></span></Show> :: s0=1 |
| 21 | +// A derived store's first landing is its first answer: loading before it, |
| 22 | +// final at it — never pending (F-9, D-30, A19 exc. 1), exactly as for the |
| 23 | +// memo it copies. A sibling derive reading `latest(m0)` gives the landing |
| 24 | +// flush a transaction; the leaf born staged from the landing then answered |
| 25 | +// `isPending` true for one pass, which opened the gate onto a reader of the |
| 26 | +// poisoned `m1`, whose error reached the root boundary. |
| 27 | +describe("a derived store's first landing is not pending", () => { |
| 28 | + const settle = async () => { |
| 29 | + for (let i = 0; i < 4; i++) { |
| 30 | + for (let j = 0; j < 6; j++) await Promise.resolve(); |
| 31 | + flush(); |
| 32 | + await new Promise(r => setTimeout(r, 0)); |
| 33 | + } |
| 34 | + flush(); |
| 35 | + }; |
| 36 | + |
| 37 | + afterEach(() => { |
| 38 | + DEV!.hooks.onRead = undefined; |
| 39 | + }); |
| 40 | + |
| 41 | + // `stagedLeafReads` (dev): counts devtools reads of store 0's leaf `v` |
| 42 | + // while it holds a staging — the first landing's reads, which `nodeValue` |
| 43 | + // serves without core `read`. |
| 44 | + async function run(via: "memo" | "store", gated: boolean, stagedLeafReads?: { n: number }) { |
| 45 | + const log: string[] = []; |
| 46 | + const flights: (() => void)[] = []; |
| 47 | + let set!: (v: number) => void; |
| 48 | + let dispose!: () => void; |
| 49 | + createRoot(d => { |
| 50 | + dispose = d; |
| 51 | + const [s, setS] = createSignal(0); |
| 52 | + set = setS; |
| 53 | + const m0 = createMemo(() => { |
| 54 | + const v = 1 + 2 * s(); |
| 55 | + return new Promise<number>(r => flights.push(() => r(v))); |
| 56 | + }); |
| 57 | + const m1 = createMemo(() => { |
| 58 | + const v = 1 + latest(m0) + s(); |
| 59 | + if (v % 4 === 1) throw new Error("m1:" + v); |
| 60 | + return v; |
| 61 | + }); |
| 62 | + let view0: () => any = m0; |
| 63 | + let view1: () => any = m1; |
| 64 | + if (via === "store") { |
| 65 | + const [st0] = createStore<{ v?: number }>(draft => { |
| 66 | + draft.v = m0(); |
| 67 | + }, {}); |
| 68 | + const [st1] = createStore<{ v?: number }>(draft => { |
| 69 | + draft.v = m1(); |
| 70 | + }, {}); |
| 71 | + view0 = () => st0.v; |
| 72 | + view1 = () => st1.v; |
| 73 | + if (stagedLeafReads) |
| 74 | + DEV!.hooks.onRead = n => { |
| 75 | + const leaf = (st0 as any)[$TARGET].n?.v; |
| 76 | + if (n === leaf && leaf._pendingValue !== NOT_PENDING) stagedLeafReads.n++; |
| 77 | + }; |
| 78 | + } |
| 79 | + const out = createErrorBoundary( |
| 80 | + () => |
| 81 | + createLoadingBoundary( |
| 82 | + () => { |
| 83 | + const when = createMemo(() => isPending(view0)); |
| 84 | + createRenderEffect(when, v => { |
| 85 | + log.push(`pending:${v}`); |
| 86 | + }); |
| 87 | + if (gated) |
| 88 | + createRenderEffect( |
| 89 | + () => (when() ? view1() : "-"), |
| 90 | + v => { |
| 91 | + log.push(`child:${v}`); |
| 92 | + } |
| 93 | + ); |
| 94 | + return "content"; |
| 95 | + }, |
| 96 | + () => "loading" |
| 97 | + ), |
| 98 | + () => "error" |
| 99 | + ); |
| 100 | + createRenderEffect(out, v => { |
| 101 | + log.push(String(v)); |
| 102 | + }); |
| 103 | + }); |
| 104 | + flush(); |
| 105 | + set(1); |
| 106 | + await settle(); |
| 107 | + while (flights.length) { |
| 108 | + flights.splice(0, 1).forEach(f => f()); |
| 109 | + await settle(); |
| 110 | + } |
| 111 | + dispose(); |
| 112 | + return log; |
| 113 | + } |
| 114 | + |
| 115 | + it("isPending of the leaf never reads true at the first landing", async () => { |
| 116 | + const memo = await run("memo", false); |
| 117 | + expect(memo).toEqual(["loading", "pending:false", "content"]); |
| 118 | + expect(await run("store", false)).toEqual(memo); |
| 119 | + }); |
| 120 | + |
| 121 | + it("a reader gated on that verdict never opens onto the sibling's error", async () => { |
| 122 | + const memo = await run("memo", true); |
| 123 | + expect(memo).toEqual(["loading", "pending:false", "child:-", "content"]); |
| 124 | + expect(await run("store", true)).toEqual(memo); |
| 125 | + }); |
| 126 | + |
| 127 | + it("dev: a staged first-landing read of the leaf still reaches the read hook", async () => { |
| 128 | + const stagedLeafReads = { n: 0 }; |
| 129 | + expect(await run("store", true, stagedLeafReads)).toEqual([ |
| 130 | + "loading", |
| 131 | + "pending:false", |
| 132 | + "child:-", |
| 133 | + "content" |
| 134 | + ]); |
| 135 | + expect(stagedLeafReads.n).toBeGreaterThan(0); |
| 136 | + }); |
| 137 | +}); |
0 commit comments