|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createMemo, |
| 4 | + createRenderEffect, |
| 5 | + createRoot, |
| 6 | + createSignal, |
| 7 | + flush, |
| 8 | + isPending |
| 9 | +} from "../src/index.js"; |
| 10 | + |
| 11 | +let now = 0; |
| 12 | +let timers: { at: number; run: () => void }[] = []; |
| 13 | +function delay<T>(ms: number, value?: T): Promise<T> { |
| 14 | + return new Promise<T>(r => timers.push({ at: now + ms, run: () => r(value as T) })); |
| 15 | +} |
| 16 | +async function settle() { |
| 17 | + for (let r = 0; r < 3; r++) { |
| 18 | + for (let i = 0; i < 10; i++) await Promise.resolve(); |
| 19 | + flush(); |
| 20 | + } |
| 21 | +} |
| 22 | +async function advanceTo(t: number) { |
| 23 | + while (true) { |
| 24 | + timers.sort((a, b) => a.at - b.at); |
| 25 | + const next = timers[0]; |
| 26 | + if (!next || next.at > t) break; |
| 27 | + timers.shift(); |
| 28 | + now = next.at; |
| 29 | + next.run(); |
| 30 | + await settle(); |
| 31 | + } |
| 32 | + now = t; |
| 33 | + await settle(); |
| 34 | +} |
| 35 | +function frames(log: string[], when: number[]): string[] { |
| 36 | + const byTime = new Map<number, string[]>(); |
| 37 | + log.forEach((v, i) => (byTime.get(when[i]) ?? byTime.set(when[i], []).get(when[i])!).push(v)); |
| 38 | + return [...byTime].map(([t, vs]) => `${t}: ${vs.sort().join(" | ")}`); |
| 39 | +} |
| 40 | +function text(fn: () => string, log: string[], when: number[]) { |
| 41 | + let last: string | undefined; |
| 42 | + createRenderEffect(fn, v => { |
| 43 | + if (v !== last) { |
| 44 | + last = v; |
| 45 | + log.push(v); |
| 46 | + when.push(now); |
| 47 | + } |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +async function scenario(variant: "issue" | "no-pending" | "getter") { |
| 52 | + now = 0; |
| 53 | + timers = []; |
| 54 | + const log: string[] = []; |
| 55 | + const when: number[] = []; |
| 56 | + let setCount!: (v: number) => void; |
| 57 | + createRoot(() => { |
| 58 | + const [count, sC] = createSignal(0); |
| 59 | + setCount = sC; |
| 60 | + const slow = createMemo(() => delay(1000, count())); |
| 61 | + const fast = createMemo(async () => count()); |
| 62 | + const copy = variant === "getter" ? () => slow() : createMemo(() => slow()); |
| 63 | + if (variant !== "no-pending") |
| 64 | + text(() => `Pending: ${isPending(() => [fast(), copy()])}`, log, when); |
| 65 | + text(() => `Fast: ${fast()}`, log, when); |
| 66 | + text(() => `Slow: ${copy()}`, log, when); |
| 67 | + }); |
| 68 | + flush(); |
| 69 | + await settle(); |
| 70 | + await advanceTo(2000); |
| 71 | + setCount(1); |
| 72 | + await settle(); |
| 73 | + await advanceTo(5000); |
| 74 | + return frames(log, when); |
| 75 | +} |
| 76 | + |
| 77 | +describe("combined isPending read across two async memos (#3442)", () => { |
| 78 | + // The write (2000) starts two flights; `fast` lands in a microtask and is |
| 79 | + // held with `slow` (1000ms). The probe effect carries the companion lane of |
| 80 | + // the pending signals it reads, and its pull of `copy` (a sync memo over |
| 81 | + // `slow`) used to run under that lane, where a pending node on no lane |
| 82 | + // serves its committed value instead of throwing: `copy` published a |
| 83 | + // stale settled `0`, dropped its pending status, and its readers stopped |
| 84 | + // holding `slow` — the hold released with `slow` still in flight |
| 85 | + // (`Fast: 1` beside `Slow: 0`, `Pending: false`). A memo now computes |
| 86 | + // under its own lane posture only. |
| 87 | + const atomic = [ |
| 88 | + "1000: Fast: 0 | Pending: false | Slow: 0", |
| 89 | + "2000: Pending: true", |
| 90 | + "3000: Fast: 1 | Pending: false | Slow: 1" |
| 91 | + ]; |
| 92 | + it("A31 / #3442 isPending(() => [fast(), copy()]) with copy a sync memo holds both flights", async () => { |
| 93 | + expect(await scenario("issue")).toEqual(atomic); |
| 94 | + }); |
| 95 | + it("control: copy is a plain getter", async () => { |
| 96 | + expect(await scenario("getter")).toEqual(atomic); |
| 97 | + }); |
| 98 | + it("control: no pending read", async () => { |
| 99 | + expect(await scenario("no-pending")).toEqual([ |
| 100 | + "1000: Fast: 0 | Slow: 0", |
| 101 | + "3000: Fast: 1 | Slow: 1" |
| 102 | + ]); |
| 103 | + }); |
| 104 | +}); |
0 commit comments