|
| 1 | +/** |
| 2 | + * #4284 — `latest()` tears ahead of the screen, and the frame does not depend |
| 3 | + * on how reads are grouped into render effects. |
| 4 | + * |
| 5 | + * An action writes `x` (a plain signal, or an optimistic guess) and yields a |
| 6 | + * promise. A render effect shows `${x()} ${derived()} / ${latest(x)}`, either |
| 7 | + * as one effect or split into `${x()} ${derived()} /` and `${latest(x)}`. |
| 8 | + * |
| 9 | + * - held write: `1 / 5` either way; |
| 10 | + * - guess, no downstream async: `5 / 5` either way; |
| 11 | + * - guess with a downstream async memo (`derived`, the lane held on it): |
| 12 | + * `1 x1 / 5` either way — the render effect reads the guess as the screen |
| 13 | + * (committed) beside `latest(guess)` — and `5 x5 / 5` at the lane's |
| 14 | + * landing. The guess does not reveal before `derived(5)` lands. |
| 15 | + * |
| 16 | + * Before, the combined effect entered the lane on its `x()` read and parked |
| 17 | + * with it, freezing the `latest(x)` beside it at `1 x1 / 1`. |
| 18 | + */ |
| 19 | +import { describe, expect, it } from "vitest"; |
| 20 | +import { |
| 21 | + action, |
| 22 | + createMemo, |
| 23 | + createOptimistic, |
| 24 | + createRenderEffect, |
| 25 | + createRoot, |
| 26 | + createSignal, |
| 27 | + flush, |
| 28 | + latest |
| 29 | +} from "../src/index.js"; |
| 30 | + |
| 31 | +type Kind = "held" | "opt" | "lane"; |
| 32 | +type Mode = "one" | "split"; |
| 33 | +type Order = "plain-first" | "latest-first"; |
| 34 | + |
| 35 | +async function tick() { |
| 36 | + for (let i = 0; i < 3; i++) { |
| 37 | + await new Promise(r => setTimeout(r, 0)); |
| 38 | + flush(); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function setup(kind: Kind, mode: Mode, order: Order = "plain-first") { |
| 43 | + let resolveDerived: (() => void) | undefined; |
| 44 | + let resolveAction!: () => void; |
| 45 | + let attempt!: () => Promise<void>; |
| 46 | + let screen = ""; |
| 47 | + let a = ""; |
| 48 | + let b = ""; |
| 49 | + const dispose = createRoot(d => { |
| 50 | + let x: () => number; |
| 51 | + let set: (v: number) => void; |
| 52 | + if (kind === "held") [x, set] = createSignal(1); |
| 53 | + else { |
| 54 | + const [base] = createSignal(1); |
| 55 | + [x, set] = createOptimistic(() => base()); |
| 56 | + } |
| 57 | + const derived = |
| 58 | + kind === "lane" |
| 59 | + ? createMemo(() => |
| 60 | + x() === 1 ? "x1" : new Promise<string>(r => (resolveDerived = () => r("x" + x()))) |
| 61 | + ) |
| 62 | + : () => "-"; |
| 63 | + attempt = action(function* () { |
| 64 | + set(5); |
| 65 | + yield new Promise<void>(r => (resolveAction = r)); |
| 66 | + }); |
| 67 | + if (mode === "one") |
| 68 | + createRenderEffect( |
| 69 | + () => { |
| 70 | + if (order === "latest-first") { |
| 71 | + const l = latest(x); |
| 72 | + return `${x()} ${derived()} / ${l}`; |
| 73 | + } |
| 74 | + return `${x()} ${derived()} / ${latest(x)}`; |
| 75 | + }, |
| 76 | + v => void (screen = v) |
| 77 | + ); |
| 78 | + else { |
| 79 | + createRenderEffect( |
| 80 | + () => `${x()} ${derived()} /`, |
| 81 | + v => void (screen = `${(a = v)} ${b}`) |
| 82 | + ); |
| 83 | + createRenderEffect( |
| 84 | + () => latest(x), |
| 85 | + v => void (screen = `${a} ${(b = String(v))}`) |
| 86 | + ); |
| 87 | + } |
| 88 | + return d; |
| 89 | + }); |
| 90 | + return { |
| 91 | + dispose, |
| 92 | + attempt, |
| 93 | + screen: () => screen, |
| 94 | + resolveDerived: () => resolveDerived!(), |
| 95 | + resolveAction: () => resolveAction() |
| 96 | + }; |
| 97 | +} |
| 98 | + |
| 99 | +describe("#4284 latest() tears ahead of the hold however reads are grouped", () => { |
| 100 | + for (const mode of ["one", "split"] as const) { |
| 101 | + it(`held write (${mode}): the screen beside latest — 1 - / 5`, async () => { |
| 102 | + const t = setup("held", mode); |
| 103 | + flush(); |
| 104 | + await tick(); |
| 105 | + expect(t.screen()).toBe("1 - / 1"); |
| 106 | + void t.attempt(); |
| 107 | + flush(); |
| 108 | + await tick(); |
| 109 | + expect(t.screen()).toBe("1 - / 5"); |
| 110 | + t.resolveAction(); |
| 111 | + await tick(); |
| 112 | + expect(t.screen()).toBe("5 - / 5"); |
| 113 | + t.dispose(); |
| 114 | + }); |
| 115 | + |
| 116 | + it(`guess, no downstream async (${mode}): the lane shows — 5 - / 5`, async () => { |
| 117 | + const t = setup("opt", mode); |
| 118 | + flush(); |
| 119 | + await tick(); |
| 120 | + void t.attempt(); |
| 121 | + flush(); |
| 122 | + await tick(); |
| 123 | + expect(t.screen()).toBe("5 - / 5"); |
| 124 | + t.dispose(); |
| 125 | + }); |
| 126 | + |
| 127 | + it(`guess with a downstream async memo (${mode}): 1 x1 / 5, then 5 x5 / 5 at the landing`, async () => { |
| 128 | + const t = setup("lane", mode); |
| 129 | + flush(); |
| 130 | + await tick(); |
| 131 | + expect(t.screen()).toBe("1 x1 / 1"); |
| 132 | + void t.attempt(); |
| 133 | + flush(); |
| 134 | + await tick(); |
| 135 | + // The lane holds on derived(5): the guess does not reveal; latest tears. |
| 136 | + expect(t.screen()).toBe("1 x1 / 5"); |
| 137 | + t.resolveDerived(); |
| 138 | + await tick(); |
| 139 | + expect(t.screen()).toBe("5 x5 / 5"); |
| 140 | + t.dispose(); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + it("guess with a downstream async memo, latest() read first in the one effect: 1 x1 / 5, then 5 x5 / 5", async () => { |
| 145 | + const t = setup("lane", "one", "latest-first"); |
| 146 | + flush(); |
| 147 | + await tick(); |
| 148 | + void t.attempt(); |
| 149 | + flush(); |
| 150 | + await tick(); |
| 151 | + expect(t.screen()).toBe("1 x1 / 5"); |
| 152 | + t.resolveDerived(); |
| 153 | + await tick(); |
| 154 | + expect(t.screen()).toBe("5 x5 / 5"); |
| 155 | + t.dispose(); |
| 156 | + }); |
| 157 | + |
| 158 | + it("guess, no downstream async, one effect: one pass for the guess (no stale re-run)", async () => { |
| 159 | + let passes = 0; |
| 160 | + let attempt!: () => Promise<void>; |
| 161 | + const seen: string[] = []; |
| 162 | + const dispose = createRoot(d => { |
| 163 | + const [base] = createSignal(1); |
| 164 | + const [x, set] = createOptimistic(() => base()); |
| 165 | + attempt = action(function* () { |
| 166 | + set(5); |
| 167 | + yield new Promise<void>(() => {}); |
| 168 | + }); |
| 169 | + createRenderEffect( |
| 170 | + () => (passes++, `${x()} / ${latest(x)}`), |
| 171 | + v => void seen.push(v) |
| 172 | + ); |
| 173 | + return d; |
| 174 | + }); |
| 175 | + flush(); |
| 176 | + void attempt(); |
| 177 | + flush(); |
| 178 | + await tick(); |
| 179 | + expect(seen).toEqual(["1 / 1", "5 / 5"]); |
| 180 | + expect(passes).toBe(2); |
| 181 | + dispose(); |
| 182 | + }); |
| 183 | +}); |
0 commit comments