|
| 1 | +/** |
| 2 | + * #4379 (ReactiveScript tracker eval): a keyed `createOptimisticStore` list |
| 3 | + * whose question reads `filter()`, under `<Loading on={filter()}>`, with a |
| 4 | + * render effect per row (as `<For>` binds each row's text). An action |
| 5 | + * guesses a row, yields, then `refresh()`es the list. A plain `filter` |
| 6 | + * write while the action is out is held with it (D-34, #4336), so the |
| 7 | + * boundary's swap is staged into that hold (D-15) and the screen keeps the |
| 8 | + * old list. |
| 9 | + * |
| 10 | + * Once the new question's answer lands, held with the same action, the |
| 11 | + * content is ready inside the hold the fallback was staged into: the seam |
| 12 | + * folds the fallback back (D-16, U-9), as it does when the readers are |
| 13 | + * already landed at the re-arm. When the action ends, its own `refresh()` |
| 14 | + * is quiet (F-9): it must not pull the fallback on screen. On `next` the |
| 15 | + * fold only ran in the re-arm's own flush (`_armed`); the rows' effects |
| 16 | + * survive the answer (same keys) and land a flush later, so the staged |
| 17 | + * fallback committed with the action, and the refresh's pending, caught by |
| 18 | + * a boundary still collecting, showed it until the refresh answered. |
| 19 | + */ |
| 20 | +import { describe, expect, it } from "vitest"; |
| 21 | +import { |
| 22 | + action, |
| 23 | + createLoadingBoundary, |
| 24 | + createOptimisticStore, |
| 25 | + createRenderEffect, |
| 26 | + createRoot, |
| 27 | + createSignal, |
| 28 | + flush, |
| 29 | + mapArray, |
| 30 | + refresh, |
| 31 | + untrack |
| 32 | +} from "../src/index.js"; |
| 33 | + |
| 34 | +const tick = async () => { |
| 35 | + for (let i = 0; i < 4; i++) { |
| 36 | + await new Promise(r => setTimeout(r, 0)); |
| 37 | + flush(); |
| 38 | + } |
| 39 | +}; |
| 40 | +type Issue = { id: number; assignee: string | null }; |
| 41 | +const OPEN: Issue[] = [ |
| 42 | + { id: 1, assignee: null }, |
| 43 | + { id: 3, assignee: null } |
| 44 | +]; |
| 45 | +const ASSIGNED: Issue[] = [ |
| 46 | + { id: 1, assignee: null }, |
| 47 | + { id: 3, assignee: "ann" } |
| 48 | +]; |
| 49 | + |
| 50 | +function page(on: boolean) { |
| 51 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 52 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 53 | + const land = async (key: string, v: any) => { |
| 54 | + const i = asks.findIndex(a => a.key === key); |
| 55 | + if (i < 0) throw new Error(`not asked: ${key}; asked: ${asks.map(a => a.key)}`); |
| 56 | + // A fresh answer each time, as a fetch gives. |
| 57 | + asks.splice(i, 1)[0].res(structuredClone(v)); |
| 58 | + await tick(); |
| 59 | + }; |
| 60 | + const frames: string[] = []; |
| 61 | + let setFilter!: (f: string) => void; |
| 62 | + let assign!: (id: number, who: string) => void; |
| 63 | + createRoot(() => { |
| 64 | + const [filter, sf] = createSignal("open"); |
| 65 | + setFilter = sf; |
| 66 | + const [issues, setIssues] = createOptimisticStore<Issue[]>( |
| 67 | + async (d: Issue[]) => { |
| 68 | + const r: Issue[] = await ask(`fetchIssues:${filter()}`); |
| 69 | + d.length = r.length; |
| 70 | + r.forEach((x, i) => ((d as any)[i] = x)); |
| 71 | + }, |
| 72 | + [], |
| 73 | + { key: "id" } as any |
| 74 | + ); |
| 75 | + assign = action(function* (id: number, who: string) { |
| 76 | + const i = issues.findIndex(t => t.id === id); |
| 77 | + setIssues((d: Issue[]) => { |
| 78 | + d[i].assignee = who; |
| 79 | + }); |
| 80 | + yield ask(`assign:${id},${who}`); |
| 81 | + refresh(issues); |
| 82 | + }) as any; |
| 83 | + const list = untrack(() => |
| 84 | + createLoadingBoundary( |
| 85 | + () => { |
| 86 | + const m = mapArray( |
| 87 | + () => issues, |
| 88 | + (i: any) => { |
| 89 | + // Frame readers per row, under the boundary (`<For>`'s text |
| 90 | + // holes). The `id` read is not served by the guess: over the |
| 91 | + // new question it is an ordinary pending read (#4336 |
| 92 | + // `stands`), recorded by the boundary, and it lands held. |
| 93 | + createRenderEffect( |
| 94 | + () => i.id, |
| 95 | + () => {} |
| 96 | + ); |
| 97 | + createRenderEffect( |
| 98 | + () => i.assignee, |
| 99 | + () => {} |
| 100 | + ); |
| 101 | + return () => `#${i.id}(${i.assignee ?? "-"})`; |
| 102 | + } |
| 103 | + ); |
| 104 | + return () => |
| 105 | + m() |
| 106 | + .map(f => f()) |
| 107 | + .join(" "); |
| 108 | + }, |
| 109 | + () => "loading", |
| 110 | + on ? { on: () => filter() } : undefined |
| 111 | + ) |
| 112 | + ); |
| 113 | + createRenderEffect( |
| 114 | + () => { |
| 115 | + const v: any = list(); |
| 116 | + return typeof v === "function" ? v() : v; |
| 117 | + }, |
| 118 | + v => { |
| 119 | + if (frames.at(-1) !== v) frames.push(v); |
| 120 | + } |
| 121 | + ); |
| 122 | + }); |
| 123 | + return { |
| 124 | + land, |
| 125 | + asks, |
| 126 | + frames, |
| 127 | + screen: () => frames.at(-1), |
| 128 | + setFilter: async (f: string) => { |
| 129 | + setFilter(f); |
| 130 | + flush(); |
| 131 | + await tick(); |
| 132 | + }, |
| 133 | + assign: async (id: number, who: string) => { |
| 134 | + assign(id, who); |
| 135 | + flush(); |
| 136 | + await tick(); |
| 137 | + } |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +const MOUNT = ["loading", "#1(-) #3(-)"]; |
| 142 | + |
| 143 | +describe("#4379 a held `on` change whose content lands inside the hold", () => { |
| 144 | + for (const on of [true, false]) { |
| 145 | + it(`toggled back while held: the action's refresh shows no fallback (on=${on})`, async () => { |
| 146 | + const p = page(on); |
| 147 | + await tick(); |
| 148 | + await p.land("fetchIssues:open", OPEN); |
| 149 | + await p.assign(3, "ann"); |
| 150 | + await p.setFilter("closed"); |
| 151 | + await p.setFilter("open"); |
| 152 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 153 | + await p.land("fetchIssues:closed", []); |
| 154 | + await p.land("fetchIssues:open", OPEN); |
| 155 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 156 | + await p.land("assign:3,ann", undefined); |
| 157 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 158 | + await p.land("fetchIssues:open", ASSIGNED); |
| 159 | + expect(p.frames).toEqual([...MOUNT, "#1(-) #3(ann)"]); |
| 160 | + expect(p.asks).toEqual([]); |
| 161 | + }); |
| 162 | + |
| 163 | + // The same seam without the toggle: the new question's answer keeps the |
| 164 | + // rows (same keys), so their effects survive and land held. |
| 165 | + it(`a new question whose answer keeps the rows: no fallback (on=${on})`, async () => { |
| 166 | + const p = page(on); |
| 167 | + await tick(); |
| 168 | + await p.land("fetchIssues:open", OPEN); |
| 169 | + await p.assign(3, "ann"); |
| 170 | + await p.setFilter("closed"); |
| 171 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 172 | + await p.land("fetchIssues:closed", OPEN); |
| 173 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 174 | + await p.land("assign:3,ann", undefined); |
| 175 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 176 | + await p.land("fetchIssues:closed", ASSIGNED); |
| 177 | + expect(p.frames).toEqual([...MOUNT, "#1(-) #3(ann)"]); |
| 178 | + expect(p.asks).toEqual([]); |
| 179 | + }); |
| 180 | + } |
| 181 | + |
| 182 | + // Control (passes on `next`): the answer drops the rows, so their effects |
| 183 | + // die and the boundary's own pass finds nothing left to wait on. |
| 184 | + it("control: an answer that drops the rows holds the old list to the refresh", async () => { |
| 185 | + const p = page(true); |
| 186 | + await tick(); |
| 187 | + await p.land("fetchIssues:open", OPEN); |
| 188 | + await p.assign(3, "ann"); |
| 189 | + await p.setFilter("closed"); |
| 190 | + await p.land("fetchIssues:closed", []); |
| 191 | + await p.land("assign:3,ann", undefined); |
| 192 | + expect(p.screen()).toBe("#1(-) #3(ann)"); |
| 193 | + await p.land("fetchIssues:closed", []); |
| 194 | + expect(p.frames).toEqual([...MOUNT, "#1(-) #3(ann)", ""]); |
| 195 | + }); |
| 196 | + |
| 197 | + // Control: nothing holds the change — the fallback shows with it (D-15). |
| 198 | + it("control: no hold — the fallback shows with the change and clears at its answer", async () => { |
| 199 | + const p = page(true); |
| 200 | + await tick(); |
| 201 | + await p.land("fetchIssues:open", OPEN); |
| 202 | + await p.setFilter("closed"); |
| 203 | + expect(p.screen()).toBe("loading"); |
| 204 | + await p.land("fetchIssues:closed", OPEN); |
| 205 | + expect(p.frames).toEqual([...MOUNT, "loading", "#1(-) #3(-)"]); |
| 206 | + }); |
| 207 | +}); |
0 commit comments