|
| 1 | +// #4366: an unowned memo left with no reader goes dormant (its last reader |
| 2 | +// was torn down, or it settled with nothing subscribed). The next read wakes |
| 3 | +// it with a recompute. When that read is an `isPending(m1)` probe, the wake |
| 4 | +// is the memo's own pass (A31), not the probe's: it must read its sources as |
| 5 | +// any pass does. Woken inside the probe window, it read them as the probe |
| 6 | +// does, the committed value, so `m1` derived from the screen under a |
| 7 | +// landing (`undefined + 1`) or from the value before an error recovered. |
| 8 | +import { expect, it } from "vitest"; |
| 9 | +import { |
| 10 | + createErrorBoundary, |
| 11 | + createLoadingBoundary, |
| 12 | + createMemo, |
| 13 | + createRenderEffect, |
| 14 | + createRoot, |
| 15 | + createSignal, |
| 16 | + flush, |
| 17 | + isPending, |
| 18 | + latest |
| 19 | +} from "../src/index.js"; |
| 20 | + |
| 21 | +const tick = async () => { |
| 22 | + for (let i = 0; i < 8; i++) await Promise.resolve(); |
| 23 | + flush(); |
| 24 | +}; |
| 25 | + |
| 26 | +// Show: a condition memo, and a branch created when it turns truthy. |
| 27 | +function show(when: () => unknown, content: () => () => string, fallback = "[F]") { |
| 28 | + const cond = createMemo(() => !!when()); |
| 29 | + const branch = createMemo(() => (cond() ? content() : null)); |
| 30 | + return () => branch()?.() ?? fallback; |
| 31 | +} |
| 32 | + |
| 33 | +function mount(view: () => unknown) { |
| 34 | + let out: unknown; |
| 35 | + createRoot(() => |
| 36 | + createRenderEffect( |
| 37 | + () => { |
| 38 | + const v = view(); |
| 39 | + return typeof v === "function" ? v() : v; |
| 40 | + }, |
| 41 | + v => { |
| 42 | + out = v; |
| 43 | + } |
| 44 | + ) |
| 45 | + ); |
| 46 | + return () => out; |
| 47 | +} |
| 48 | + |
| 49 | +for (const probe of [true, false]) { |
| 50 | + const text = (m1: () => number) => () => |
| 51 | + createMemo(() => `${probe ? String(isPending(m1)) + " " : ""}${String(m1())}`); |
| 52 | + |
| 53 | + it(`an unowned memo over an async source lands its value${probe ? " with an isPending reader" : ""} (#4366)`, async () => { |
| 54 | + let release!: (v: number) => void; |
| 55 | + const m0 = createMemo(() => new Promise<number>(r => (release = r))); |
| 56 | + const m1 = createMemo(() => m0() + 1); |
| 57 | + const out = mount(() => |
| 58 | + createLoadingBoundary( |
| 59 | + () => show(m0, text(m1)), |
| 60 | + () => "[L]" |
| 61 | + ) |
| 62 | + ); |
| 63 | + flush(); |
| 64 | + expect(out()).toBe("[L]"); |
| 65 | + release(2); |
| 66 | + await tick(); |
| 67 | + expect(out()).toBe(probe ? "false 3" : "3"); |
| 68 | + }); |
| 69 | + |
| 70 | + it(`an unowned memo re-derives after its source recovers from an error${probe ? " with an isPending reader" : ""} (#4366)`, () => { |
| 71 | + const [s0, set0] = createSignal(3); |
| 72 | + const [s1, set1] = createSignal(3); |
| 73 | + const m0 = createMemo(() => { |
| 74 | + const v = s1() + s0() + 1; |
| 75 | + if (v % 4 === 2) throw new Error("m0:" + v); |
| 76 | + return v; |
| 77 | + }); |
| 78 | + const m1 = createMemo(() => m0() + 1); |
| 79 | + const out = mount(() => |
| 80 | + createErrorBoundary( |
| 81 | + () => show(() => latest(m0), text(m1)), |
| 82 | + e => `[E ${(e() as Error)?.message}]` |
| 83 | + ) |
| 84 | + ); |
| 85 | + flush(); |
| 86 | + expect(out()).toBe(probe ? "false 8" : "8"); |
| 87 | + set1(2); |
| 88 | + flush(); |
| 89 | + expect(out()).toBe("[E m0:6]"); |
| 90 | + set0(2); |
| 91 | + flush(); |
| 92 | + expect(out()).toBe(probe ? "false 6" : "6"); |
| 93 | + }); |
| 94 | +} |
0 commit comments