|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + action, |
| 4 | + createEffect, |
| 5 | + createLoadingBoundary, |
| 6 | + createOptimisticStore, |
| 7 | + createRenderEffect, |
| 8 | + createRoot, |
| 9 | + createSignal, |
| 10 | + flush, |
| 11 | + mapArray, |
| 12 | + refresh, |
| 13 | + untrack |
| 14 | +} from "../src/index.js"; |
| 15 | + |
| 16 | +// #4440: a per-key guess whose action landed, its `refresh` in the air; a |
| 17 | +// second action replaces the list. The refresh's landing settles readers a |
| 18 | +// guess's lane holds: the flush must not join that lane as its transaction |
| 19 | +// (the lane dissolves in the same flush), or the rows born in it are held by |
| 20 | +// a dead lane and never render. |
| 21 | +const tick = async () => { |
| 22 | + for (let i = 0; i < 4; i++) { |
| 23 | + await new Promise(r => setTimeout(r, 0)); |
| 24 | + flush(); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +for (const withCount of [false, true]) |
| 29 | + for (const withAssign of [false, true]) |
| 30 | + it(`a new row renders after a list guess joins a pending refresh (count=${withCount}, assign=${withAssign})`, async () => { |
| 31 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 32 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 33 | + const land = async (key: string, v: any) => { |
| 34 | + for (const a of asks.filter(a => a.key === key)) { |
| 35 | + asks.splice(asks.indexOf(a), 1); |
| 36 | + a.res(v); |
| 37 | + } |
| 38 | + await tick(); |
| 39 | + }; |
| 40 | + let assignTo!: (id: number) => any; |
| 41 | + let closeOne!: (id: number) => any; |
| 42 | + let view: () => string = () => ""; |
| 43 | + createRoot(() => { |
| 44 | + const [issues, set] = createOptimisticStore( |
| 45 | + async (d: any[]) => { |
| 46 | + const r = await ask("fetch"); |
| 47 | + d.length = r.length; |
| 48 | + r.forEach((x: any, i: number) => { |
| 49 | + d[i] = x; |
| 50 | + }); |
| 51 | + }, |
| 52 | + [] as any[], |
| 53 | + { key: "id" } |
| 54 | + ); |
| 55 | + assignTo = action(function* (id: number) { |
| 56 | + const i = untrack(() => issues.findIndex((t: any) => t.id === id)); |
| 57 | + set((d: any) => { |
| 58 | + d[i].assignee = "guest"; |
| 59 | + }); |
| 60 | + yield ask(`assign:${id}`); |
| 61 | + refresh(issues); |
| 62 | + }); |
| 63 | + closeOne = action(function* (id: number) { |
| 64 | + const next = untrack(() => issues.filter((t: any) => t.id !== id)); |
| 65 | + set((d: any) => { |
| 66 | + d.length = next.length; |
| 67 | + next.forEach((x: any, i: number) => { |
| 68 | + d[i] = x; |
| 69 | + }); |
| 70 | + }); |
| 71 | + yield ask(`close:${id}`); |
| 72 | + refresh(issues); |
| 73 | + }); |
| 74 | + const [count, setCount] = createSignal(-1); |
| 75 | + if (withCount) |
| 76 | + createEffect( |
| 77 | + () => issues.length, |
| 78 | + (n: number) => { |
| 79 | + setCount(n); |
| 80 | + } |
| 81 | + ); |
| 82 | + const b = createLoadingBoundary( |
| 83 | + () => |
| 84 | + mapArray( |
| 85 | + () => issues, |
| 86 | + (it: any) => { |
| 87 | + const s = { id: "", a: "" }; |
| 88 | + createRenderEffect( |
| 89 | + () => it.id, |
| 90 | + v => { |
| 91 | + s.id = v; |
| 92 | + } |
| 93 | + ); |
| 94 | + createRenderEffect( |
| 95 | + () => it.assignee, |
| 96 | + v => { |
| 97 | + s.a = v; |
| 98 | + } |
| 99 | + ); |
| 100 | + return s; |
| 101 | + } |
| 102 | + ), |
| 103 | + () => "loading" |
| 104 | + ); |
| 105 | + createRenderEffect( |
| 106 | + () => { |
| 107 | + const v: any = b(); |
| 108 | + return typeof v === "function" ? v() : v; |
| 109 | + }, |
| 110 | + (v: any) => { |
| 111 | + view = () => |
| 112 | + typeof v === "string" ? v : v.map((s: any) => `[#${s.id} ${s.a}]`).join(""); |
| 113 | + } |
| 114 | + ); |
| 115 | + createRenderEffect(count, () => {}); |
| 116 | + }); |
| 117 | + await tick(); |
| 118 | + await land("fetch", [ |
| 119 | + { id: 1, assignee: "ann" }, |
| 120 | + { id: 3, assignee: "bob" }, |
| 121 | + { id: 4, assignee: "dana" } |
| 122 | + ]); |
| 123 | + if (withAssign) { |
| 124 | + assignTo(4); |
| 125 | + await tick(); |
| 126 | + await land("assign:4", {}); |
| 127 | + } |
| 128 | + closeOne(3); |
| 129 | + await tick(); |
| 130 | + await land("close:3", {}); |
| 131 | + await land("fetch", [ |
| 132 | + { id: 1, assignee: "ann" }, |
| 133 | + { id: 4, assignee: withAssign ? "guest" : "dana" } |
| 134 | + ]); |
| 135 | + expect(view()).toBe(`[#1 ann][#4 ${withAssign ? "guest" : "dana"}]`); |
| 136 | + }); |
0 commit comments