|
| 1 | +/** |
| 2 | + * #4377 (D-41, F-6): a memo over `latest(m)` shows `m`'s error as a leaf |
| 3 | + * `latest(m)` does. `m` is sync; the write that makes it throw is staged with |
| 4 | + * the flush, and `latest()` throws the staged error ahead of its commit (the |
| 5 | + * verdict lane's work, shown now). The render effect reading the memo held |
| 6 | + * the error before `m` did, so `<Errored>`, which waits on what threw, let it |
| 7 | + * go as settled; at the commit the leaf re-derived and reached the boundary |
| 8 | + * again, the memo's reader did not (an equal status re-notifies nobody) and |
| 9 | + * kept `m`'s last value. With a held write the same: the leaf shows the |
| 10 | + * error at the reveal, the derivation never did. Fixed by #4364's `prune` |
| 11 | + * change (#4387): a staged error on an error boundary's source keeps the |
| 12 | + * boundary waiting, so the commit that publishes it finds the boundary still |
| 13 | + * waiting on `m`. These pin it. |
| 14 | + */ |
| 15 | +import { describe, expect, it } from "vitest"; |
| 16 | +import { |
| 17 | + action, |
| 18 | + createErrorBoundary, |
| 19 | + createMemo, |
| 20 | + createRenderEffect, |
| 21 | + createRoot, |
| 22 | + createSignal, |
| 23 | + flush, |
| 24 | + latest |
| 25 | +} from "../src/index.js"; |
| 26 | + |
| 27 | +const tick = async () => { |
| 28 | + await new Promise(r => setTimeout(r, 0)); |
| 29 | + flush(); |
| 30 | +}; |
| 31 | + |
| 32 | +type Shape = "leaf" | "derive" | "derive2" | "plain"; |
| 33 | + |
| 34 | +function scene(shape: Shape) { |
| 35 | + const [a, setA] = createSignal(0); |
| 36 | + let screen: unknown; |
| 37 | + createRoot(() => { |
| 38 | + const m = createMemo(() => { |
| 39 | + if (a() === 1) throw new Error("boom"); |
| 40 | + return a(); |
| 41 | + }); |
| 42 | + const d = createMemo(() => latest(m)); |
| 43 | + const d2 = createMemo(() => d()); |
| 44 | + const read = { leaf: () => latest(m), derive: d, derive2: d2, plain: m }[shape]; |
| 45 | + // <Errored fallback="[ERR]"><span>{read()}</span></Errored> |
| 46 | + const out = createErrorBoundary( |
| 47 | + () => { |
| 48 | + createRenderEffect(read, v => void (screen = v)); |
| 49 | + return "content"; |
| 50 | + }, |
| 51 | + () => "[ERR]" |
| 52 | + ); |
| 53 | + createRenderEffect(out, v => void (v === "[ERR]" && (screen = v))); |
| 54 | + }); |
| 55 | + flush(); |
| 56 | + expect(screen).toBe(0); |
| 57 | + return { setA, screen: () => screen }; |
| 58 | +} |
| 59 | + |
| 60 | +describe("a memo over latest(m) shows m's sync error as the leaf does (#4377)", () => { |
| 61 | + for (const shape of ["leaf", "derive", "derive2", "plain"] as const) { |
| 62 | + it(`${shape}: a plain write`, () => { |
| 63 | + const s = scene(shape); |
| 64 | + s.setA(1); |
| 65 | + flush(); |
| 66 | + expect(s.screen()).toBe("[ERR]"); |
| 67 | + }); |
| 68 | + |
| 69 | + it(`${shape}: a held write shows the error at the reveal`, async () => { |
| 70 | + const s = scene(shape); |
| 71 | + let release!: () => void; |
| 72 | + action(function* () { |
| 73 | + s.setA(1); |
| 74 | + yield new Promise<void>(r => (release = r)); |
| 75 | + })(); |
| 76 | + flush(); |
| 77 | + await tick(); |
| 78 | + expect(s.screen()).toBe(0); |
| 79 | + release(); |
| 80 | + await tick(); |
| 81 | + await tick(); |
| 82 | + expect(s.screen()).toBe("[ERR]"); |
| 83 | + }); |
| 84 | + } |
| 85 | +}); |
0 commit comments