|
| 1 | +/** |
| 2 | + * #4203: #4166 carries a verdict lane's mount through nested first passes, |
| 3 | + * so a render effect its content creates is held like a direct read. A |
| 4 | + * plain memo the content creates is not a render effect: one that reads a |
| 5 | + * signal the mount wrote (a staging) and never settles, with no reader, |
| 6 | + * holds nothing and reaches no boundary (Reader). Before, it was held, so |
| 7 | + * `<Show when={latest(gate) ?? gate()}><Loading>…` never revealed. A memo |
| 8 | + * that settles is still held like #4166's render effect. |
| 9 | + */ |
| 10 | +import { describe, expect, it } from "vitest"; |
| 11 | +import { |
| 12 | + action, |
| 13 | + createLoadingBoundary, |
| 14 | + createMemo, |
| 15 | + createRenderEffect, |
| 16 | + createRoot, |
| 17 | + createSignal, |
| 18 | + flush, |
| 19 | + latest, |
| 20 | + untrack |
| 21 | +} from "../src/index.js"; |
| 22 | + |
| 23 | +const settle = async () => { |
| 24 | + for (let i = 0; i < 3; i++) { |
| 25 | + await new Promise(r => setTimeout(r, 0)); |
| 26 | + flush(); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +describe("#4203: an unread pending memo does not hold a verdict-mounted boundary", () => { |
| 31 | + it("the boundary reveals its content", async () => { |
| 32 | + let slot: unknown; |
| 33 | + createRoot(() => { |
| 34 | + const content = () => { |
| 35 | + const [version, setVersion] = createSignal(0, { ownedWrite: true }); |
| 36 | + setVersion(1); |
| 37 | + // Never read; never settles. |
| 38 | + createMemo(() => { |
| 39 | + version(); |
| 40 | + return new Promise(() => {}); |
| 41 | + }); |
| 42 | + return "CHILD"; |
| 43 | + }; |
| 44 | + const gate = createMemo(async () => "x"); |
| 45 | + const outer = createLoadingBoundary( |
| 46 | + () => { |
| 47 | + // <Show when={latest(gate) ?? gate()}> |
| 48 | + const when = createMemo(() => latest(gate) ?? gate()); |
| 49 | + const condition = createMemo(() => !!when()); |
| 50 | + const children = createMemo(() => |
| 51 | + condition() |
| 52 | + ? untrack(() => createLoadingBoundary(content, () => "INNER LOADING")) |
| 53 | + : undefined |
| 54 | + ); |
| 55 | + return () => { |
| 56 | + const c = children(); |
| 57 | + return typeof c === "function" ? c() : c; |
| 58 | + }; |
| 59 | + }, |
| 60 | + () => "OUTER LOADING" |
| 61 | + ); |
| 62 | + createRenderEffect( |
| 63 | + () => { |
| 64 | + let v: any = outer(); |
| 65 | + while (typeof v === "function") v = v(); |
| 66 | + return v; |
| 67 | + }, |
| 68 | + v => { |
| 69 | + slot = v; |
| 70 | + } |
| 71 | + ); |
| 72 | + }); |
| 73 | + flush(); |
| 74 | + await settle(); |
| 75 | + expect(slot).toBe("CHILD"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("a settled memo the content creates is still held (#4166)", async () => { |
| 79 | + const [x, setX] = createSignal(0); |
| 80 | + let screenX: unknown; |
| 81 | + let inner: unknown; |
| 82 | + let slot: unknown; |
| 83 | + createRoot(() => { |
| 84 | + createRenderEffect(x, v => { |
| 85 | + screenX = v; |
| 86 | + }); |
| 87 | + // <Show when={latest(x) > 0}><Loading><p>{double()}</p></Loading></Show> |
| 88 | + const content = () => { |
| 89 | + const double = createMemo(() => x() * 2); |
| 90 | + createRenderEffect(double, v => { |
| 91 | + inner = v; |
| 92 | + }); |
| 93 | + return "p"; |
| 94 | + }; |
| 95 | + const when = createMemo(() => latest(x) > 0); |
| 96 | + const children = createMemo(() => |
| 97 | + when() ? untrack(() => createLoadingBoundary(content, () => "pending")) : "closed" |
| 98 | + ); |
| 99 | + createRenderEffect( |
| 100 | + () => { |
| 101 | + const c = children(); |
| 102 | + return typeof c === "function" ? c() : c; |
| 103 | + }, |
| 104 | + v => { |
| 105 | + slot = v; |
| 106 | + } |
| 107 | + ); |
| 108 | + }); |
| 109 | + flush(); |
| 110 | + let release!: () => void; |
| 111 | + const done = action(function* () { |
| 112 | + setX(1); |
| 113 | + yield new Promise<void>(r => (release = r)); |
| 114 | + })(); |
| 115 | + await settle(); |
| 116 | + expect([screenX, slot, inner]).toEqual([0, "pending", undefined]); |
| 117 | + release(); |
| 118 | + await done; |
| 119 | + await settle(); |
| 120 | + expect([screenX, slot, inner]).toEqual([1, "p", 2]); |
| 121 | + }); |
| 122 | +}); |
0 commit comments