|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * @vitest-environment jsdom |
| 4 | + */ |
| 5 | +// #4378 (async-app generator, pair E8): a reader recreated in the flush that |
| 6 | +// clears an upstream error ends with the async memo's error, as the reader |
| 7 | +// that was not recreated does — not an empty hole. The signals-side pin is |
| 8 | +// `packages/signals/tests/recreated-reader-held-boundary-4378.test.ts`. |
| 9 | +import { describe, expect, test } from "vitest"; |
| 10 | +import { createMemo, createSignal, Errored, flush, latest, Loading, Show } from "solid-js"; |
| 11 | +import { render } from "../src/index.js"; |
| 12 | +import { runScenario } from "./async-gen/run.js"; |
| 13 | +import type { Scenario } from "./async-gen/scenario.js"; |
| 14 | + |
| 15 | +const tick = async () => { |
| 16 | + for (let i = 0; i < 5; i++) await Promise.resolve(); |
| 17 | + await new Promise(r => setTimeout(r, 0)); |
| 18 | + flush(); |
| 19 | +}; |
| 20 | + |
| 21 | +describe("a reader born in the flush that clears an upstream error (#4378)", () => { |
| 22 | + for (const recreate of [false, true]) |
| 23 | + test(recreate ? "recreated in that flush" : "kept", async () => { |
| 24 | + const requests: Array<() => void> = []; |
| 25 | + const fetchM1 = (x: number) => |
| 26 | + new Promise<number>((res, rej) => |
| 27 | + requests.push(() => (x % 4 === 0 ? rej(new Error("m1:" + x)) : res(x))) |
| 28 | + ); |
| 29 | + const div = document.createElement("div"); |
| 30 | + let setS!: (v: number) => void, setEpoch!: (v: number) => void; |
| 31 | + const dispose = render(() => { |
| 32 | + const [s, _setS] = createSignal(3); |
| 33 | + setS = _setS; |
| 34 | + const m0 = createMemo(() => { |
| 35 | + const v = 1 + latest(s); |
| 36 | + if (v % 4 === 0) throw new Error("m0"); |
| 37 | + return v; |
| 38 | + }); |
| 39 | + const m1 = createMemo(() => fetchM1(1 + m0())); |
| 40 | + const [epoch, _setEpoch] = createSignal(1); |
| 41 | + setEpoch = _setEpoch; |
| 42 | + return ( |
| 43 | + <Errored fallback={() => "[ERR]"}> |
| 44 | + <Loading fallback="[LOAD]"> |
| 45 | + <Show when={epoch()} keyed> |
| 46 | + <span>{m1()}</span> |
| 47 | + </Show> |
| 48 | + </Loading> |
| 49 | + </Errored> |
| 50 | + ); |
| 51 | + }, div); |
| 52 | + flush(); |
| 53 | + await tick(); |
| 54 | + expect(div.innerHTML).toBe("[ERR]"); |
| 55 | + setS(2); |
| 56 | + if (recreate) setEpoch(2); |
| 57 | + flush(); |
| 58 | + for (let i = 0; i < 5; i++) { |
| 59 | + while (requests.length) requests.shift()!(); |
| 60 | + await tick(); |
| 61 | + } |
| 62 | + expect(div.innerHTML).toBe("[ERR]"); |
| 63 | + dispose(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +// The generator's E8 case at next @ 40cde2cd: the staging the hold built is |
| 68 | +// superseded at every frame node up the chain (the inner Errored, the root |
| 69 | +// Loading and Errored), each re-run from the heap outside its hold and back |
| 70 | +// to its committed fallback. |
| 71 | +test("the generator's E8 case: recreated readers under nested boundaries (#4378)", async () => { |
| 72 | + const sc: Scenario = { |
| 73 | + signals: [2, 3], |
| 74 | + memos: [ |
| 75 | + { async: false, deps: [{ src: { k: "s", i: 1 }, how: "latest" }], poison: 0 }, |
| 76 | + { async: true, deps: [{ src: { k: "m", i: 0 }, how: "plain" }], poison: 0 } |
| 77 | + ], |
| 78 | + tree: { |
| 79 | + t: "errored", |
| 80 | + child: { |
| 81 | + t: "leaf", |
| 82 | + text: [], |
| 83 | + attrs: [ |
| 84 | + { how: "plain", src: { k: "s", i: 1 } }, |
| 85 | + { how: "plain", src: { k: "m", i: 1 } } |
| 86 | + ], |
| 87 | + grouped: false |
| 88 | + } |
| 89 | + }, |
| 90 | + steps: [ |
| 91 | + { t: "settle", pick: 0 }, |
| 92 | + { t: "task" }, |
| 93 | + { t: "action", s: 1, v: 0 }, |
| 94 | + { t: "write", s: 1, v: 1 }, |
| 95 | + { t: "flush" }, |
| 96 | + { t: "action", s: 1, v: 0 }, |
| 97 | + { t: "write", s: 1, v: 2 }, |
| 98 | + { t: "settle", pick: 3 } |
| 99 | + ] |
| 100 | + } as Scenario; |
| 101 | + const kept = await runScenario(sc); |
| 102 | + const recreated = await runScenario(sc, { remount: true }); |
| 103 | + // Where nothing is in flight (the last step settles the last request). |
| 104 | + expect(recreated.screens.at(-1)).toBe("[E]"); |
| 105 | + expect(recreated.final).toBe(kept.final); |
| 106 | + expect(recreated.final).toBe("[E]"); |
| 107 | +}); |
0 commit comments