|
| 1 | +/** |
| 2 | + * #4343: how a subtree was opened does not change when its render effects |
| 3 | + * run. `<Loading fallback="outer"> > <Show when={ready()}> > |
| 4 | + * <Loading fallback="inner"> > Screen`, where Screen binds a pending async |
| 5 | + * memo (`<p>{data()}</p>`, caught by the inner boundary) and has a render |
| 6 | + * effect that reads nothing pending. That effect's first run goes through |
| 7 | + * with the mount (D-19), beside the inner fallback, whether `ready` is a |
| 8 | + * signal set later or an async memo that settles. Before, under the async |
| 9 | + * gate the effect waited for `data` to land. A user effect's first run is |
| 10 | + * queued and waits for the reveal under both gates. |
| 11 | + */ |
| 12 | +import { describe, expect, it } from "vitest"; |
| 13 | +import { |
| 14 | + createEffect, |
| 15 | + createLoadingBoundary, |
| 16 | + createMemo, |
| 17 | + createRenderEffect, |
| 18 | + createRoot, |
| 19 | + createSignal, |
| 20 | + flush, |
| 21 | + untrack |
| 22 | +} from "../src/index.js"; |
| 23 | + |
| 24 | +function deferred<T>() { |
| 25 | + let resolve!: (v: T) => void; |
| 26 | + const promise = new Promise<T>(r => (resolve = r)); |
| 27 | + return { promise, resolve }; |
| 28 | +} |
| 29 | + |
| 30 | +const settle = async () => { |
| 31 | + for (let i = 0; i < 3; i++) { |
| 32 | + await new Promise(r => setTimeout(r, 0)); |
| 33 | + flush(); |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +async function run(gate: "signal" | "async") { |
| 38 | + const log: string[] = []; |
| 39 | + const readyGate = deferred<boolean>(); |
| 40 | + const dataGate = deferred<string>(); |
| 41 | + const [open, setOpen] = createSignal(false); |
| 42 | + let slot: unknown; |
| 43 | + const screens: unknown[] = []; |
| 44 | + const snap = () => screens.push([slot, [...log]]); |
| 45 | + |
| 46 | + createRoot(() => { |
| 47 | + const ready = gate === "async" ? createMemo(() => readyGate.promise) : open; |
| 48 | + const screen = () => { |
| 49 | + const data = createMemo(async () => { |
| 50 | + const v = await dataGate.promise; |
| 51 | + log.push("data"); |
| 52 | + return v; |
| 53 | + }); |
| 54 | + createRenderEffect( |
| 55 | + () => 0, |
| 56 | + () => { |
| 57 | + log.push("effect"); |
| 58 | + } |
| 59 | + ); |
| 60 | + // A user effect's first run is queued: it waits behind the fallback. |
| 61 | + createEffect( |
| 62 | + () => 0, |
| 63 | + () => { |
| 64 | + log.push("user"); |
| 65 | + } |
| 66 | + ); |
| 67 | + // <p>{data()}</p> |
| 68 | + return () => data(); |
| 69 | + }; |
| 70 | + const outer = createLoadingBoundary( |
| 71 | + () => { |
| 72 | + // <Show when={ready()}> |
| 73 | + const condition = createMemo(() => !!ready()); |
| 74 | + return createMemo(() => |
| 75 | + condition() ? untrack(() => createLoadingBoundary(screen, () => "inner")) : undefined |
| 76 | + ); |
| 77 | + }, |
| 78 | + () => "outer" |
| 79 | + ); |
| 80 | + createRenderEffect(outer, v => { |
| 81 | + slot = v; |
| 82 | + }); |
| 83 | + }); |
| 84 | + flush(); |
| 85 | + snap(); |
| 86 | + |
| 87 | + if (gate === "signal") { |
| 88 | + setOpen(true); |
| 89 | + flush(); |
| 90 | + } else readyGate.resolve(true); |
| 91 | + await settle(); |
| 92 | + snap(); |
| 93 | + |
| 94 | + dataGate.resolve("v"); |
| 95 | + await settle(); |
| 96 | + snap(); |
| 97 | + return screens; |
| 98 | +} |
| 99 | + |
| 100 | +describe("#4343: a render effect under a nested boundary runs when its subtree mounts", () => { |
| 101 | + it("gate: a signal set later", async () => { |
| 102 | + expect(await run("signal")).toEqual([ |
| 103 | + [undefined, []], |
| 104 | + ["inner", ["effect"]], |
| 105 | + ["v", ["effect", "data", "user"]] |
| 106 | + ]); |
| 107 | + }); |
| 108 | + |
| 109 | + it("gate: an async memo that settles", async () => { |
| 110 | + expect(await run("async")).toEqual([ |
| 111 | + ["outer", []], |
| 112 | + ["inner", ["effect"]], |
| 113 | + ["v", ["effect", "data", "user"]] |
| 114 | + ]); |
| 115 | + }); |
| 116 | +}); |
0 commit comments