|
| 1 | +// An untracked `isPending`/`latest` is a one-off read: it marks its reader a |
| 2 | +// verdict reader for how the pass reads (a frame reader sees the screen, A10) |
| 3 | +// but not for when the pass runs. A tracked verdict re-derives its reader when |
| 4 | +// a dependency goes pending, since the answer changed; an untracked one does |
| 5 | +// not, the same as any other untracked read. Before, the window stamped the |
| 6 | +// running computation either way, so a library that probes pending state |
| 7 | +// inside `untrack` (a router's link claim) made whatever computation created |
| 8 | +// it re-run on each of its own dependencies going pending. |
| 9 | + |
| 10 | +import { describe, expect, it } from "vitest"; |
| 11 | +import { |
| 12 | + action, |
| 13 | + createEffect, |
| 14 | + createMemo, |
| 15 | + createRenderEffect, |
| 16 | + createRoot, |
| 17 | + createSignal, |
| 18 | + flush, |
| 19 | + isPending, |
| 20 | + latest, |
| 21 | + untrack |
| 22 | +} from "../src/index.js"; |
| 23 | + |
| 24 | +function deferred<T>() { |
| 25 | + let resolve!: (v: T) => void; |
| 26 | + const promise = new Promise<T>(r => (resolve = r)); |
| 27 | + return { promise, resolve }; |
| 28 | +} |
| 29 | + |
| 30 | +async function settle() { |
| 31 | + await Promise.resolve(); |
| 32 | + await Promise.resolve(); |
| 33 | + flush(); |
| 34 | +} |
| 35 | + |
| 36 | +/** Runs of a memo over an async source while the source refetches. */ |
| 37 | +async function runsWhilePending(probe: (data: () => string) => void) { |
| 38 | + let runs = 0; |
| 39 | + let setId!: (v: number) => void; |
| 40 | + let cur = deferred<string>(); |
| 41 | + const seen: string[] = []; |
| 42 | + createRoot(() => { |
| 43 | + const [id, set] = createSignal(1); |
| 44 | + setId = set; |
| 45 | + const data = createMemo(() => { |
| 46 | + const v = id(); |
| 47 | + return cur.promise.then(s => `${s}-${v}`); |
| 48 | + }); |
| 49 | + const host = createMemo(() => { |
| 50 | + runs++; |
| 51 | + const v = data(); |
| 52 | + probe(data); |
| 53 | + return v; |
| 54 | + }); |
| 55 | + createRenderEffect( |
| 56 | + () => { |
| 57 | + try { |
| 58 | + return host(); |
| 59 | + } catch { |
| 60 | + return "pending"; |
| 61 | + } |
| 62 | + }, |
| 63 | + v => { |
| 64 | + seen.push(v); |
| 65 | + } |
| 66 | + ); |
| 67 | + }); |
| 68 | + flush(); |
| 69 | + cur.resolve("a"); |
| 70 | + await settle(); |
| 71 | + const before = runs; |
| 72 | + cur = deferred<string>(); |
| 73 | + setId(2); |
| 74 | + flush(); |
| 75 | + await settle(); |
| 76 | + const pendingRuns = runs - before; |
| 77 | + cur.resolve("b"); |
| 78 | + await settle(); |
| 79 | + await settle(); |
| 80 | + return { pendingRuns, seen }; |
| 81 | +} |
| 82 | + |
| 83 | +describe("untracked verdict reads", () => { |
| 84 | + it("an untracked isPending does not re-derive its reader while a dependency is pending", async () => { |
| 85 | + const { pendingRuns, seen } = await runsWhilePending(() => untrack(() => isPending(() => {}))); |
| 86 | + expect(pendingRuns).toBe(0); |
| 87 | + expect(seen.at(-1)).toBe("b-2"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("an untracked latest does not re-derive its reader while a dependency is pending", async () => { |
| 91 | + const { pendingRuns, seen } = await runsWhilePending(data => untrack(() => latest(data))); |
| 92 | + expect(pendingRuns).toBe(0); |
| 93 | + expect(seen.at(-1)).toBe("b-2"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("a tracked isPending still re-derives its reader while a dependency is pending", async () => { |
| 97 | + const { pendingRuns, seen } = await runsWhilePending(data => isPending(data)); |
| 98 | + expect(pendingRuns).toBe(1); |
| 99 | + expect(seen.at(-1)).toBe("b-2"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("a reader that stops reading isPending tracked stops re-deriving", async () => { |
| 103 | + let runs = 0; |
| 104 | + let setId!: (v: number) => void; |
| 105 | + let setTracked!: (v: boolean) => void; |
| 106 | + let cur = deferred<string>(); |
| 107 | + createRoot(() => { |
| 108 | + const [id, set] = createSignal(1); |
| 109 | + const [tracked, setT] = createSignal(true); |
| 110 | + setId = set; |
| 111 | + setTracked = setT; |
| 112 | + const data = createMemo(() => { |
| 113 | + const v = id(); |
| 114 | + return cur.promise.then(s => `${s}-${v}`); |
| 115 | + }); |
| 116 | + const host = createMemo(() => { |
| 117 | + runs++; |
| 118 | + const v = data(); |
| 119 | + if (tracked()) isPending(data); |
| 120 | + else untrack(() => isPending(data)); |
| 121 | + return v; |
| 122 | + }); |
| 123 | + createRenderEffect( |
| 124 | + () => { |
| 125 | + try { |
| 126 | + return host(); |
| 127 | + } catch { |
| 128 | + return "pending"; |
| 129 | + } |
| 130 | + }, |
| 131 | + () => {} |
| 132 | + ); |
| 133 | + }); |
| 134 | + flush(); |
| 135 | + cur.resolve("a"); |
| 136 | + await settle(); |
| 137 | + setTracked(false); |
| 138 | + flush(); |
| 139 | + const before = runs; |
| 140 | + cur = deferred<string>(); |
| 141 | + setId(2); |
| 142 | + flush(); |
| 143 | + await settle(); |
| 144 | + expect(runs - before).toBe(0); |
| 145 | + cur.resolve("b"); |
| 146 | + await settle(); |
| 147 | + }); |
| 148 | + |
| 149 | + it("an untracked isPending of a source that rejects before resolving answers false instead of erroring", async () => { |
| 150 | + const log: string[] = []; |
| 151 | + let reject!: (e: unknown) => void; |
| 152 | + createRoot(() => { |
| 153 | + const a = createMemo( |
| 154 | + () => |
| 155 | + new Promise<string>((_, r) => { |
| 156 | + reject = r; |
| 157 | + }) |
| 158 | + ); |
| 159 | + const probe = createMemo(() => untrack(() => isPending(() => a()))); |
| 160 | + createRenderEffect( |
| 161 | + () => { |
| 162 | + try { |
| 163 | + return String(probe()); |
| 164 | + } catch (e) { |
| 165 | + return `ERR:${(e as Error)?.message ?? ""}`; |
| 166 | + } |
| 167 | + }, |
| 168 | + v => { |
| 169 | + log.push(v); |
| 170 | + } |
| 171 | + ); |
| 172 | + }); |
| 173 | + flush(); |
| 174 | + reject(new Error("boom")); |
| 175 | + for (let i = 0; i < 6; i++) await settle(); |
| 176 | + await new Promise(r => setTimeout(r, 20)); |
| 177 | + flush(); |
| 178 | + expect(log.at(-1)).toBe("false"); |
| 179 | + }); |
| 180 | + |
| 181 | + it("A10: an untracked isPending reader never pairs pending with the fresh value", async () => { |
| 182 | + const log: string[] = []; |
| 183 | + let setX!: (v: number) => void; |
| 184 | + let cur = deferred<string>(); |
| 185 | + const gate = deferred<void>(); |
| 186 | + createRoot(() => { |
| 187 | + const [x, set] = createSignal(1); |
| 188 | + setX = set; |
| 189 | + const m = createMemo(() => { |
| 190 | + const v = x(); |
| 191 | + return cur.promise.then(s => `${s}-${v}`); |
| 192 | + }); |
| 193 | + const pair = () => |
| 194 | + `[${untrack(() => isPending(() => m()))}, ${(() => { |
| 195 | + try { |
| 196 | + return m(); |
| 197 | + } catch { |
| 198 | + return "THROWN"; |
| 199 | + } |
| 200 | + })()}]`; |
| 201 | + const host = createMemo(pair); |
| 202 | + createRenderEffect( |
| 203 | + () => { |
| 204 | + try { |
| 205 | + return host(); |
| 206 | + } catch { |
| 207 | + return "THROWN"; |
| 208 | + } |
| 209 | + }, |
| 210 | + v => { |
| 211 | + log.push(`memo:${v}`); |
| 212 | + } |
| 213 | + ); |
| 214 | + createRenderEffect(pair, v => { |
| 215 | + log.push(`render:${v}`); |
| 216 | + }); |
| 217 | + createEffect(pair, v => { |
| 218 | + log.push(`user:${v}`); |
| 219 | + }); |
| 220 | + }); |
| 221 | + flush(); |
| 222 | + cur.resolve("data"); |
| 223 | + await settle(); |
| 224 | + |
| 225 | + const act = action(function* () { |
| 226 | + cur = deferred<string>(); |
| 227 | + setX(2); |
| 228 | + yield gate.promise; |
| 229 | + }); |
| 230 | + act(); |
| 231 | + flush(); |
| 232 | + await settle(); |
| 233 | + cur.resolve("data"); |
| 234 | + await settle(); |
| 235 | + gate.resolve(); |
| 236 | + await settle(); |
| 237 | + await settle(); |
| 238 | + |
| 239 | + expect(log.filter(v => v.includes("true, data-2"))).toEqual([]); |
| 240 | + expect(log.slice(-3).sort()).toEqual([ |
| 241 | + "memo:[false, data-2]", |
| 242 | + "render:[false, data-2]", |
| 243 | + "user:[false, data-2]" |
| 244 | + ]); |
| 245 | + }); |
| 246 | +}); |
0 commit comments