|
| 1 | +/** |
| 2 | + * #4342: a derived store's pass that peeks (untracked, `NotReadyError` |
| 3 | + * caught) at a memo built from one of its own leaves does not re-run without |
| 4 | + * bound. The peek of a pending memo links it, so the memo's change re-runs |
| 5 | + * the derive; the reader of the leaf is linked to the derive by the flight it |
| 6 | + * observed. A held frame keeps both links after the passes that dropped them |
| 7 | + * (A30). The derive's sync pass woke every reader linked to it, so a reader |
| 8 | + * that builds a fresh object (a query key) re-ran the memo, the memo re-ran |
| 9 | + * the derive, and the derive re-ran the reader, all in one flush. A pass of |
| 10 | + * a settled derive now wakes no reader; a landing still does (#4070). |
| 11 | + * |
| 12 | + * "issue": the report's shape (a query hook whose projection peeks at every |
| 13 | + * mounted hook's key on a cache miss, a keyed list of rows each with its own |
| 14 | + * hook, a parent key from an async memo). The regression of #4307: the wake |
| 15 | + * parks the rows' readers on the derive, so a row's key is still pending |
| 16 | + * when the derive's pass for the new parent key peeks at it. |
| 17 | + * "rows" / "direct": the same loop without boundaries or a row hook, through |
| 18 | + * a keyed `mapArray` and through a reader of the leaf with no list. |
| 19 | + * "other-source": the peeked key is pending on a different async memo, not on |
| 20 | + * the derive (this loop predates #4307). |
| 21 | + */ |
| 22 | +import { describe, expect, it } from "vitest"; |
| 23 | +import { |
| 24 | + createErrorBoundary, |
| 25 | + createLoadingBoundary, |
| 26 | + createMemo, |
| 27 | + createProjection, |
| 28 | + createRenderEffect, |
| 29 | + createRoot, |
| 30 | + createSignal, |
| 31 | + flush, |
| 32 | + mapArray, |
| 33 | + NotReadyError, |
| 34 | + onCleanup, |
| 35 | + untrack |
| 36 | +} from "../src/index.js"; |
| 37 | + |
| 38 | +const wait = (ms: number) => new Promise(r => setTimeout(r, ms)); |
| 39 | +const tick = async () => { |
| 40 | + for (let i = 0; i < 3; i++) { |
| 41 | + await wait(0); |
| 42 | + flush(); |
| 43 | + } |
| 44 | +}; |
| 45 | +const peekAll = (peeks: Iterable<() => unknown>) => { |
| 46 | + for (const peek of peeks) |
| 47 | + try { |
| 48 | + untrack(peek); |
| 49 | + } catch (err) { |
| 50 | + if (!(err instanceof NotReadyError)) throw err; |
| 51 | + } |
| 52 | +}; |
| 53 | +const resolveAll = (v: any): string => |
| 54 | + typeof v === "function" |
| 55 | + ? resolveAll(v()) |
| 56 | + : Array.isArray(v) |
| 57 | + ? v.map(resolveAll).join("") |
| 58 | + : String(v ?? ""); |
| 59 | + |
| 60 | +describe("#4342: a derive's peek at a reader of its own leaf", () => { |
| 61 | + it("issue: switching the parent key settles", async () => { |
| 62 | + const keys = new Set<() => { hash: string }>(); |
| 63 | + let runs = 0; |
| 64 | + function useQuery<T>(keyFn: () => string, fetcher: () => Promise<T>) { |
| 65 | + const key = createMemo(() => ({ hash: keyFn() })); |
| 66 | + keys.add(key); |
| 67 | + onCleanup(() => keys.delete(key)); |
| 68 | + const cache = new Map<string, { p: Promise<{ value: T }>; d?: T }>(); |
| 69 | + const data = createProjection<{ value?: T }>(() => { |
| 70 | + const k = key().hash; |
| 71 | + let e = cache.get(k); |
| 72 | + if (!e) { |
| 73 | + const entry: { p: Promise<{ value: T }>; d?: T } = { |
| 74 | + p: fetcher().then(d => ((entry.d = d), { value: d })) |
| 75 | + }; |
| 76 | + cache.set(k, (e = entry)); |
| 77 | + peekAll(keys); |
| 78 | + } |
| 79 | + return e.d !== undefined ? { value: e.d } : e.p; |
| 80 | + }, {}); |
| 81 | + return () => data.value as T; |
| 82 | + } |
| 83 | + const [id, setId] = createSignal("A"); |
| 84 | + let release!: (v: { key: string }[]) => void; |
| 85 | + const later = new Promise<{ key: string }[]>(r => (release = r)); |
| 86 | + const Item = (props: { link: string; item: { key: string } }) => { |
| 87 | + const opts = createMemo(() => { |
| 88 | + if (++runs > 200) throw new Error("runaway"); |
| 89 | + return { link: props.link, item: props.item.key }; |
| 90 | + }); |
| 91 | + useQuery( |
| 92 | + () => `${opts().link}:${opts().item}`, |
| 93 | + async () => 0 |
| 94 | + ); |
| 95 | + return () => props.item.key; |
| 96 | + }; |
| 97 | + const List = (props: { link: string }) => { |
| 98 | + const list = useQuery( |
| 99 | + () => props.link, |
| 100 | + () => (props.link === "A" ? Promise.resolve([{ key: "a" }]) : later) |
| 101 | + ); |
| 102 | + return createLoadingBoundary( |
| 103 | + () => |
| 104 | + createErrorBoundary( |
| 105 | + () => |
| 106 | + mapArray( |
| 107 | + () => list(), |
| 108 | + x => |
| 109 | + untrack(() => |
| 110 | + Item({ |
| 111 | + get link() { |
| 112 | + return props.link; |
| 113 | + }, |
| 114 | + get item() { |
| 115 | + return x(); |
| 116 | + } |
| 117 | + }) |
| 118 | + ), |
| 119 | + { keyed: (x: { key: string }) => x.key } |
| 120 | + ), |
| 121 | + e => String(e()) |
| 122 | + ), |
| 123 | + () => "..." |
| 124 | + ); |
| 125 | + }; |
| 126 | + let screen = ""; |
| 127 | + const dispose = createRoot(d => { |
| 128 | + const link = createMemo(async () => { |
| 129 | + const v = id(); |
| 130 | + await Promise.resolve(); |
| 131 | + return v; |
| 132 | + }); |
| 133 | + const out = createLoadingBoundary( |
| 134 | + () => |
| 135 | + untrack(() => |
| 136 | + List({ |
| 137 | + get link() { |
| 138 | + return link(); |
| 139 | + } |
| 140 | + }) |
| 141 | + ), |
| 142 | + () => "..." |
| 143 | + ); |
| 144 | + createRenderEffect( |
| 145 | + () => resolveAll(out), |
| 146 | + v => { |
| 147 | + screen = v; |
| 148 | + } |
| 149 | + ); |
| 150 | + return d; |
| 151 | + }); |
| 152 | + flush(); |
| 153 | + await wait(20); |
| 154 | + expect(screen).toBe("a"); |
| 155 | + const before = runs; |
| 156 | + setId("B"); |
| 157 | + flush(); |
| 158 | + await wait(20); |
| 159 | + release([{ key: "b" }]); |
| 160 | + await wait(20); |
| 161 | + expect(screen).toBe("b"); |
| 162 | + expect(runs - before).toBeLessThan(10); |
| 163 | + dispose(); |
| 164 | + }); |
| 165 | + |
| 166 | + for (const mode of ["rows", "direct", "other-source"] as const) { |
| 167 | + it(`${mode}: the landing settles`, async () => { |
| 168 | + const [id, setId] = createSignal("A"); |
| 169 | + const res: Record<string, (v: { key: string }[]) => void> = {}; |
| 170 | + const slowRes: Record<string, (v: string) => void> = {}; |
| 171 | + let runs = 0; |
| 172 | + let screen = ""; |
| 173 | + const dispose = createRoot(d => { |
| 174 | + // "other-source": the readers wait on `slow`; the derive's input is sync. |
| 175 | + const link = |
| 176 | + mode === "other-source" |
| 177 | + ? id |
| 178 | + : createMemo(async () => { |
| 179 | + const v = id(); |
| 180 | + await Promise.resolve(); |
| 181 | + return v; |
| 182 | + }); |
| 183 | + // The rows' other input: the parent key itself, else a slower memo. |
| 184 | + const slow = |
| 185 | + mode === "other-source" |
| 186 | + ? createMemo(() => { |
| 187 | + const v = id(); |
| 188 | + return v === "A" ? v : new Promise<string>(r => (slowRes[v] = r)); |
| 189 | + }) |
| 190 | + : link; |
| 191 | + const peeks: (() => unknown)[] = []; |
| 192 | + const cache: Record<string, { p: Promise<{ rows: { key: string }[] }>; d?: any }> = {}; |
| 193 | + const p = createProjection<{ rows?: { key: string }[] }>(() => { |
| 194 | + const k = link(); |
| 195 | + let e = cache[k]; |
| 196 | + if (!e) { |
| 197 | + const entry: (typeof cache)[string] = { |
| 198 | + p: new Promise<{ key: string }[]>(r => (res[k] = r)).then( |
| 199 | + v => ((entry.d = v), { rows: v }) |
| 200 | + ) |
| 201 | + }; |
| 202 | + e = cache[k] = entry; |
| 203 | + peekAll(peeks); |
| 204 | + } |
| 205 | + return e.d ? { rows: e.d } : e.p; |
| 206 | + }, {}); |
| 207 | + const row = (item: () => string | undefined) => { |
| 208 | + const opts = createMemo(() => { |
| 209 | + if (++runs > 200) throw new Error("runaway"); |
| 210 | + return { s: slow(), item: item() }; |
| 211 | + }); |
| 212 | + const key = createMemo(() => ({ hash: `${opts().s}:${opts().item}` })); |
| 213 | + peeks.push(key); |
| 214 | + return () => key().hash; |
| 215 | + }; |
| 216 | + const view = |
| 217 | + mode === "direct" |
| 218 | + ? (f => () => [f])(row(() => p.rows?.[0]?.key)) |
| 219 | + : mapArray( |
| 220 | + () => p.rows, |
| 221 | + r => row(() => r().key), |
| 222 | + { keyed: (r: { key: string }) => r.key } |
| 223 | + ); |
| 224 | + createRenderEffect( |
| 225 | + () => |
| 226 | + view() |
| 227 | + .map(f => f()) |
| 228 | + .join(","), |
| 229 | + v => { |
| 230 | + screen = v; |
| 231 | + } |
| 232 | + ); |
| 233 | + return d; |
| 234 | + }); |
| 235 | + flush(); |
| 236 | + await tick(); |
| 237 | + res.A([{ key: "a" }]); |
| 238 | + await tick(); |
| 239 | + expect(screen).toBe("A:a"); |
| 240 | + const before = runs; |
| 241 | + setId("B"); |
| 242 | + flush(); |
| 243 | + await tick(); |
| 244 | + if (mode === "other-source") { |
| 245 | + slowRes.B("B"); |
| 246 | + await tick(); |
| 247 | + } |
| 248 | + res.B([{ key: "b" }]); |
| 249 | + await tick(); |
| 250 | + expect(screen).toBe("B:b"); |
| 251 | + expect(runs - before).toBeLessThan(10); |
| 252 | + dispose(); |
| 253 | + }); |
| 254 | + } |
| 255 | +}); |
0 commit comments