|
| 1 | +/** |
| 2 | + * S-46 (D-27, D-37; ruled 2026-10-30): the content a display-ahead `Show` |
| 3 | + * opens reads the screen, whoever opened the hold. |
| 4 | + * |
| 5 | + * A plain write to `id` is held because an async memo over it is on screen. |
| 6 | + * `<Show when={latest(id)}>` opens at once — `latest` is the read that asks |
| 7 | + * ahead — but a plain `id()` inside the branch is an independent read: it |
| 8 | + * means the same thing as the `<p>{id()}</p>` outside, the committed value. |
| 9 | + * The branch's first pass read the staging under a display-ahead reader, so |
| 10 | + * it is born held (D-27): it does not attach until the landing, and the |
| 11 | + * screen keeps the fallback. `latest(id)` inside shows `1` now; a `Loading` |
| 12 | + * inside shows its fallback now (S-31's form). Never `0` outside beside |
| 13 | + * `selected: 1` inside — two plain reads of one signal disagreeing on one |
| 14 | + * screen. |
| 15 | + * |
| 16 | + * S-31 (`verdict-mount-first-pass-3851`) pins the action + `Loading` form. |
| 17 | + * This is the plain-write, no-boundary form — "actions aren't special; they |
| 18 | + * are a way to extend the hold" — pinned so the rule's reach is explicit. |
| 19 | + */ |
| 20 | +import { describe, expect, it } from "vitest"; |
| 21 | +import { |
| 22 | + createLoadingBoundary, |
| 23 | + createMemo, |
| 24 | + createRenderEffect, |
| 25 | + createRoot, |
| 26 | + createSignal, |
| 27 | + flush, |
| 28 | + latest, |
| 29 | + untrack |
| 30 | +} from "../src/index.js"; |
| 31 | + |
| 32 | +const settle = async () => { |
| 33 | + for (let i = 0; i < 3; i++) { |
| 34 | + await new Promise(r => setTimeout(r, 0)); |
| 35 | + flush(); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +type Content = "plain" | "latest" | "loading"; |
| 40 | + |
| 41 | +async function run(content: Content) { |
| 42 | + const [id, setId] = createSignal(0); |
| 43 | + const gates: ((v: string) => void)[] = []; |
| 44 | + const fetchDetail = (v: number) => new Promise<string>(r => gates.push(() => r(`d${v}`))); |
| 45 | + let screenId: unknown; |
| 46 | + let slot: unknown; |
| 47 | + let screenDetail: unknown; |
| 48 | + createRoot(() => { |
| 49 | + // <p>{id()}</p> |
| 50 | + createRenderEffect(id, v => { |
| 51 | + screenId = v; |
| 52 | + }); |
| 53 | + // <p>{detail()}</p> — the async over `id` that holds a write to it |
| 54 | + const detail = createMemo(() => fetchDetail(id())); |
| 55 | + createRenderEffect(detail, v => { |
| 56 | + screenDetail = v; |
| 57 | + }); |
| 58 | + // <Show when={latest(id)} fallback="none">…</Show> — the branch is a |
| 59 | + // mount: a render effect created under the display-ahead reader, as |
| 60 | + // `Show` + `insert` create one for the child. |
| 61 | + const when = createMemo(() => latest(id) > 0); |
| 62 | + const children = createMemo(() => |
| 63 | + when() |
| 64 | + ? untrack(() => { |
| 65 | + if (content === "loading") |
| 66 | + return createLoadingBoundary( |
| 67 | + () => `selected: ${id()}`, |
| 68 | + () => "fallback" |
| 69 | + ); |
| 70 | + createRenderEffect( |
| 71 | + () => `selected: ${content === "latest" ? latest(id) : id()}`, |
| 72 | + v => { |
| 73 | + slot = v; |
| 74 | + } |
| 75 | + ); |
| 76 | + return "opened"; |
| 77 | + }) |
| 78 | + : "none" |
| 79 | + ); |
| 80 | + createRenderEffect( |
| 81 | + () => { |
| 82 | + const c = children(); |
| 83 | + return typeof c === "function" ? c() : c; |
| 84 | + }, |
| 85 | + v => { |
| 86 | + // the mounted leaf writes the slot itself; "opened" is the branch |
| 87 | + // existing with its leaf not yet attached |
| 88 | + if (v !== "opened") slot = v; |
| 89 | + } |
| 90 | + ); |
| 91 | + }); |
| 92 | + flush(); |
| 93 | + gates.shift()!("d0"); |
| 94 | + await settle(); |
| 95 | + const screens: unknown[] = [[screenId, slot, screenDetail]]; |
| 96 | + |
| 97 | + setId(1); |
| 98 | + flush(); |
| 99 | + await settle(); |
| 100 | + screens.push([screenId, slot, screenDetail]); |
| 101 | + |
| 102 | + gates.shift()!("d1"); |
| 103 | + await settle(); |
| 104 | + screens.push([screenId, slot, screenDetail]); |
| 105 | + return screens; |
| 106 | +} |
| 107 | + |
| 108 | +describe("S-46: the content a display-ahead Show opens reads the screen (plain-write hold)", () => { |
| 109 | + it("plain `id()` inside: the branch is born held, the fallback stays until the landing", async () => { |
| 110 | + expect(await run("plain")).toEqual([ |
| 111 | + [0, "none", "d0"], |
| 112 | + [0, "none", "d0"], |
| 113 | + [1, "selected: 1", "d1"] |
| 114 | + ]); |
| 115 | + }); |
| 116 | + |
| 117 | + it("`latest(id)` inside: the branch shows the held value now", async () => { |
| 118 | + expect(await run("latest")).toEqual([ |
| 119 | + [0, "none", "d0"], |
| 120 | + [0, "selected: 1", "d0"], |
| 121 | + [1, "selected: 1", "d1"] |
| 122 | + ]); |
| 123 | + }); |
| 124 | + |
| 125 | + it("a `Loading` inside: its fallback now, the content at the landing (S-31's form)", async () => { |
| 126 | + expect(await run("loading")).toEqual([ |
| 127 | + [0, "none", "d0"], |
| 128 | + [0, "fallback", "d0"], |
| 129 | + [1, "selected: 1", "d1"] |
| 130 | + ]); |
| 131 | + }); |
| 132 | +}); |
0 commit comments