|
| 1 | +import { expect, test } from "vitest"; |
| 2 | +import { |
| 3 | + action, |
| 4 | + createErrorBoundary, |
| 5 | + createMemo, |
| 6 | + createOptimisticStore, |
| 7 | + createRenderEffect, |
| 8 | + createRoot, |
| 9 | + createSignal, |
| 10 | + flush, |
| 11 | + refresh |
| 12 | +} from "../src/index.js"; |
| 13 | + |
| 14 | +const tick = async () => { |
| 15 | + for (let i = 0; i < 4; i++) { |
| 16 | + await new Promise(r => setTimeout(r, 0)); |
| 17 | + flush(); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +test("F-1 in the #4577 shape: one memo's readers agree, a memo is whole, the screen settles as one", async () => { |
| 22 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 23 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 24 | + const settle = async (key: string, v: any) => { |
| 25 | + const i = asks.findIndex(a => a.key === key); |
| 26 | + asks.splice(i, 1)[0].res(v); |
| 27 | + await tick(); |
| 28 | + }; |
| 29 | + const frames: string[] = []; |
| 30 | + let f = "", |
| 31 | + a = "", |
| 32 | + b = "", |
| 33 | + s = "", |
| 34 | + o = ""; |
| 35 | + let q = false; |
| 36 | + const snap = () => { |
| 37 | + if (!q) { |
| 38 | + q = true; |
| 39 | + queueMicrotask(() => { |
| 40 | + q = false; |
| 41 | + frames.push(`${f}|${a}|${b}|${s}|${o}`); |
| 42 | + }); |
| 43 | + } |
| 44 | + }; |
| 45 | + let setFilter!: (v: string) => void, closeOne!: (id: number) => unknown, dispose!: () => void; |
| 46 | + createRoot(d => { |
| 47 | + dispose = d; |
| 48 | + const [filter, sf] = createSignal("open"); |
| 49 | + setFilter = sf; |
| 50 | + const [issues, set] = createOptimisticStore<{ id: number }[]>( |
| 51 | + () => ask(`issues:${filter()}`), |
| 52 | + [], |
| 53 | + { key: "id" } |
| 54 | + ); |
| 55 | + const other = createMemo(() => (issues.length, ask(`other:${filter()}`))); |
| 56 | + closeOne = action(function* (id: number) { |
| 57 | + set(l => { |
| 58 | + const i = l.findIndex(r => r.id === id); |
| 59 | + if (i >= 0) l.splice(i, 1); |
| 60 | + }); |
| 61 | + yield ask(`close:${id}`); |
| 62 | + refresh(issues); |
| 63 | + }); |
| 64 | + const list = createMemo(() => issues.map(r => `[#${r.id}]`).join("")); |
| 65 | + const shared = createMemo(() => `${filter()}:${issues.map(r => `[#${r.id}]`).join("")}`); |
| 66 | + createRenderEffect(filter, v => { |
| 67 | + f = v; |
| 68 | + snap(); |
| 69 | + }); |
| 70 | + createRenderEffect( |
| 71 | + () => list(), |
| 72 | + v => { |
| 73 | + a = v; |
| 74 | + snap(); |
| 75 | + } |
| 76 | + ); |
| 77 | + createRenderEffect( |
| 78 | + () => list(), |
| 79 | + v => { |
| 80 | + b = v; |
| 81 | + snap(); |
| 82 | + } |
| 83 | + ); |
| 84 | + createRenderEffect(shared, v => { |
| 85 | + s = v; |
| 86 | + snap(); |
| 87 | + }); |
| 88 | + createRenderEffect( |
| 89 | + createErrorBoundary( |
| 90 | + () => other(), |
| 91 | + () => "ERR" |
| 92 | + ) as any, |
| 93 | + (v: any) => { |
| 94 | + o = v; |
| 95 | + snap(); |
| 96 | + } |
| 97 | + ); |
| 98 | + }); |
| 99 | + await tick(); |
| 100 | + await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]); |
| 101 | + await settle("other:open", "O-open"); |
| 102 | + setFilter("closed"); |
| 103 | + await tick(); |
| 104 | + closeOne(1); |
| 105 | + await tick(); |
| 106 | + await settle("issues:closed", [{ id: 1 }, { id: 2 }]); |
| 107 | + await settle("other:closed", "O-closed"); |
| 108 | + await settle("close:1", {}); |
| 109 | + await settle("issues:closed", [{ id: 2 }]); |
| 110 | + for (let k = 0; k < 10 && asks.length; k++) { |
| 111 | + const x = asks.shift()!; |
| 112 | + x.key.startsWith("other") |
| 113 | + ? x.res("O-" + x.key.split(":")[1]) |
| 114 | + : x.key.startsWith("issues:closed") |
| 115 | + ? x.res([{ id: 2 }]) |
| 116 | + : x.res({}); |
| 117 | + await tick(); |
| 118 | + } |
| 119 | + for (let i = 0; i < 5; i++) await tick(); |
| 120 | + for (const fr of frames) { |
| 121 | + const [F, A, B, S, O] = fr.split("|"); |
| 122 | + if (!A) continue; |
| 123 | + expect(A, fr).toBe(B); |
| 124 | + expect(S.split(":")[0], fr).toBe(F); |
| 125 | + expect(O === "O-closed", fr).toBe(F === "closed"); |
| 126 | + } |
| 127 | + dispose(); |
| 128 | +}); |
0 commit comments