|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { createMemo, createRenderEffect, createRoot, createSignal, flush } from "../src/index.js"; |
| 3 | + |
| 4 | +// Manual clock (see async-chain-supersession.test.ts). |
| 5 | +let now = 0; |
| 6 | +let timers: { at: number; run: () => void }[] = []; |
| 7 | +function delay<T>(ms: number, value?: T): Promise<T> { |
| 8 | + return new Promise<T>(r => timers.push({ at: now + ms, run: () => r(value as T) })); |
| 9 | +} |
| 10 | +async function settle() { |
| 11 | + for (let r = 0; r < 3; r++) { |
| 12 | + for (let i = 0; i < 10; i++) await Promise.resolve(); |
| 13 | + flush(); |
| 14 | + } |
| 15 | +} |
| 16 | +async function advanceTo(t: number) { |
| 17 | + while (true) { |
| 18 | + timers.sort((a, b) => a.at - b.at); |
| 19 | + const next = timers[0]; |
| 20 | + if (!next || next.at > t) break; |
| 21 | + timers.shift(); |
| 22 | + now = next.at; |
| 23 | + next.run(); |
| 24 | + await settle(); |
| 25 | + } |
| 26 | + now = t; |
| 27 | + await settle(); |
| 28 | +} |
| 29 | +function reset() { |
| 30 | + now = 0; |
| 31 | + timers = []; |
| 32 | +} |
| 33 | +function frames(log: string[], when: number[]): string[] { |
| 34 | + const byTime = new Map<number, string[]>(); |
| 35 | + log.forEach((v, i) => (byTime.get(when[i]) ?? byTime.set(when[i], []).get(when[i])!).push(v)); |
| 36 | + return [...byTime].map(([t, vs]) => `${t}: ${vs.sort().join(" | ")}`); |
| 37 | +} |
| 38 | +function text(fn: () => string, log: string[], when: number[]) { |
| 39 | + let last: string | undefined; |
| 40 | + createRenderEffect(fn, v => { |
| 41 | + if (v !== last) { |
| 42 | + last = v; |
| 43 | + log.push(v); |
| 44 | + when.push(now); |
| 45 | + } |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +// #3461: `selected = async () => (b() ? b() : a())`. `setB(1)` is held by `slow`; |
| 50 | +// selected's held pass reads only `b` and its flight lands into the hold. The |
| 51 | +// landing used to trim `a` at once, so the later mainline `setA(1)` never |
| 52 | +// reached selected: `A: 1` committed beside `Selected: 0` while `B` still read |
| 53 | +// 0. A30: the landed value is staged, the committed 0 still derives from `a`. |
| 54 | +async function scenario(asyncSelected: boolean) { |
| 55 | + reset(); |
| 56 | + const log: string[] = []; |
| 57 | + const when: number[] = []; |
| 58 | + let setA!: (v: number) => void; |
| 59 | + let setB!: (v: number) => void; |
| 60 | + createRoot(() => { |
| 61 | + const [a, sA] = createSignal(0); |
| 62 | + const [b, sB] = createSignal(0); |
| 63 | + setA = sA; |
| 64 | + setB = sB; |
| 65 | + const slow = createMemo(() => delay(2000, b())); |
| 66 | + const selected = asyncSelected |
| 67 | + ? createMemo(async () => (b() ? b() : a())) |
| 68 | + : createMemo(() => (b() ? b() : a())); |
| 69 | + text(() => `A: ${a()}`, log, when); |
| 70 | + text(() => `B: ${b()}`, log, when); |
| 71 | + text(() => `Slow: ${slow()}`, log, when); |
| 72 | + text(() => `Selected: ${selected()}`, log, when); |
| 73 | + }); |
| 74 | + flush(); |
| 75 | + await settle(); |
| 76 | + await advanceTo(3000); |
| 77 | + setB(1); |
| 78 | + await settle(); |
| 79 | + await advanceTo(3500); |
| 80 | + setA(1); |
| 81 | + await settle(); |
| 82 | + await advanceTo(6000); |
| 83 | + return frames(log, when); |
| 84 | +} |
| 85 | + |
| 86 | +describe("async memo landing keeps the committed frame's dependencies", () => { |
| 87 | + it("A30 / #3461 a held async landing does not trim the input its committed value derives from", async () => { |
| 88 | + // The `a` write (3500) reaches selected through the dependency its |
| 89 | + // committed value still has, and joins the hold through selected's |
| 90 | + // stamp: one frame when slow lands, never `A: 1` beside `Selected: 0`. |
| 91 | + // (The async memo's first landing is held with the mount's slow flight |
| 92 | + // and reveals with it at 2000; the sync control publishes it at 0.) |
| 93 | + expect(await scenario(true)).toEqual([ |
| 94 | + "0: A: 0 | B: 0", |
| 95 | + "2000: Selected: 0 | Slow: 0", |
| 96 | + "5000: A: 1 | B: 1 | Selected: 1 | Slow: 1" |
| 97 | + ]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("A30 / #3410 sync control: the same conditional as a plain memo", async () => { |
| 101 | + expect(await scenario(false)).toEqual([ |
| 102 | + "0: A: 0 | B: 0 | Selected: 0", |
| 103 | + "2000: Slow: 0", |
| 104 | + "5000: A: 1 | B: 1 | Selected: 1 | Slow: 1" |
| 105 | + ]); |
| 106 | + }); |
| 107 | +}); |
0 commit comments