|
| 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 | +const tick = async () => { |
| 14 | + for (let i = 0; i < 4; i++) { |
| 15 | + await new Promise(r => setTimeout(r, 0)); |
| 16 | + flush(); |
| 17 | + } |
| 18 | +}; |
| 19 | +test("#4577 with a second async change entangled mid-hold: the screen settles as one (F-1, D-9)", async () => { |
| 20 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 21 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 22 | + const settle = async (pre: string, v: any) => { |
| 23 | + asks |
| 24 | + .splice( |
| 25 | + asks.findIndex(a => a.key.startsWith(pre)), |
| 26 | + 1 |
| 27 | + )[0] |
| 28 | + .res(v); |
| 29 | + await tick(); |
| 30 | + }; |
| 31 | + const frames: string[] = []; |
| 32 | + let f = "", |
| 33 | + l = "", |
| 34 | + o = ""; |
| 35 | + let q = false; |
| 36 | + const snap = () => { |
| 37 | + if (!q) { |
| 38 | + q = true; |
| 39 | + queueMicrotask(() => { |
| 40 | + q = false; |
| 41 | + frames.push(`${f}: ${l} | ${o}`); |
| 42 | + }); |
| 43 | + } |
| 44 | + }; |
| 45 | + let setFilter!: (v: string) => void, |
| 46 | + setTag!: (v: string) => void, |
| 47 | + closeOne!: (id: number) => unknown, |
| 48 | + dispose!: () => void; |
| 49 | + createRoot(d => { |
| 50 | + dispose = d; |
| 51 | + const [filter, sf] = createSignal("open"); |
| 52 | + setFilter = sf; |
| 53 | + const [tag, st] = createSignal("t0"); |
| 54 | + setTag = st; |
| 55 | + const [issues, set] = createOptimisticStore<{ id: number }[]>( |
| 56 | + () => ask(`issues:${filter()}`), |
| 57 | + [], |
| 58 | + { key: "id" } |
| 59 | + ); |
| 60 | + const other = createMemo(() => (issues.length, ask(`other:${filter()}:${tag()}`))); |
| 61 | + const tagv = createMemo(() => ask(`tag:${tag()}`)); |
| 62 | + closeOne = action(function* (id: number) { |
| 63 | + set(xs => { |
| 64 | + const i = xs.findIndex(t => t.id === id); |
| 65 | + if (i >= 0) xs.splice(i, 1); |
| 66 | + }); |
| 67 | + yield ask(`close:${id}`); |
| 68 | + refresh(issues); |
| 69 | + }); |
| 70 | + const list = createMemo(() => issues.map(r => `[#${r.id}]`).join("")); |
| 71 | + createRenderEffect(filter, v => { |
| 72 | + f = v; |
| 73 | + snap(); |
| 74 | + }); |
| 75 | + createRenderEffect(list, v => { |
| 76 | + l = v; |
| 77 | + snap(); |
| 78 | + }); |
| 79 | + createRenderEffect( |
| 80 | + createErrorBoundary( |
| 81 | + () => other(), |
| 82 | + () => "ERR" |
| 83 | + ) as any, |
| 84 | + (v: any) => { |
| 85 | + o = String(v); |
| 86 | + snap(); |
| 87 | + } |
| 88 | + ); |
| 89 | + createRenderEffect( |
| 90 | + createErrorBoundary( |
| 91 | + () => String(tagv()), |
| 92 | + () => "ERR" |
| 93 | + ) as any, |
| 94 | + () => {} |
| 95 | + ); |
| 96 | + }); |
| 97 | + await tick(); |
| 98 | + await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]); |
| 99 | + await settle("other:open", "O-open"); |
| 100 | + await settle("tag:t0", "t0"); |
| 101 | + setFilter("closed"); |
| 102 | + await tick(); |
| 103 | + closeOne(1); |
| 104 | + await tick(); |
| 105 | + setTag("t1"); |
| 106 | + await tick(); |
| 107 | + await settle("issues:closed", [{ id: 1 }, { id: 2 }]); |
| 108 | + await settle("other:closed", "O-closed"); |
| 109 | + await settle("close:1", {}); |
| 110 | + await settle("issues:closed", [{ id: 2 }]); |
| 111 | + for (let k = 0; k < 20 && asks.length; k++) { |
| 112 | + const a = asks.shift()!; |
| 113 | + const p = a.key.split(":"); |
| 114 | + a.res( |
| 115 | + p[0] === "other" ? `O-${p[1]}` : p[0] === "tag" ? p[1] : p[0] === "issues" ? [{ id: 2 }] : {} |
| 116 | + ); |
| 117 | + await tick(); |
| 118 | + } |
| 119 | + for (let i = 0; i < 5; i++) await tick(); |
| 120 | + for (const fr of frames) |
| 121 | + if (fr.includes("|") && !fr.endsWith("| ")) |
| 122 | + expect(fr.startsWith("closed"), fr).toBe(fr.includes("O-closed")); |
| 123 | + expect(`${f}: ${l} | ${o}`).toBe("closed: [#2] | O-closed"); |
| 124 | + dispose(); |
| 125 | +}); |
| 126 | + |
| 127 | +// The matrix around it: when the second change starts (before the guess, |
| 128 | +// after it, after the filter's answer lands), whether `other` reads the |
| 129 | +// guessed list, and whether filter and list share one render effect. |
| 130 | +for (const when of ["beforeGuess", "afterGuess", "afterLanding"] as const) |
| 131 | + for (const reader of ["readsList", "plain"] as const) |
| 132 | + for (const shape of ["shared", "split"] as const) |
| 133 | + test(`#4577 entangled matrix: tag ${when}, other ${reader}, ${shape} (F-1, D-9)`, async () => { |
| 134 | + const asks: { key: string; res: (v: any) => void }[] = []; |
| 135 | + const ask = (key: string) => new Promise<any>(res => asks.push({ key, res })); |
| 136 | + const settle = async (pre: string, v: any) => { |
| 137 | + asks |
| 138 | + .splice( |
| 139 | + asks.findIndex(a => a.key.startsWith(pre)), |
| 140 | + 1 |
| 141 | + )[0] |
| 142 | + .res(v); |
| 143 | + await tick(); |
| 144 | + }; |
| 145 | + const frames: string[] = []; |
| 146 | + let f = "", |
| 147 | + l = "", |
| 148 | + o = ""; |
| 149 | + let q = false; |
| 150 | + const snap = () => { |
| 151 | + if (!q) { |
| 152 | + q = true; |
| 153 | + queueMicrotask(() => { |
| 154 | + q = false; |
| 155 | + frames.push(`${f}: ${l} | ${o}`); |
| 156 | + }); |
| 157 | + } |
| 158 | + }; |
| 159 | + let setFilter!: (v: string) => void, |
| 160 | + setTag!: (v: string) => void, |
| 161 | + closeOne!: (id: number) => unknown, |
| 162 | + dispose!: () => void; |
| 163 | + createRoot(d => { |
| 164 | + dispose = d; |
| 165 | + const [filter, sf] = createSignal("open"); |
| 166 | + setFilter = sf; |
| 167 | + const [tag, st] = createSignal("t0"); |
| 168 | + setTag = st; |
| 169 | + const [issues, set] = createOptimisticStore<{ id: number }[]>( |
| 170 | + () => ask(`issues:${filter()}`), |
| 171 | + [], |
| 172 | + { key: "id" } |
| 173 | + ); |
| 174 | + const other = createMemo( |
| 175 | + () => (reader === "readsList" && issues.length, ask(`other:${filter()}:${tag()}`)) |
| 176 | + ); |
| 177 | + const tagv = createMemo(() => ask(`tag:${tag()}`)); |
| 178 | + closeOne = action(function* (id: number) { |
| 179 | + set(xs => { |
| 180 | + const i = xs.findIndex(t => t.id === id); |
| 181 | + if (i >= 0) xs.splice(i, 1); |
| 182 | + }); |
| 183 | + yield ask(`close:${id}`); |
| 184 | + refresh(issues); |
| 185 | + }); |
| 186 | + const list = createMemo(() => issues.map(r => `[#${r.id}]`).join("")); |
| 187 | + if (shape === "shared") |
| 188 | + createRenderEffect( |
| 189 | + () => [filter(), list()] as const, |
| 190 | + ([a, b]) => { |
| 191 | + f = a; |
| 192 | + l = b; |
| 193 | + snap(); |
| 194 | + } |
| 195 | + ); |
| 196 | + else { |
| 197 | + createRenderEffect(filter, v => { |
| 198 | + f = v; |
| 199 | + snap(); |
| 200 | + }); |
| 201 | + createRenderEffect(list, v => { |
| 202 | + l = v; |
| 203 | + snap(); |
| 204 | + }); |
| 205 | + } |
| 206 | + createRenderEffect( |
| 207 | + createErrorBoundary( |
| 208 | + () => other(), |
| 209 | + () => "ERR" |
| 210 | + ) as any, |
| 211 | + (v: any) => { |
| 212 | + o = String(v); |
| 213 | + snap(); |
| 214 | + } |
| 215 | + ); |
| 216 | + createRenderEffect( |
| 217 | + createErrorBoundary( |
| 218 | + () => String(tagv()), |
| 219 | + () => "ERR" |
| 220 | + ) as any, |
| 221 | + () => {} |
| 222 | + ); |
| 223 | + }); |
| 224 | + await tick(); |
| 225 | + await settle("issues:open", [{ id: 1 }, { id: 3 }, { id: 4 }]); |
| 226 | + await settle("other:open", "O-open"); |
| 227 | + await settle("tag:t0", "t0"); |
| 228 | + setFilter("closed"); |
| 229 | + await tick(); |
| 230 | + if (when === "beforeGuess") (setTag("t1"), await tick()); |
| 231 | + closeOne(1); |
| 232 | + await tick(); |
| 233 | + if (when === "afterGuess") (setTag("t1"), await tick()); |
| 234 | + await settle("issues:closed", [{ id: 1 }, { id: 2 }]); |
| 235 | + if (when === "afterLanding") (setTag("t1"), await tick()); |
| 236 | + await settle("other:closed", "O-closed"); |
| 237 | + await settle("close:1", {}); |
| 238 | + await settle("issues:closed", [{ id: 2 }]); |
| 239 | + for (let k = 0; k < 20 && asks.length; k++) { |
| 240 | + const a = asks.shift()!; |
| 241 | + const p = a.key.split(":"); |
| 242 | + a.res( |
| 243 | + p[0] === "other" |
| 244 | + ? `O-${p[1]}` |
| 245 | + : p[0] === "tag" |
| 246 | + ? p[1] |
| 247 | + : p[0] === "issues" |
| 248 | + ? [{ id: 2 }] |
| 249 | + : {} |
| 250 | + ); |
| 251 | + await tick(); |
| 252 | + } |
| 253 | + for (let i = 0; i < 5; i++) await tick(); |
| 254 | + for (const fr of frames) |
| 255 | + if (fr.includes("|") && !fr.endsWith("| ")) |
| 256 | + expect(fr.startsWith("closed"), fr).toBe(fr.includes("O-closed")); |
| 257 | + expect(`${f}: ${l} | ${o}`).toBe("closed: [#2] | O-closed"); |
| 258 | + dispose(); |
| 259 | + }); |
0 commit comments