|
| 1 | +// #4341: a memo born in a branch that a staged or held condition opened has |
| 2 | +// no screen of its own — the branch exists only in the world that opened it. |
| 3 | +// Its first pass reads that world like any derivation of it, whether or not |
| 4 | +// it enters a `latest()`/`isPending()` window, and in whatever order: a |
| 5 | +// verdict window must not turn the branch's own first pass into a reader of |
| 6 | +// the screen, where its condition is still closed (the <Match> accessor then |
| 7 | +// saw its branch as unmounted and threw, halting the app). |
| 8 | +import { |
| 9 | + createLoadingBoundary, |
| 10 | + createMemo, |
| 11 | + createRenderEffect, |
| 12 | + createRoot, |
| 13 | + createSignal, |
| 14 | + flush, |
| 15 | + isPending, |
| 16 | + latest, |
| 17 | + untrack, |
| 18 | + type Accessor |
| 19 | +} from "../src/index.js"; |
| 20 | + |
| 21 | +const settle = async () => { |
| 22 | + for (let i = 0; i < 3; i++) { |
| 23 | + await Promise.resolve(); |
| 24 | + flush(); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +type Read = (zone: Accessor<string>, format: Accessor<string>, open: () => string) => string; |
| 29 | + |
| 30 | +const reads: [string, Read][] = [ |
| 31 | + ["latest(format) first", (zone, format, open) => `${latest(format)}:${zone()}:${open()}`], |
| 32 | + ["latest(format) between", (zone, format, open) => `${zone()}:${latest(format)}:${open()}`], |
| 33 | + ["latest(zone)", (zone, format, open) => `${format()}:${latest(zone)}:${open()}`], |
| 34 | + [ |
| 35 | + "isPending(format)", |
| 36 | + (zone, format, open) => `${isPending(format) ? "?" : format()}:${zone()}:${open()}` |
| 37 | + ], |
| 38 | + ["plain reads (control)", (zone, format, open) => `${format()}:${zone()}:${open()}`] |
| 39 | +]; |
| 40 | + |
| 41 | +/** |
| 42 | + * `zone` gates a branch (as `<Match when={zone() === "ready"}>`), and an |
| 43 | + * async memo under its own boundary re-asks on `zone`, so the frame that |
| 44 | + * flips it has a transaction. |
| 45 | + * - "staged": one write opens the branch; the selecting chain is deep |
| 46 | + * enough that it runs after the frame has its transaction, so the |
| 47 | + * selection reads a staging of that flush. |
| 48 | + * - "joined": the write is held first; a second, unrelated write re-runs |
| 49 | + * the selection, which reads the held condition (it joins the hold) and |
| 50 | + * opens the branch. |
| 51 | + * The reader is a memo, a render effect, or a memo a plain wrapper memo |
| 52 | + * creates (its creator is not the selecting pass itself). |
| 53 | + */ |
| 54 | +function setup(read: Read, reader: "memo" | "effect" | "nested memo", opener: "staged" | "joined") { |
| 55 | + const [ready, setReady] = createSignal(false); |
| 56 | + const [gate, setGate] = createSignal(opener === "staged"); |
| 57 | + let resolve!: (v: string) => void; |
| 58 | + const screen: Record<string, unknown> = {}; |
| 59 | + const seen: string[] = []; |
| 60 | + createRoot(() => { |
| 61 | + const zone = createMemo(() => (ready() ? "ready" : "loading")); |
| 62 | + const format = createMemo(() => "ru"); |
| 63 | + const data = createMemo(() => { |
| 64 | + if (zone() !== "ready") return "none"; |
| 65 | + return new Promise<string>(r => (resolve = r)); |
| 66 | + }); |
| 67 | + let selected: Accessor<boolean> = createMemo(() => zone() === "ready"); |
| 68 | + for (let i = 0; i < 6; i++) { |
| 69 | + const prev = selected; |
| 70 | + selected = createMemo(() => prev()); |
| 71 | + } |
| 72 | + const sel = selected; |
| 73 | + const show = (label: Accessor<string>) => |
| 74 | + createRenderEffect(label, v => void seen.push((screen.span = v))); |
| 75 | + const branch = createMemo(() => { |
| 76 | + if (!gate() || !sel()) return "closed"; |
| 77 | + return untrack(() => { |
| 78 | + // The narrowed accessor's guard: is this branch still the selected one? |
| 79 | + const open = () => (untrack(sel) ? "sel" : "stale"); |
| 80 | + if (reader === "effect") show(() => read(zone, format, open)); |
| 81 | + else if (reader === "memo") show(createMemo(() => read(zone, format, open))); |
| 82 | + else createMemo(() => show(createMemo(() => read(zone, format, open)))); |
| 83 | + return "open"; |
| 84 | + }); |
| 85 | + }); |
| 86 | + createRenderEffect(branch, v => void (screen.branch = v)); |
| 87 | + createRenderEffect( |
| 88 | + createLoadingBoundary( |
| 89 | + () => data(), |
| 90 | + () => "wait" |
| 91 | + ), |
| 92 | + v => void (screen.data = v) |
| 93 | + ); |
| 94 | + }); |
| 95 | + flush(); |
| 96 | + return { screen, seen, setReady, setGate, resolve: (v: string) => resolve(v) }; |
| 97 | +} |
| 98 | + |
| 99 | +describe("a memo born in a newly opened branch reads the world that opened it (#4341)", () => { |
| 100 | + for (const opener of ["staged", "joined"] as const) |
| 101 | + for (const reader of ["memo", "effect", "nested memo"] as const) |
| 102 | + for (const [name, read] of reads) |
| 103 | + it(`${opener}, ${reader}: ${name}`, async () => { |
| 104 | + const { screen, seen, setReady, setGate, resolve } = setup(read, reader, opener); |
| 105 | + expect(screen).toEqual({ branch: "closed", data: "none" }); |
| 106 | + setReady(true); |
| 107 | + flush(); |
| 108 | + if (opener === "joined") { |
| 109 | + expect(screen).toEqual({ branch: "closed", data: "none" }); |
| 110 | + setGate(true); |
| 111 | + flush(); |
| 112 | + } |
| 113 | + // Held on `data`: the branch has not opened on screen. |
| 114 | + expect(screen.branch).toBe("closed"); |
| 115 | + resolve("data"); |
| 116 | + await settle(); |
| 117 | + const want = name === "latest(format) between" ? "ready:ru:sel" : "ru:ready:sel"; |
| 118 | + expect(screen).toEqual({ branch: "open", span: want, data: "data" }); |
| 119 | + // No pass of the branch's reader saw its branch closed. |
| 120 | + expect(seen.filter(v => v !== want)).toEqual([]); |
| 121 | + }); |
| 122 | +}); |
0 commit comments