|
| 1 | +/** |
| 2 | + * #4640 review: a plain memo `m` over an async memo `am` over an async source |
| 3 | + * `s`. A write (or an action's write) re-asks `s` and a sibling flight on the |
| 4 | + * same write keeps the change held after `s` rejects. |
| 5 | + * |
| 6 | + * `m` reads the staging through `am` (D-41: "a derivation that reads a |
| 7 | + * staged error throws it"), so its verdict stays pending until the commit |
| 8 | + * (F-9, D-22, D-23) and an untracked `latest(m)` in that window throws the |
| 9 | + * staged error rather than returning the old value that the commit then |
| 10 | + * replaces (#4456). The commit shows the error once: an effect's error arm |
| 11 | + * over `m` fires a single time. |
| 12 | + * |
| 13 | + * Narrowing the staging to readers of a staging (#4640) must not drop the |
| 14 | + * async memo from the marking: unmarked, `m` reported final and `latest(m)` |
| 15 | + * returned "A!" in the window. |
| 16 | + */ |
| 17 | +import { expect, it } from "vitest"; |
| 18 | +import { |
| 19 | + action, |
| 20 | + createEffect, |
| 21 | + createErrorBoundary, |
| 22 | + createMemo, |
| 23 | + createRenderEffect, |
| 24 | + createRoot, |
| 25 | + createSignal, |
| 26 | + flush, |
| 27 | + isPending, |
| 28 | + latest |
| 29 | +} from "../src/index.js"; |
| 30 | + |
| 31 | +const tick = async () => { |
| 32 | + for (let k = 0; k < 4; k++) { |
| 33 | + await new Promise(r => setTimeout(r, 0)); |
| 34 | + flush(); |
| 35 | + } |
| 36 | +}; |
| 37 | + |
| 38 | +it.each(["write", "action"] as const)( |
| 39 | + "a memo over an async memo over a rejected source stays pending until the commit (%s)", |
| 40 | + async how => { |
| 41 | + const asks: { res: (v: string) => void; rej: (e: unknown) => void }[] = []; |
| 42 | + const keeps: ((v: string) => void)[] = []; |
| 43 | + const frames: string[] = []; |
| 44 | + const errors: string[] = []; |
| 45 | + const scr = { m: "-", p: "-" }; |
| 46 | + const [sel, setSel] = createSignal(1); |
| 47 | + let m!: () => string; |
| 48 | + createRoot(() => { |
| 49 | + const s = createMemo(() => { |
| 50 | + sel(); |
| 51 | + return new Promise<string>((res, rej) => asks.push({ res, rej })); |
| 52 | + }); |
| 53 | + const am = createMemo(async () => { |
| 54 | + const v = s(); |
| 55 | + await 0; |
| 56 | + return v; |
| 57 | + }); |
| 58 | + m = createMemo(() => am() + "!"); |
| 59 | + const keep = createMemo(() => { |
| 60 | + sel(); |
| 61 | + return new Promise<string>(res => keeps.push(res)); |
| 62 | + }); |
| 63 | + const eb = <T>(f: () => T) => createErrorBoundary(f, () => "ERR"); |
| 64 | + const vm = eb(() => m()); |
| 65 | + const vp = eb(() => String(isPending(m))); |
| 66 | + const vk = eb(() => keep()); |
| 67 | + createRenderEffect( |
| 68 | + () => vm(), |
| 69 | + v => void (scr.m = v) |
| 70 | + ); |
| 71 | + createRenderEffect( |
| 72 | + () => vp(), |
| 73 | + v => void (scr.p = v) |
| 74 | + ); |
| 75 | + createRenderEffect( |
| 76 | + () => vk(), |
| 77 | + () => {} |
| 78 | + ); |
| 79 | + createEffect(m, { |
| 80 | + effect: () => {}, |
| 81 | + error: e => void errors.push((e as Error).message) |
| 82 | + }); |
| 83 | + }); |
| 84 | + const frame = (l: string) => frames.push(`${l}[${scr.m} pending=${scr.p}]`); |
| 85 | + |
| 86 | + await tick(); |
| 87 | + asks.shift()!.res("A"); |
| 88 | + keeps.shift()!("k"); |
| 89 | + await tick(); |
| 90 | + frame("shown"); |
| 91 | + |
| 92 | + let release: (() => void) | undefined; |
| 93 | + if (how === "action") |
| 94 | + action(function* () { |
| 95 | + setSel(2); |
| 96 | + yield new Promise<void>(r => (release = r)); |
| 97 | + })(); |
| 98 | + else setSel(2); |
| 99 | + flush(); |
| 100 | + await tick(); |
| 101 | + frame("held"); |
| 102 | + |
| 103 | + asks.shift()!.rej(new Error("boom")); |
| 104 | + await tick(); |
| 105 | + frame("rejected"); |
| 106 | + let mid: string; |
| 107 | + try { |
| 108 | + mid = latest(m); |
| 109 | + } catch (e) { |
| 110 | + mid = "throws " + ((e as any)?.cause?.message ?? (e as Error).message); |
| 111 | + } |
| 112 | + |
| 113 | + keeps.shift()!("k2"); |
| 114 | + await tick(); |
| 115 | + if (release) { |
| 116 | + release(); |
| 117 | + await tick(); |
| 118 | + } |
| 119 | + frame("committed"); |
| 120 | + |
| 121 | + expect(frames).toEqual([ |
| 122 | + "shown[A! pending=false]", |
| 123 | + "held[A! pending=true]", |
| 124 | + "rejected[A! pending=true]", |
| 125 | + "committed[ERR pending=false]" |
| 126 | + ]); |
| 127 | + expect(mid).toBe("throws boom"); |
| 128 | + expect(errors).toEqual(["boom"]); |
| 129 | + } |
| 130 | +); |
0 commit comments