|
| 1 | +/** |
| 2 | + * #3540 under L2 — A29's boundary exemption, in a flush and on a hold. |
| 3 | + * |
| 4 | + * A loading boundary that has not shown content mounted while a transaction |
| 5 | + * holds what it reads shows its fallback now and its content at the commit: |
| 6 | + * the first pass under it is the boundary's, not the tick's. A mount inside |
| 7 | + * a flush (a Show opening) publishes with it; a derivation outside the |
| 8 | + * boundary in the same flush still holds the tick. Content bound by a render |
| 9 | + * effect (the tree never reads the held value) is collected too, so the |
| 10 | + * boundary never reveals empty content. |
| 11 | + */ |
| 12 | +import { describe, expect, it } from "vitest"; |
| 13 | +import { |
| 14 | + action, |
| 15 | + createLoadingBoundary, |
| 16 | + createMemo, |
| 17 | + createRenderEffect, |
| 18 | + createRoot, |
| 19 | + createSignal, |
| 20 | + flush, |
| 21 | + untrack |
| 22 | +} from "../src/index.js"; |
| 23 | + |
| 24 | +const tick = async () => { |
| 25 | + for (let i = 0; i < 6; i++) await Promise.resolve(); |
| 26 | + flush(); |
| 27 | +}; |
| 28 | + |
| 29 | +function hold(write: () => void) { |
| 30 | + let release!: () => void; |
| 31 | + action(function* () { |
| 32 | + write(); |
| 33 | + yield new Promise<void>(resolve => (release = resolve)); |
| 34 | + })(); |
| 35 | + flush(); |
| 36 | + return () => release(); |
| 37 | +} |
| 38 | + |
| 39 | +/** `<Loading fallback={fallback}>{fn()}</Loading>`, created untracked as |
| 40 | + * createComponent does. */ |
| 41 | +const Loading = <T>(fn: () => T, fallback: string) => |
| 42 | + untrack(() => createLoadingBoundary(fn, () => fallback)); |
| 43 | + |
| 44 | +type Content = "memo" | "direct" | "bound"; |
| 45 | + |
| 46 | +/** The boundary's content over `x`, logging into `log`. */ |
| 47 | +function content(kind: Content, x: () => number, log: string[]) { |
| 48 | + return () => { |
| 49 | + if (kind === "direct") return `content ${x()}`; |
| 50 | + const m = createMemo(() => `content ${x()}`); |
| 51 | + if (kind === "memo") return m(); |
| 52 | + // <p>{m()}</p>: the content's binding reads the memo, the tree does not. |
| 53 | + createRenderEffect(m, v => { |
| 54 | + log.push(`text ${v}`); |
| 55 | + }); |
| 56 | + return "p"; |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function mount(kind: Content, x: () => number, log: string[]) { |
| 61 | + const view = Loading(content(kind, x, log), "fallback"); |
| 62 | + createRenderEffect(view, v => { |
| 63 | + log.push(`view ${v}`); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +const shapes = [ |
| 68 | + ["memo", "through a memo"], |
| 69 | + ["direct", "directly"], |
| 70 | + ["bound", "bound by a render effect under the boundary"] |
| 71 | +] as const; |
| 72 | + |
| 73 | +/** The commit's reveal: the content's own entries, in any order. */ |
| 74 | +const revealed = (kind: Content) => |
| 75 | + kind === "bound" ? ["text content 1", "view p"] : ["view content 1"]; |
| 76 | + |
| 77 | +describe("#3540 in a flush: a fresh Loading mounted over a held value shows its fallback now", () => { |
| 78 | + function setup(kind: Content, nested = false) { |
| 79 | + const [x, setX] = createSignal(0); |
| 80 | + const [open, setOpen] = createSignal(false); |
| 81 | + const log: string[] = []; |
| 82 | + createRoot(() => { |
| 83 | + createRenderEffect(x, v => { |
| 84 | + log.push(`x ${v}`); |
| 85 | + }); |
| 86 | + // <Show when={open()}><Loading fallback="fallback"><Content/></Loading></Show> |
| 87 | + const show = () => |
| 88 | + createRenderEffect( |
| 89 | + () => { |
| 90 | + if (!open()) return false; |
| 91 | + mount(kind, x, log); |
| 92 | + return true; |
| 93 | + }, |
| 94 | + v => { |
| 95 | + log.push(`open ${v}`); |
| 96 | + } |
| 97 | + ); |
| 98 | + if (!nested) return show(); |
| 99 | + // Under an outer Loading that has shown content. |
| 100 | + const outer = Loading(() => (show(), "outer"), "outer fallback"); |
| 101 | + createRenderEffect(outer, v => { |
| 102 | + log.push(`outer ${v}`); |
| 103 | + }); |
| 104 | + }); |
| 105 | + flush(); |
| 106 | + if (nested) expect(log).toContain("outer outer"); |
| 107 | + const release = hold(() => setX(1)); |
| 108 | + log.length = 0; |
| 109 | + return { setOpen, log, release }; |
| 110 | + } |
| 111 | + |
| 112 | + for (const [kind, how] of shapes) |
| 113 | + it(`content reads the held value ${how}: the mount publishes, the content reveals at the commit`, async () => { |
| 114 | + const s = setup(kind); |
| 115 | + s.setOpen(true); |
| 116 | + flush(); |
| 117 | + expect(s.log).toEqual(["view fallback", "open true"]); |
| 118 | + |
| 119 | + s.release(); |
| 120 | + await tick(); |
| 121 | + expect(s.log.slice(2).sort()).toEqual(["x 1", ...revealed(kind)].sort()); |
| 122 | + }); |
| 123 | + |
| 124 | + it("nested under an outer Loading that has shown content: the inner fallback now, the outer keeps its content", async () => { |
| 125 | + const s = setup("memo", true); |
| 126 | + s.setOpen(true); |
| 127 | + flush(); |
| 128 | + expect(s.log).toEqual(["view fallback", "open true"]); |
| 129 | + |
| 130 | + s.release(); |
| 131 | + await tick(); |
| 132 | + expect(s.log.slice(2).sort()).toEqual(["view content 1", "x 1"]); |
| 133 | + }); |
| 134 | + |
| 135 | + it("a derivation outside the boundary in the same flush still holds the tick (membership is the tick's)", async () => { |
| 136 | + const [x, setX] = createSignal(0); |
| 137 | + const [open, setOpen] = createSignal(false); |
| 138 | + const log: string[] = []; |
| 139 | + createRoot(() => { |
| 140 | + createRenderEffect( |
| 141 | + () => { |
| 142 | + if (!open()) return false; |
| 143 | + const label = createMemo(() => `label ${x()}`); |
| 144 | + createRenderEffect(label, v => { |
| 145 | + log.push(v); |
| 146 | + }); |
| 147 | + mount("direct", x, log); |
| 148 | + return true; |
| 149 | + }, |
| 150 | + v => { |
| 151 | + log.push(`open ${v}`); |
| 152 | + } |
| 153 | + ); |
| 154 | + }); |
| 155 | + flush(); |
| 156 | + const release = hold(() => setX(1)); |
| 157 | + log.length = 0; |
| 158 | + |
| 159 | + setOpen(true); |
| 160 | + flush(); |
| 161 | + expect(log).not.toContain("open true"); |
| 162 | + expect(log.filter(l => l.startsWith("label"))).toEqual([]); |
| 163 | + |
| 164 | + release(); |
| 165 | + await tick(); |
| 166 | + expect(log).toContain("open true"); |
| 167 | + expect(log).toContain("label 1"); |
| 168 | + expect(log.at(-1)).toBe("view content 1"); |
| 169 | + }); |
| 170 | + |
| 171 | + it("the boundary's hold stays its own: a mount over another hold after the flush reveals at that hold's release", async () => { |
| 172 | + const [x, setX] = createSignal(0); |
| 173 | + const [y, setY] = createSignal(0); |
| 174 | + const [open, setOpen] = createSignal(false); |
| 175 | + const log: string[] = []; |
| 176 | + createRoot(() => { |
| 177 | + createRenderEffect( |
| 178 | + () => { |
| 179 | + if (!open()) return false; |
| 180 | + mount("direct", x, log); |
| 181 | + return true; |
| 182 | + }, |
| 183 | + v => { |
| 184 | + log.push(`open ${v}`); |
| 185 | + } |
| 186 | + ); |
| 187 | + }); |
| 188 | + flush(); |
| 189 | + const releaseX = hold(() => setX(1)); |
| 190 | + const releaseY = hold(() => setY(1)); |
| 191 | + log.length = 0; |
| 192 | + |
| 193 | + setOpen(true); |
| 194 | + flush(); |
| 195 | + expect(log).toEqual(["view fallback", "open true"]); |
| 196 | + |
| 197 | + // Mounted from mainline, outside a flush, over y's hold only. |
| 198 | + const other: string[] = []; |
| 199 | + createRoot(() => mount("direct", y, other)); |
| 200 | + flush(); |
| 201 | + expect(other).toEqual(["view fallback"]); |
| 202 | + |
| 203 | + releaseY(); |
| 204 | + await tick(); |
| 205 | + expect(other).toEqual(["view fallback", "view content 1"]); |
| 206 | + expect(log).toEqual(["view fallback", "open true"]); |
| 207 | + |
| 208 | + releaseX(); |
| 209 | + await tick(); |
| 210 | + expect(log.at(-1)).toBe("view content 1"); |
| 211 | + }); |
| 212 | +}); |
| 213 | + |
| 214 | +describe("#3540 on a hold: a fresh Loading mounted from mainline over a held value", () => { |
| 215 | + for (const [kind, how] of shapes) |
| 216 | + it(`content reads the held value ${how}: the fallback now, the content at the commit`, async () => { |
| 217 | + const [x, setX] = createSignal(0); |
| 218 | + const release = hold(() => setX(1)); |
| 219 | + const log: string[] = []; |
| 220 | + createRoot(() => mount(kind, x, log)); |
| 221 | + flush(); |
| 222 | + expect(log).toEqual(["view fallback"]); |
| 223 | + |
| 224 | + release(); |
| 225 | + await tick(); |
| 226 | + expect(log.slice(1).sort()).toEqual(revealed(kind).sort()); |
| 227 | + }); |
| 228 | +}); |
0 commit comments