|
| 1 | +/** |
| 2 | + * #3884: isPending/latest on a superseded write must not depend on whether the |
| 3 | + * parked computation also reads them. |
| 4 | + * |
| 5 | + * Under #3774, verdict readers see the committed screen. The "nothing" row is |
| 6 | + * the consistent answer, and every other variant must match it. An outside |
| 7 | + * `isPending(source)` read is true for as long as the superseding write is |
| 8 | + * parked. |
| 9 | + */ |
| 10 | +import { describe, expect, it } from "vitest"; |
| 11 | +import { |
| 12 | + createMemo, |
| 13 | + createRenderEffect, |
| 14 | + createRoot, |
| 15 | + createSignal, |
| 16 | + flush, |
| 17 | + isPending, |
| 18 | + latest, |
| 19 | + untrack |
| 20 | +} from "../src/index.js"; |
| 21 | + |
| 22 | +const tick = () => new Promise(r => setTimeout(r, 0)); |
| 23 | + |
| 24 | +type Extra = |
| 25 | + | "nothing" |
| 26 | + | "untracked latest" |
| 27 | + | "untracked isPending" |
| 28 | + | "tracked latest" |
| 29 | + | "tracked isPending"; |
| 30 | + |
| 31 | +const EXTRAS: Extra[] = [ |
| 32 | + "nothing", |
| 33 | + "untracked latest", |
| 34 | + "untracked isPending", |
| 35 | + "tracked latest", |
| 36 | + "tracked isPending" |
| 37 | +]; |
| 38 | + |
| 39 | +function readExtra(source: () => string, extra: Extra): void { |
| 40 | + if (extra === "untracked latest") untrack(() => latest(source)); |
| 41 | + if (extra === "untracked isPending") untrack(() => isPending(source)); |
| 42 | + if (extra === "tracked latest") latest(source); |
| 43 | + if (extra === "tracked isPending") isPending(source); |
| 44 | +} |
| 45 | + |
| 46 | +type Checkpoint = { |
| 47 | + label: string; |
| 48 | + effect: string; |
| 49 | + isPending: boolean; |
| 50 | + latest?: string; |
| 51 | +}; |
| 52 | + |
| 53 | +/** The value the UI effect is showing at this point in the log. */ |
| 54 | +function effectAt(log: string[], index: number): string { |
| 55 | + let effect = "(none)"; |
| 56 | + for (let i = 0; i <= index; i++) { |
| 57 | + const line = log[i]; |
| 58 | + if (line.startsWith("effect: ")) effect = line.slice("effect: ".length); |
| 59 | + } |
| 60 | + return effect; |
| 61 | +} |
| 62 | + |
| 63 | +function checkpoints(log: string[]): Checkpoint[] { |
| 64 | + const out: Checkpoint[] = []; |
| 65 | + for (let i = 0; i < log.length; i++) { |
| 66 | + const line = log[i]; |
| 67 | + if (!line.startsWith("| ")) continue; |
| 68 | + const pending = /isPending=(true|false)/.exec(line); |
| 69 | + const latestMatch = /latest=([^ ]+)/.exec(line); |
| 70 | + if (!pending) throw new Error(`checkpoint missing isPending: ${line}`); |
| 71 | + out.push({ |
| 72 | + label: line.slice(2, line.indexOf(":")), |
| 73 | + effect: effectAt(log, i), |
| 74 | + isPending: pending[1] === "true", |
| 75 | + ...(latestMatch ? { latest: latestMatch[1] } : {}) |
| 76 | + }); |
| 77 | + } |
| 78 | + return out; |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Narrowed repro (rc.14 comment): the parked memo reads `source` directly and |
| 83 | + * the UI effect reads the verdicts directly. |
| 84 | + * |
| 85 | + * Baseline "nothing": effect `/slow1` when A parks, `/slow2` at B's flush, |
| 86 | + * `-` when B lands; outside isPending true from A parked until B lands. |
| 87 | + */ |
| 88 | +async function runNarrowed(extra: Extra): Promise<string[]> { |
| 89 | + const log: string[] = []; |
| 90 | + const gates: Record<string, PromiseWithResolvers<string>> = {}; |
| 91 | + const gate = (k: string) => (gates[k] ??= Promise.withResolvers<string>()); |
| 92 | + const [source, setSource] = createSignal("/"); |
| 93 | + |
| 94 | + createRoot(() => { |
| 95 | + const data = createMemo(() => { |
| 96 | + const v = source(); |
| 97 | + readExtra(source, extra); |
| 98 | + return v.startsWith("/slow") ? gate(v).promise : v; |
| 99 | + }); |
| 100 | + createRenderEffect(data, () => {}); |
| 101 | + createRenderEffect( |
| 102 | + () => (isPending(source) ? latest(source) : "-"), |
| 103 | + v => void log.push(`effect: ${v}`) |
| 104 | + ); |
| 105 | + }); |
| 106 | + flush(); |
| 107 | + const cp = (label: string) => log.push(`| ${label}: isPending=${isPending(source)}`); |
| 108 | + |
| 109 | + setSource("/slow1"); |
| 110 | + flush(); |
| 111 | + cp("A parked"); |
| 112 | + await tick(); |
| 113 | + setSource("/slow2"); |
| 114 | + flush(); |
| 115 | + cp("B flushed"); |
| 116 | + await tick(); |
| 117 | + cp("B flushed +tick"); |
| 118 | + gates["/slow1"].resolve("one"); |
| 119 | + await tick(); |
| 120 | + cp("A resolved"); |
| 121 | + gates["/slow2"].resolve("two"); |
| 122 | + await tick(); |
| 123 | + cp("B resolved"); |
| 124 | + return log; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Original repro: a `path` memo between source and the parked memo, and the |
| 129 | + * effect behind a `target` memo. |
| 130 | + * |
| 131 | + * Baseline "nothing": effect shows `/slow2` at B's flush; outside isPending |
| 132 | + * true while B is parked. |
| 133 | + */ |
| 134 | +async function runOriginal(extra: Extra): Promise<string[]> { |
| 135 | + const log: string[] = []; |
| 136 | + const gates: Record<string, PromiseWithResolvers<string>> = {}; |
| 137 | + const gate = (k: string) => (gates[k] ??= Promise.withResolvers<string>()); |
| 138 | + const [source, setSource] = createSignal("/"); |
| 139 | + |
| 140 | + createRoot(() => { |
| 141 | + const path = createMemo(() => source()); |
| 142 | + const data = createMemo(() => { |
| 143 | + const v = path(); |
| 144 | + readExtra(source, extra); |
| 145 | + return v.startsWith("/slow") ? gate(v).promise : v; |
| 146 | + }); |
| 147 | + createRenderEffect(data, () => {}); |
| 148 | + const target = createMemo(() => (isPending(source) ? latest(source) : "-")); |
| 149 | + createRenderEffect(target, v => void log.push(`effect: ${v}`)); |
| 150 | + }); |
| 151 | + flush(); |
| 152 | + const cp = (label: string) => |
| 153 | + log.push(`| ${label}: isPending=${isPending(source)} latest=${latest(source)}`); |
| 154 | + |
| 155 | + setSource("/slow1"); |
| 156 | + flush(); |
| 157 | + cp("A parked"); |
| 158 | + await tick(); |
| 159 | + setSource("/slow2"); |
| 160 | + flush(); |
| 161 | + cp("B flushed"); |
| 162 | + await tick(); |
| 163 | + cp("B flushed +tick"); |
| 164 | + gates["/slow1"].resolve("one"); |
| 165 | + await tick(); |
| 166 | + cp("A resolved"); |
| 167 | + gates["/slow2"].resolve("two"); |
| 168 | + await tick(); |
| 169 | + cp("B resolved"); |
| 170 | + return log; |
| 171 | +} |
| 172 | + |
| 173 | +/** What "nothing" shows. Every variant must match this. */ |
| 174 | +const NARROWED: Checkpoint[] = [ |
| 175 | + { label: "A parked", effect: "/slow1", isPending: true }, |
| 176 | + { label: "B flushed", effect: "/slow2", isPending: true }, |
| 177 | + { label: "B flushed +tick", effect: "/slow2", isPending: true }, |
| 178 | + { label: "A resolved", effect: "/slow2", isPending: true }, |
| 179 | + { label: "B resolved", effect: "-", isPending: false } |
| 180 | +]; |
| 181 | + |
| 182 | +const ORIGINAL: Checkpoint[] = [ |
| 183 | + { label: "A parked", effect: "/slow1", isPending: true, latest: "/slow1" }, |
| 184 | + { label: "B flushed", effect: "/slow2", isPending: true, latest: "/slow2" }, |
| 185 | + { label: "B flushed +tick", effect: "/slow2", isPending: true, latest: "/slow2" }, |
| 186 | + { label: "A resolved", effect: "/slow2", isPending: true, latest: "/slow2" }, |
| 187 | + { label: "B resolved", effect: "-", isPending: false, latest: "/slow2" } |
| 188 | +]; |
| 189 | + |
| 190 | +describe("#3884 superseded-transition verdicts", () => { |
| 191 | + describe("narrowed repro", () => { |
| 192 | + for (const extra of EXTRAS) { |
| 193 | + it(`${extra} matches the nothing baseline`, async () => { |
| 194 | + const log = await runNarrowed(extra); |
| 195 | + expect(checkpoints(log), log.join(" ; ")).toEqual(NARROWED); |
| 196 | + }); |
| 197 | + } |
| 198 | + }); |
| 199 | + |
| 200 | + describe("original repro", () => { |
| 201 | + for (const extra of EXTRAS) { |
| 202 | + it(`${extra} matches the nothing baseline`, async () => { |
| 203 | + const log = await runOriginal(extra); |
| 204 | + expect(checkpoints(log), log.join(" ; ")).toEqual(ORIGINAL); |
| 205 | + }); |
| 206 | + } |
| 207 | + }); |
| 208 | +}); |
0 commit comments