|
| 1 | +/** |
| 2 | + * until() created AFTER its confirming frame was staged (#3482). |
| 3 | + * |
| 4 | + * The single-primitive shape the until() docstring recommends: an optimistic |
| 5 | + * row on the store a live stream feeds, confirmed by the server's echo. When |
| 6 | + * the server broadcasts to subscribers BEFORE it answers the mutation, the |
| 7 | + * confirming frame lands while the action is still awaiting the answer and is |
| 8 | + * held under the action's optimism. The action then reaches `yield until(...)` |
| 9 | + * from mainline (after its await). |
| 10 | + * |
| 11 | + * #3451 made mainline creation under a hold born held (A29). A born-held |
| 12 | + * effect skips its synchronous first run and is replayed at the commit — but |
| 13 | + * the commit IS the action's settle, which the until() promise is holding |
| 14 | + * open. Deadlock: with a timeout, TimeoutError; without one, forever. |
| 15 | + * |
| 16 | + * Ruling: a CONFIG_DIRECT_COMMIT reader (resolve / until / awaitable |
| 17 | + * refresh's waiter) is the tunnel through a hold by contract and applies on |
| 18 | + * its own microtask, so `enterStagedRead` neither enters it nor bears it held |
| 19 | + * — the same exemption verdict pulls already have. The predicate sees the |
| 20 | + * held frame, the action settles, the commit reveals frame + overlay revert. |
| 21 | + * |
| 22 | + * Reproduction and analysis by @brenelz (#3482). Regression from d80cd1f6. |
| 23 | + */ |
| 24 | +import { expect, test } from "vitest"; |
| 25 | +import { |
| 26 | + action, |
| 27 | + createOptimisticStore, |
| 28 | + createRenderEffect, |
| 29 | + createRoot, |
| 30 | + flush, |
| 31 | + TimeoutError, |
| 32 | + until |
| 33 | +} from "../src/index.js"; |
| 34 | + |
| 35 | +type Row = { id: string; status: "pending" | "confirmed" }; |
| 36 | +type Snapshot = { rows: Row[] }; |
| 37 | + |
| 38 | +const settle = async (n = 3) => { |
| 39 | + for (let i = 0; i < n; i++) { |
| 40 | + await new Promise(r => setTimeout(r, 0)); |
| 41 | + flush(); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +// A manually pumped AsyncIterable — what a live() server-function stream |
| 46 | +// materializes as on the client. |
| 47 | +function stream<T>() { |
| 48 | + const buffered: IteratorResult<T>[] = []; |
| 49 | + let waiter: ((r: IteratorResult<T>) => void) | null = null; |
| 50 | + const iterable: AsyncIterable<T> = { |
| 51 | + [Symbol.asyncIterator]: () => ({ |
| 52 | + next: () => |
| 53 | + new Promise<IteratorResult<T>>(res => { |
| 54 | + if (buffered.length) res(buffered.shift()!); |
| 55 | + else waiter = res; |
| 56 | + }), |
| 57 | + return: () => Promise.resolve({ done: true as const, value: undefined }) |
| 58 | + }) |
| 59 | + }; |
| 60 | + return { |
| 61 | + iterable, |
| 62 | + push(value: T) { |
| 63 | + const r = { done: false as const, value }; |
| 64 | + if (waiter) { |
| 65 | + const w = waiter; |
| 66 | + waiter = null; |
| 67 | + w(r); |
| 68 | + } else buffered.push(r); |
| 69 | + } |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +/** The optimistic-row + socket-echo shape; `frameFirst` picks the ordering. */ |
| 74 | +async function reserveScenario(frameFirst: boolean) { |
| 75 | + const feed = stream<Snapshot>(); |
| 76 | + let answer!: (row: Row) => void; |
| 77 | + const mutation = new Promise<Row>(res => (answer = res)); |
| 78 | + const views: string[] = []; |
| 79 | + let reserve!: () => Promise<unknown>; |
| 80 | + |
| 81 | + createRoot(() => { |
| 82 | + const [store, setStore] = createOptimisticStore<Snapshot>( |
| 83 | + () => feed.iterable, |
| 84 | + { rows: [] }, |
| 85 | + { key: "id" } |
| 86 | + ); |
| 87 | + reserve = action(async function* () { |
| 88 | + setStore(d => { |
| 89 | + d.rows.push({ id: "temp", status: "pending" }); |
| 90 | + }); |
| 91 | + const saved = await mutation; |
| 92 | + yield until(() => store.rows.some(r => r.id === saved.id), { timeout: 200 }); |
| 93 | + }); |
| 94 | + createRenderEffect( |
| 95 | + () => store.rows.map(r => `${r.id}:${r.status}`).join(",") || "empty", |
| 96 | + v => { |
| 97 | + views.push(v); |
| 98 | + } |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + feed.push({ rows: [] }); |
| 103 | + await settle(); |
| 104 | + expect(views.at(-1)).toBe("empty"); |
| 105 | + |
| 106 | + const done = reserve().then( |
| 107 | + () => "settled", |
| 108 | + e => e |
| 109 | + ); |
| 110 | + await settle(); |
| 111 | + expect(views.at(-1)).toBe("temp:pending"); |
| 112 | + |
| 113 | + const confirmed: Snapshot = { rows: [{ id: "res_1", status: "confirmed" }] }; |
| 114 | + if (frameFirst) { |
| 115 | + // Server mutates and broadcasts first — the frame lands while the action |
| 116 | + // is still awaiting the mutation's answer, and is held under its optimism. |
| 117 | + feed.push(confirmed); |
| 118 | + await settle(); |
| 119 | + expect(views.at(-1)).toBe("temp:pending"); |
| 120 | + // Then it answers, and the action reaches its until(). |
| 121 | + answer({ id: "res_1", status: "confirmed" }); |
| 122 | + } else { |
| 123 | + // Control: the answer arrives first, until() subscribes, then the frame. |
| 124 | + answer({ id: "res_1", status: "confirmed" }); |
| 125 | + await settle(); |
| 126 | + expect(views.at(-1)).toBe("temp:pending"); |
| 127 | + feed.push(confirmed); |
| 128 | + } |
| 129 | + await settle(); |
| 130 | + |
| 131 | + const outcome = await done; |
| 132 | + expect(outcome).not.toBeInstanceOf(TimeoutError); |
| 133 | + expect(outcome).toBe("settled"); |
| 134 | + await settle(); |
| 135 | + expect(views.at(-1)).toBe("res_1:confirmed"); |
| 136 | +} |
| 137 | + |
| 138 | +test("until() sees a confirming frame that landed on the held store before until() was called", async () => { |
| 139 | + await reserveScenario(true); |
| 140 | +}); |
| 141 | + |
| 142 | +test("control: until() subscribed before the confirming frame lands (unchanged)", async () => { |
| 143 | + await reserveScenario(false); |
| 144 | +}); |
0 commit comments