|
| 1 | +/** |
| 2 | + * #3548 — overlapping optimistic moves duplicate an item across filtered |
| 3 | + * keyed lists. |
| 4 | + * |
| 5 | + * Reduced from the report's playground: a `createOptimisticStore` backed by |
| 6 | + * an async iterable (manual `publish`), two cards, three lanes each rendered |
| 7 | + * as a keyed `mapArray` over `cards.filter(c => c.lane === lane)`. Four |
| 8 | + * optimistic moves via actions overlap; the authoritative result of the FIRST |
| 9 | + * move is published and only that action resolves while the later ones stay |
| 10 | + * pending. |
| 11 | + * |
| 12 | + * 1. card 0 → lane 1 (order 1) [action 1] |
| 13 | + * 2. card 0 → lane 2 (order 6) [action 2] |
| 14 | + * 3. card 1 → lane 1 (order 9) [action 3] |
| 15 | + * 4. publish {card0: lane 1, card1: lane 0}; resolve action 1 only; task turn |
| 16 | + * 5. card 1 within lane 1 (order 3) [action 4] |
| 17 | + * |
| 18 | + * Reported: after step 5 lane 1 renders cards 0 and 1 AND lane 2 renders |
| 19 | + * card 0 — card 0 is on screen twice although the store places it once (lane |
| 20 | + * 2). Expected: lane 1 = [1], lane 2 = [0], lane 0 = []. |
| 21 | + * |
| 22 | + * The invariant pinned: in every frame the lanes publish, no card id appears |
| 23 | + * in two lanes, and the rendered placement equals the store's own placement. |
| 24 | + */ |
| 25 | +import { |
| 26 | + action, |
| 27 | + createOptimisticStore, |
| 28 | + createRenderEffect, |
| 29 | + createRoot, |
| 30 | + flush, |
| 31 | + mapArray |
| 32 | +} from "../src/index.js"; |
| 33 | + |
| 34 | +afterEach(() => flush()); |
| 35 | + |
| 36 | +interface Card { |
| 37 | + id: number; |
| 38 | + lane: number; |
| 39 | + order: number; |
| 40 | + version: number; |
| 41 | +} |
| 42 | +interface Source { |
| 43 | + cards: Card[]; |
| 44 | +} |
| 45 | + |
| 46 | +describe("#3548 overlapping optimistic moves across filtered keyed lists", () => { |
| 47 | + it("never renders a card in two lanes while an older move lands under newer pending moves", async () => { |
| 48 | + let source: Source = { |
| 49 | + cards: [0, 1].map(id => ({ id, lane: 0, order: 0, version: 0 })) |
| 50 | + }; |
| 51 | + let publish!: (value: Source) => void; |
| 52 | + async function* stream() { |
| 53 | + yield source; |
| 54 | + while (true) yield await new Promise<Source>(resolve => (publish = resolve)); |
| 55 | + } |
| 56 | + |
| 57 | + const pending = new Map<number, () => void>(); |
| 58 | + const frames: { lanes: number[][]; store: number[] }[] = []; |
| 59 | + let view!: Source; |
| 60 | + let dispose!: () => void; |
| 61 | + |
| 62 | + createRoot(d => { |
| 63 | + dispose = d; |
| 64 | + const [v, setView] = createOptimisticStore<Source>(stream, source); |
| 65 | + view = v; |
| 66 | + |
| 67 | + const move = action(function* (id: number, lane: number, order: number, version: number) { |
| 68 | + setView(state => { |
| 69 | + const card = state.cards.find(card => card.id === id)!; |
| 70 | + Object.assign(card, { lane, order, version }); |
| 71 | + }); |
| 72 | + yield new Promise<void>(resolve => pending.set(version, resolve)); |
| 73 | + }); |
| 74 | + (globalThis as any).__move3548 = move; |
| 75 | + |
| 76 | + // Three lanes, each a keyed `<For>` over the filtered+sorted store — |
| 77 | + // the `each` getter runs inside mapArray's own computation, as in |
| 78 | + // `<For each={view.cards.filter(...).sort(...)} keyed={c => c.id}>`. |
| 79 | + const lanes = [0, 1, 2].map(lane => |
| 80 | + mapArray( |
| 81 | + () => view.cards.filter(c => c.lane === lane).sort((a, b) => a.order - b.order), |
| 82 | + card => card, |
| 83 | + { keyed: (c: Card) => c.id } |
| 84 | + ) |
| 85 | + ); |
| 86 | + |
| 87 | + // The DOM stand-in: one frame per flush, each lane's rendered ids. |
| 88 | + createRenderEffect( |
| 89 | + () => ({ |
| 90 | + lanes: lanes.map(lane => lane().map(card => card().id)), |
| 91 | + store: view.cards.map(c => c.lane) |
| 92 | + }), |
| 93 | + frame => { |
| 94 | + frames.push(frame); |
| 95 | + } |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + const move = (globalThis as any).__move3548 as ( |
| 100 | + id: number, |
| 101 | + lane: number, |
| 102 | + order: number, |
| 103 | + version: number |
| 104 | + ) => Promise<void>; |
| 105 | + const start = (id: number, lane: number, order: number, version: number) => { |
| 106 | + void move(id, lane, order, version); |
| 107 | + flush(); |
| 108 | + }; |
| 109 | + // First yield lands. |
| 110 | + flush(); |
| 111 | + await Promise.resolve(); |
| 112 | + await Promise.resolve(); |
| 113 | + flush(); |
| 114 | + expect(frames.at(-1)!.lanes).toEqual([[0, 1], [], []]); |
| 115 | + |
| 116 | + start(0, 1, 1, 1); // 1. card 0 → lane 1 |
| 117 | + start(0, 2, 6, 2); // 2. card 0 → lane 2 |
| 118 | + start(1, 1, 9, 3); // 3. card 1 → lane 1 |
| 119 | + expect(frames.at(-1)!.lanes).toEqual([[], [1], [0]]); |
| 120 | + |
| 121 | + // 4. authoritative result of action 1 lands; only action 1 resolves. |
| 122 | + // Mainline provenance: it supersedes card 0's newer override for the |
| 123 | + // graph (A18), while the screen keeps the override until the merged |
| 124 | + // transaction (actions 2–4) commits (A18 (c)). |
| 125 | + source = { |
| 126 | + cards: [ |
| 127 | + { id: 0, lane: 1, order: 1, version: 1 }, |
| 128 | + { id: 1, lane: 0, order: 0, version: 0 } |
| 129 | + ] |
| 130 | + }; |
| 131 | + publish(source); |
| 132 | + pending.get(1)!(); |
| 133 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 134 | + flush(); |
| 135 | + |
| 136 | + // 5. card 1 within lane 1 — a fresh lane reaching lane 1's list, which |
| 137 | + // also derives from card 0's superseded field. |
| 138 | + start(1, 1, 3, 4); |
| 139 | + |
| 140 | + const last = frames.at(-1)!; |
| 141 | + // Store's own placement: card 0 in lane 2, card 1 in lane 1. |
| 142 | + expect(view.cards.map(c => c.lane)).toEqual([2, 1]); |
| 143 | + |
| 144 | + // No frame ever shows a card in two lanes, and every frame's rendering |
| 145 | + // agrees with the store's placement read in the same effect. |
| 146 | + for (const frame of frames) { |
| 147 | + const seen = new Map<number, number[]>(); |
| 148 | + frame.lanes.forEach((ids, lane) => |
| 149 | + ids.forEach(id => seen.set(id, [...(seen.get(id) ?? []), lane])) |
| 150 | + ); |
| 151 | + for (const [id, inLanes] of seen) { |
| 152 | + expect( |
| 153 | + inLanes, |
| 154 | + `card ${id} rendered in lanes ${inLanes} — frame ${JSON.stringify(frame)}` |
| 155 | + ).toHaveLength(1); |
| 156 | + } |
| 157 | + const fromStore = [[], [], []] as number[][]; |
| 158 | + frame.store.forEach((lane, id) => fromStore[lane].push(id)); |
| 159 | + expect( |
| 160 | + frame.lanes.map(l => [...l].sort()), |
| 161 | + `rendered lanes disagree with the store — frame ${JSON.stringify(frame)}` |
| 162 | + ).toEqual(fromStore); |
| 163 | + } |
| 164 | + expect(last.lanes).toEqual([[], [1], [0]]); |
| 165 | + |
| 166 | + // Settle the remaining actions: the merged transaction commits, the |
| 167 | + // overrides drop, and the published truth (card 0 in lane 1, card 1 in |
| 168 | + // lane 0) reveals to every reader — the lane-pass readers replayed at the |
| 169 | + // commit included. |
| 170 | + pending.get(2)!(); |
| 171 | + pending.get(3)!(); |
| 172 | + pending.get(4)!(); |
| 173 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 174 | + flush(); |
| 175 | + expect(view.cards.map(c => c.lane)).toEqual([1, 0]); |
| 176 | + expect(frames.at(-1)!).toEqual({ lanes: [[1], [0], []], store: [1, 0] }); |
| 177 | + dispose(); |
| 178 | + delete (globalThis as any).__move3548; |
| 179 | + }); |
| 180 | +}); |
0 commit comments