|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + action, |
| 4 | + createLoadingBoundary, |
| 5 | + createOptimisticStore, |
| 6 | + createRenderEffect, |
| 7 | + createRoot, |
| 8 | + flush, |
| 9 | + mapArray, |
| 10 | + refresh |
| 11 | +} from "../src/index.js"; |
| 12 | + |
| 13 | +// #4441: two actions each remove a row with `splice`. The first is accepted |
| 14 | +// and refreshes; its refresh answers (without the row) while the second is |
| 15 | +// still pending. The answer is re-based under the second's arrangement guess: |
| 16 | +// the row both guesses and the answer removed must not come back. |
| 17 | +const tick = async () => { |
| 18 | + for (let i = 0; i < 4; i++) { |
| 19 | + await new Promise(r => setTimeout(r, 0)); |
| 20 | + flush(); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +for (const boundary of [false, true]) |
| 25 | + for (const form of ["splice", "replace"] as const) |
| 26 | + it(`a removed row stays removed when its refresh lands under another removal (${form}, Loading=${boundary})`, async () => { |
| 27 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 28 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 29 | + const land = async (key: string, v: any) => { |
| 30 | + for (const a of asks.filter(a => a.key === key)) { |
| 31 | + asks.splice(asks.indexOf(a), 1); |
| 32 | + a.res(v); |
| 33 | + } |
| 34 | + await tick(); |
| 35 | + }; |
| 36 | + const frames: string[] = []; |
| 37 | + let view: () => string = () => ""; |
| 38 | + const snap = (l: string) => frames.push(`${l}: ${view()}`); |
| 39 | + let closeOne!: (id: number) => any; |
| 40 | + let dispose!: () => void; |
| 41 | + createRoot(d => { |
| 42 | + dispose = d; |
| 43 | + const [issues, set] = createOptimisticStore( |
| 44 | + async (d: any[]) => { |
| 45 | + const r = await ask("fetch"); |
| 46 | + d.length = r.length; |
| 47 | + r.forEach((x: any, i: number) => { |
| 48 | + d[i] = x; |
| 49 | + }); |
| 50 | + }, |
| 51 | + [] as any[], |
| 52 | + { key: "id" } |
| 53 | + ); |
| 54 | + closeOne = action(function* (id: number) { |
| 55 | + set((d: any) => { |
| 56 | + if (form === "splice") { |
| 57 | + const i = d.findIndex((t: any) => t.id === id); |
| 58 | + if (i >= 0) d.splice(i, 1); |
| 59 | + } else { |
| 60 | + const next = d.filter((t: any) => t.id !== id); |
| 61 | + d.length = next.length; |
| 62 | + next.forEach((x: any, i: number) => { |
| 63 | + d[i] = x; |
| 64 | + }); |
| 65 | + } |
| 66 | + }); |
| 67 | + yield ask(`close:${id}`); |
| 68 | + refresh(issues); |
| 69 | + }); |
| 70 | + const list = () => |
| 71 | + mapArray( |
| 72 | + () => issues, |
| 73 | + (it: any) => { |
| 74 | + const s = { id: "" }; |
| 75 | + createRenderEffect( |
| 76 | + () => it.id, |
| 77 | + v => { |
| 78 | + s.id = v; |
| 79 | + } |
| 80 | + ); |
| 81 | + return s; |
| 82 | + } |
| 83 | + ); |
| 84 | + const b = boundary ? createLoadingBoundary(list, () => "loading") : list; |
| 85 | + createRenderEffect( |
| 86 | + () => { |
| 87 | + const v: any = b(); |
| 88 | + return typeof v === "function" ? v() : v; |
| 89 | + }, |
| 90 | + (v: any) => { |
| 91 | + view = () => (typeof v === "string" ? v : v.map((s: any) => `[#${s.id}]`).join("")); |
| 92 | + } |
| 93 | + ); |
| 94 | + }); |
| 95 | + await tick(); |
| 96 | + await land("fetch", [{ id: 1 }, { id: 3 }, { id: 4 }]); |
| 97 | + snap("loaded"); |
| 98 | + closeOne(1); |
| 99 | + await tick(); |
| 100 | + snap("close 1"); |
| 101 | + closeOne(4); |
| 102 | + await tick(); |
| 103 | + snap("close 4"); |
| 104 | + await land("close:1", {}); |
| 105 | + snap("close 1 accepted"); |
| 106 | + await land("fetch", [{ id: 3 }, { id: 4 }]); |
| 107 | + snap("refresh answered"); |
| 108 | + await land("close:4", {}); |
| 109 | + await land("fetch", [{ id: 3 }]); |
| 110 | + snap("close 4 settled"); |
| 111 | + dispose(); |
| 112 | + expect(frames).toEqual([ |
| 113 | + "loaded: [#1][#3][#4]", |
| 114 | + "close 1: [#3][#4]", |
| 115 | + "close 4: [#3]", |
| 116 | + "close 1 accepted: [#3]", |
| 117 | + "refresh answered: [#3]", |
| 118 | + "close 4 settled: [#3]" |
| 119 | + ]); |
| 120 | + }); |
0 commit comments