|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createErrorBoundary, |
| 4 | + createMemo, |
| 5 | + createProjection, |
| 6 | + createRenderEffect, |
| 7 | + createRoot, |
| 8 | + createSignal, |
| 9 | + createStore, |
| 10 | + flush |
| 11 | +} from "../src/index.js"; |
| 12 | + |
| 13 | +// #4644 (D-39, F-3, F-12): a fallback no live reader holds heals. The shape: |
| 14 | +// `<Errored><Show when={page() === "list"} fallback="about">{X}</Show> |
| 15 | +// </Errored>`, `rows` an async memo owned outside the boundary that rejects. |
| 16 | +// X = "memo-only": `rows` is read only by memos (a `<Show when>` condition) |
| 17 | +// whose value the boundary's tree flattens, so the reader the boundary |
| 18 | +// caught is its own tree. X = "effect": a render effect in the branch reads |
| 19 | +// it. Navigating away disposes every reader of `rows`: nothing on the new |
| 20 | +// branch reads the error, so the boundary shows it, as a fresh render with |
| 21 | +// the same inputs would. With another live reader of `rows` under the same |
| 22 | +// boundary ("kept"), the error stays up. |
| 23 | +const tick = async () => { |
| 24 | + for (let i = 0; i < 3; i++) { |
| 25 | + await new Promise(r => setTimeout(r, 0)); |
| 26 | + flush(); |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +for (const shape of ["memo-only", "effect", "kept"] as const) |
| 31 | + it(`${shape}: navigating away from a failed memo (#4644)`, async () => { |
| 32 | + let reject!: (e: Error) => void; |
| 33 | + const [page, setPage] = createSignal("list"); |
| 34 | + const frames: unknown[] = []; |
| 35 | + const dispose = createRoot(d => { |
| 36 | + const rows = createMemo( |
| 37 | + () => |
| 38 | + new Promise<number[]>((_, r) => { |
| 39 | + reject = r; |
| 40 | + }) |
| 41 | + ); |
| 42 | + const view = createErrorBoundary( |
| 43 | + () => { |
| 44 | + if (shape === "kept") |
| 45 | + createRenderEffect( |
| 46 | + () => rows().length, |
| 47 | + () => {} |
| 48 | + ); |
| 49 | + const onList = createMemo(() => page() === "list"); |
| 50 | + return createMemo(() => { |
| 51 | + if (!onList()) return "about"; |
| 52 | + if (shape === "effect") { |
| 53 | + createRenderEffect( |
| 54 | + () => rows().length, |
| 55 | + () => {} |
| 56 | + ); |
| 57 | + return "list"; |
| 58 | + } |
| 59 | + const cond = createMemo(() => rows().length === 0); |
| 60 | + return createMemo(() => (cond() ? "none" : "some")); |
| 61 | + }); |
| 62 | + }, |
| 63 | + () => "ERR" |
| 64 | + ); |
| 65 | + createRenderEffect( |
| 66 | + () => view(), |
| 67 | + v => { |
| 68 | + frames.push(v); |
| 69 | + } |
| 70 | + ); |
| 71 | + return d; |
| 72 | + }); |
| 73 | + flush(); |
| 74 | + await tick(); |
| 75 | + reject(new Error("boom")); |
| 76 | + await tick(); |
| 77 | + expect(frames[frames.length - 1]).toBe("ERR"); |
| 78 | + setPage("about"); |
| 79 | + flush(); |
| 80 | + await tick(); |
| 81 | + dispose(); |
| 82 | + expect(frames[frames.length - 1]).toBe(shape === "kept" ? "ERR" : "about"); |
| 83 | + }); |
| 84 | + |
| 85 | +// The boundary waits on what threw only while a reader THAT CAUGHT ITS ERROR |
| 86 | +// is alive (D-39). Something else still reading `rows` is not one: a memo |
| 87 | +// hoisted outside the boundary, a derived store or projection built from it, |
| 88 | +// a reader under another error boundary. Each still keeps `rows` subscribed, |
| 89 | +// so "is `rows` read at all" kept this boundary stuck. While the list is |
| 90 | +// mounted the error stays (across a prune: another reader's settling); away, |
| 91 | +// the boundary heals; back, the error shows again (`sync`: a sync throw |
| 92 | +// re-thrown by the new reader — an async source is re-asked on the new read |
| 93 | +// instead, which is not this rule). |
| 94 | +const flat = (v: unknown) => { |
| 95 | + while (typeof v === "function") v = v(); |
| 96 | + return v; |
| 97 | +}; |
| 98 | +for (const via of [ |
| 99 | + "hoisted memo", |
| 100 | + "derived store", |
| 101 | + "projection", |
| 102 | + "other boundary", |
| 103 | + "sync" |
| 104 | +] as const) |
| 105 | + it(`${via}: a reader outside the boundary does not keep its error up (#4644)`, async () => { |
| 106 | + let reject!: (e: Error) => void; |
| 107 | + let resolveP!: (v: number) => void; |
| 108 | + const [page, setPage] = createSignal("list"); |
| 109 | + const [fail, setFail] = createSignal(false); |
| 110 | + const frames: unknown[] = []; |
| 111 | + const other: unknown[] = []; |
| 112 | + const dispose = createRoot(d => { |
| 113 | + const rows = createMemo(() => { |
| 114 | + if (via !== "sync") return new Promise<number[]>((_, r) => (reject = r)); |
| 115 | + if (fail()) throw new Error("sync boom"); |
| 116 | + return [] as number[]; |
| 117 | + }); |
| 118 | + let read: () => number[] = rows; |
| 119 | + if (via === "hoisted memo" || via === "sync") read = createMemo(() => [...rows()]); |
| 120 | + if (via === "derived store") { |
| 121 | + const [st] = createStore<{ list: number[] }>( |
| 122 | + dr => { |
| 123 | + dr.list = rows(); |
| 124 | + }, |
| 125 | + { list: [] } |
| 126 | + ); |
| 127 | + read = () => st.list; |
| 128 | + } |
| 129 | + if (via === "projection") { |
| 130 | + const st = createProjection<{ list: number[] }>( |
| 131 | + dr => { |
| 132 | + dr.list = rows(); |
| 133 | + }, |
| 134 | + { list: [] } |
| 135 | + ); |
| 136 | + read = () => st.list; |
| 137 | + } |
| 138 | + if (via === "other boundary") { |
| 139 | + const header = createErrorBoundary( |
| 140 | + () => createMemo(() => rows().length), |
| 141 | + () => "ERR2" |
| 142 | + ); |
| 143 | + createRenderEffect( |
| 144 | + () => flat(header()), |
| 145 | + v => void other.push(v) |
| 146 | + ); |
| 147 | + } |
| 148 | + const p = createMemo(() => new Promise<number>(r => (resolveP = r))); |
| 149 | + const view = createErrorBoundary( |
| 150 | + () => { |
| 151 | + createRenderEffect( |
| 152 | + () => p(), |
| 153 | + () => {} |
| 154 | + ); |
| 155 | + const onList = createMemo(() => page() === "list"); |
| 156 | + return createMemo(() => { |
| 157 | + if (!onList()) return "about"; |
| 158 | + const cond = createMemo(() => read().length === 0); |
| 159 | + return createMemo(() => (cond() ? "none" : "some")); |
| 160 | + }); |
| 161 | + }, |
| 162 | + () => "ERR" |
| 163 | + ); |
| 164 | + createRenderEffect( |
| 165 | + () => flat(view()), |
| 166 | + v => void frames.push(v) |
| 167 | + ); |
| 168 | + return d; |
| 169 | + }); |
| 170 | + const last = () => frames[frames.length - 1]; |
| 171 | + flush(); |
| 172 | + await tick(); |
| 173 | + if (via === "sync") setFail(true); |
| 174 | + else reject(new Error("boom")); |
| 175 | + await tick(); |
| 176 | + const failed = last(); |
| 177 | + resolveP(1); |
| 178 | + await tick(); |
| 179 | + const afterPrune = last(); |
| 180 | + setPage("about"); |
| 181 | + await tick(); |
| 182 | + const away = last(); |
| 183 | + setPage("list"); |
| 184 | + await tick(); |
| 185 | + const back = last(); |
| 186 | + dispose(); |
| 187 | + expect({ failed, afterPrune, away }).toEqual({ |
| 188 | + failed: "ERR", |
| 189 | + afterPrune: "ERR", |
| 190 | + away: "about" |
| 191 | + }); |
| 192 | + if (via === "sync") expect(back).toBe("ERR"); |
| 193 | + if (via === "other boundary") expect(other[other.length - 1]).toBe("ERR2"); |
| 194 | + }); |
0 commit comments