|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + */ |
| 4 | +// #4568: under a `<Loading>` that has not revealed yet, a rejection reaches |
| 5 | +// `<Errored>` at once even when a `latest()` reader under the same boundary |
| 6 | +// reads a derive whose upstream still has a flight out for an older question. |
| 7 | +// An error is an answer: it ends the wait like a value (F-12, D-41), and the |
| 8 | +// obsolete flight answers a question no reader asks anymore. |
| 9 | +import { render } from "@solidjs/web"; |
| 10 | +import { createMemo, createSignal, Errored, flush, latest, Loading } from "solid-js"; |
| 11 | +import { describe, expect, it } from "vitest"; |
| 12 | + |
| 13 | +const tick = async () => { |
| 14 | + for (let i = 0; i < 2; i++) { |
| 15 | + await new Promise(r => setTimeout(r, 0)); |
| 16 | + flush(); |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +async function run(mode: "latest" | "plain", direct = true) { |
| 21 | + const flights = new Map<string, { resolve: (v: number) => void; reject: (e: Error) => void }>(); |
| 22 | + const ask = (key: string) => |
| 23 | + new Promise<number>((resolve, reject) => flights.set(key, { resolve, reject })); |
| 24 | + const [s, setS] = createSignal(0); |
| 25 | + const el = document.createElement("div"); |
| 26 | + const dispose = render(() => { |
| 27 | + const a0 = createMemo(() => ask("a0:" + s())); |
| 28 | + const a1 = createMemo(async () => ask("a1:" + (await a0()))); |
| 29 | + const c0 = createMemo(() => `${a1()}.${a0()}`); |
| 30 | + return ( |
| 31 | + <Errored fallback={e => <p>error: {(e() as Error).message}</p>}> |
| 32 | + <Loading fallback={<p>loading</p>}> |
| 33 | + {direct ? <p>a0={a0()}</p> : null} |
| 34 | + <i>c0={mode === "latest" ? latest(c0) : c0()}</i> |
| 35 | + </Loading> |
| 36 | + </Errored> |
| 37 | + ); |
| 38 | + }, el); |
| 39 | + await tick(); |
| 40 | + setS(1); |
| 41 | + flush(); |
| 42 | + await tick(); |
| 43 | + flights.get("a0:1")!.resolve(12); |
| 44 | + await tick(); // asks a1:12 |
| 45 | + setS(2); |
| 46 | + flush(); |
| 47 | + await tick(); // asks a0:2; a1:12 is now obsolete |
| 48 | + flights.get("a0:2")!.reject(new Error("boom")); |
| 49 | + for (let i = 0; i < 6; i++) await tick(); |
| 50 | + const afterReject = el.textContent; |
| 51 | + flights.get("a1:12")!.resolve(7); |
| 52 | + await tick(); // the obsolete flight lands |
| 53 | + const afterObsolete = el.textContent; |
| 54 | + dispose(); |
| 55 | + return { afterReject, afterObsolete }; |
| 56 | +} |
| 57 | + |
| 58 | +const both = { afterReject: "error: boom", afterObsolete: "error: boom" }; |
| 59 | + |
| 60 | +describe("#4568: a rejection under an unrevealed <Loading> is not held by an obsolete flight", () => { |
| 61 | + it("plain c0(): the rejection reaches <Errored> at once", async () => { |
| 62 | + expect(await run("plain")).toEqual(both); |
| 63 | + }); |
| 64 | + it("latest(c0): the rejection reaches <Errored> at once", async () => { |
| 65 | + expect(await run("latest")).toEqual(both); |
| 66 | + }); |
| 67 | + it("latest(c0) without the direct a0 reader: the rejection reaches <Errored> at once", async () => { |
| 68 | + expect(await run("latest", false)).toEqual(both); |
| 69 | + }); |
| 70 | +}); |
0 commit comments