|
| 1 | +/** |
| 2 | + * #3835 — a subtree mounted by an optimistic write is the lane's frame |
| 3 | + * (ruling A), all the way down. A memo created by the lane pass used to sit |
| 4 | + * outside the lane while its own body ran, so the render effect it created |
| 5 | + * read the transaction's staged value and was born held: the lane revealed |
| 6 | + * the memo's element with the effect's first run still waiting on the |
| 7 | + * transaction. |
| 8 | + */ |
| 9 | +import { afterEach, describe, expect, it } from "vitest"; |
| 10 | +import { |
| 11 | + action, |
| 12 | + createMemo, |
| 13 | + createOptimistic, |
| 14 | + createRenderEffect, |
| 15 | + createRoot, |
| 16 | + createSignal, |
| 17 | + flush, |
| 18 | + createLoadingBoundary, |
| 19 | + untrack |
| 20 | +} from "../src/index.js"; |
| 21 | + |
| 22 | +afterEach(() => flush()); |
| 23 | + |
| 24 | +const tick = () => new Promise<void>(r => setTimeout(r, 0)); |
| 25 | + |
| 26 | +function setup(nested: boolean) { |
| 27 | + type El = { color?: string }; |
| 28 | + let shown: El | undefined; |
| 29 | + let run!: () => Promise<void>; |
| 30 | + let finish!: () => void; |
| 31 | + let dispose!: () => void; |
| 32 | + createRoot(d => { |
| 33 | + dispose = d; |
| 34 | + const [enabled, setEnabled] = createSignal(false); |
| 35 | + const [visible, setVisible] = createOptimistic(enabled); |
| 36 | + const content = () => { |
| 37 | + const el: El = {}; |
| 38 | + createRenderEffect( |
| 39 | + () => (enabled() ? "blue" : "red"), |
| 40 | + v => { |
| 41 | + el.color = v; |
| 42 | + } |
| 43 | + ); |
| 44 | + return el; |
| 45 | + }; |
| 46 | + const view = createMemo(() => { |
| 47 | + if (!visible()) return undefined; |
| 48 | + return nested ? createMemo(content) : () => content(); |
| 49 | + }); |
| 50 | + createRenderEffect( |
| 51 | + () => view()?.(), |
| 52 | + el => { |
| 53 | + shown = el; |
| 54 | + } |
| 55 | + ); |
| 56 | + const act = action(function* () { |
| 57 | + setEnabled(true); |
| 58 | + setVisible(true); |
| 59 | + yield new Promise<void>(r => (finish = r)); |
| 60 | + }); |
| 61 | + run = () => act(); |
| 62 | + }); |
| 63 | + flush(); |
| 64 | + return { |
| 65 | + shown: () => (shown ? { ...shown } : undefined), |
| 66 | + run: () => run(), |
| 67 | + finish: () => finish(), |
| 68 | + dispose: () => dispose() |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +describe("#3835 optimistic mount of a nested memo", () => { |
| 73 | + for (const nested of [true, false]) |
| 74 | + it(`${nested ? "memo-wrapped" : "direct"} content reveals with the screen value`, async () => { |
| 75 | + const s = setup(nested); |
| 76 | + expect(s.shown()).toBeUndefined(); |
| 77 | + const p = s.run(); |
| 78 | + flush(); |
| 79 | + await tick(); |
| 80 | + flush(); |
| 81 | + expect(s.shown()).toEqual({ color: "red" }); |
| 82 | + s.finish(); |
| 83 | + await p; |
| 84 | + await tick(); |
| 85 | + flush(); |
| 86 | + expect(s.shown()).toEqual({ color: "blue" }); |
| 87 | + s.dispose(); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +describe("#3835 a Loading mounted by an optimistic write over a parent flight", () => { |
| 92 | + // A29's boundary exemption holds for lane work too: the boundary's first |
| 93 | + // pass sees the flight pending, held or not, and shows its fallback; it is |
| 94 | + // not handed the flight's committed value. |
| 95 | + for (const order of ["same tick", "after the park"]) |
| 96 | + it(`shows its fallback (${order})`, async () => { |
| 97 | + const log: unknown[] = []; |
| 98 | + let run!: () => Promise<void>; |
| 99 | + let finish!: () => void; |
| 100 | + let dispose!: () => void; |
| 101 | + const lands: ((v: number) => void)[] = []; |
| 102 | + createRoot(d => { |
| 103 | + dispose = d; |
| 104 | + const [count, setCount] = createSignal(1); |
| 105 | + const [show, setShow] = createSignal(false); |
| 106 | + const data = createMemo(() => { |
| 107 | + const v = count(); |
| 108 | + return v === 1 ? v : new Promise<number>(r => lands.push(r)); |
| 109 | + }); |
| 110 | + const [visible, setVisible] = createOptimistic(show); |
| 111 | + const view = createMemo(() => |
| 112 | + visible() |
| 113 | + ? untrack(() => |
| 114 | + createLoadingBoundary( |
| 115 | + () => `data ${data()}`, |
| 116 | + () => "fallback" |
| 117 | + ) |
| 118 | + ) |
| 119 | + : () => "hidden" |
| 120 | + ); |
| 121 | + createRenderEffect( |
| 122 | + () => view()(), |
| 123 | + v => { |
| 124 | + log.push(v); |
| 125 | + } |
| 126 | + ); |
| 127 | + const act = action(function* () { |
| 128 | + setCount(2); |
| 129 | + if (order === "after the park") yield tick(); |
| 130 | + setShow(true); |
| 131 | + setVisible(true); |
| 132 | + yield new Promise<void>(r => (finish = r)); |
| 133 | + }); |
| 134 | + run = () => act(); |
| 135 | + }); |
| 136 | + flush(); |
| 137 | + const p = run(); |
| 138 | + for (let i = 0; i < 2; i++) { |
| 139 | + flush(); |
| 140 | + await tick(); |
| 141 | + } |
| 142 | + flush(); |
| 143 | + expect(log).toEqual(["hidden", "fallback"]); |
| 144 | + lands.forEach(land => land(2)); |
| 145 | + await tick(); |
| 146 | + flush(); |
| 147 | + expect(log).not.toContain("data 1"); |
| 148 | + finish(); |
| 149 | + await p; |
| 150 | + await tick(); |
| 151 | + flush(); |
| 152 | + expect(log.at(-1)).toBe("data 2"); |
| 153 | + expect(log).not.toContain("data 1"); |
| 154 | + dispose(); |
| 155 | + }); |
| 156 | +}); |
0 commit comments