|
| 1 | +/** |
| 2 | + * #2951 — bare (transaction-less) optimistic store writes made while the |
| 3 | + * store's own truth is in flight must ride that refetch, not revert at plain |
| 4 | + * flush end. Optimistic state clears when truth lands or its transaction |
| 5 | + * settles — never on a timer. |
| 6 | + * |
| 7 | + * The broken shape: a click handler writes a plain dep (triggering a refetch |
| 8 | + * of the derived optimistic store) and pushes an optimistic row in the same |
| 9 | + * tick. The flush's transition adopted the write but nothing marked it |
| 10 | + * blocked — signal-form createOptimistic carries the pending async and the |
| 11 | + * override on ONE node (so transitionBlocked sees both), while the store form |
| 12 | + * splits them between the firewall computed and the store targets' layer. The |
| 13 | + * transition settled in the same flush that started the refetch, its settle |
| 14 | + * consumed the layer, and the next click's draft composed on committed base — |
| 15 | + * clobbering the still-rendered optimistic row instead of stacking on it. |
| 16 | + */ |
| 17 | +import { expect, test } from "vitest"; |
| 18 | +import { |
| 19 | + createEffect, |
| 20 | + createOptimisticStore, |
| 21 | + createRoot, |
| 22 | + createSignal, |
| 23 | + flush |
| 24 | +} from "../src/index.js"; |
| 25 | + |
| 26 | +const tick = () => new Promise(r => setTimeout(r, 0)); |
| 27 | + |
| 28 | +function setup() { |
| 29 | + let setCount!: (v: number) => void; |
| 30 | + let setS!: (fn: (s: { items: string[] }) => void) => void; |
| 31 | + let dispose!: () => void; |
| 32 | + const resolvers: ((items: string[]) => void)[] = []; |
| 33 | + const views: string[][] = []; |
| 34 | + |
| 35 | + createRoot(d => { |
| 36 | + dispose = d; |
| 37 | + const [count, _setCount] = createSignal(0); |
| 38 | + setCount = _setCount; |
| 39 | + const [store, _setS] = createOptimisticStore<{ items: string[] }>( |
| 40 | + async () => { |
| 41 | + count(); |
| 42 | + const items = await new Promise<string[]>(r => resolvers.push(r)); |
| 43 | + return { items }; |
| 44 | + }, |
| 45 | + { items: [] } |
| 46 | + ); |
| 47 | + setS = _setS; |
| 48 | + createEffect( |
| 49 | + () => store.items.slice(), |
| 50 | + v => { |
| 51 | + views.push(v); |
| 52 | + } |
| 53 | + ); |
| 54 | + }); |
| 55 | + return { setCount, setS, resolvers, views, dispose }; |
| 56 | +} |
| 57 | + |
| 58 | +test("#2951: consecutive bare writes stack on the same optimistic view until truth lands", async () => { |
| 59 | + const { setCount, setS, resolvers, views, dispose } = setup(); |
| 60 | + flush(); |
| 61 | + await tick(); |
| 62 | + resolvers[0](["A"]); |
| 63 | + await tick(); |
| 64 | + flush(); |
| 65 | + expect(views.at(-1)).toEqual(["A"]); |
| 66 | + |
| 67 | + // Click 1: plain dep write starts a (slow) refetch; the bare optimistic |
| 68 | + // push in the same tick must survive the flush that starts it. |
| 69 | + setCount(1); |
| 70 | + setS(d => { |
| 71 | + d.items.push("U2*"); |
| 72 | + }); |
| 73 | + flush(); |
| 74 | + await tick(); |
| 75 | + expect(views.at(-1)).toEqual(["A", "U2*"]); |
| 76 | + |
| 77 | + // Clicks 2 and 3 land while truth is still in flight: each draft composes |
| 78 | + // ON TOP of the live optimistic view (same primitive -> same transaction). |
| 79 | + setCount(2); |
| 80 | + setS(d => { |
| 81 | + expect(d.items.slice()).toEqual(["A", "U2*"]); |
| 82 | + d.items.push("U3*"); |
| 83 | + }); |
| 84 | + flush(); |
| 85 | + await tick(); |
| 86 | + expect(views.at(-1)).toEqual(["A", "U2*", "U3*"]); |
| 87 | + |
| 88 | + setCount(3); |
| 89 | + setS(d => { |
| 90 | + d.items.push("U4*"); |
| 91 | + }); |
| 92 | + flush(); |
| 93 | + await tick(); |
| 94 | + expect(views.at(-1)).toEqual(["A", "U2*", "U3*", "U4*"]); |
| 95 | + |
| 96 | + // Truth supersedes every tentative layer: the projection landing consumes |
| 97 | + // the overrides and the settled transaction sweeps the rest. |
| 98 | + const all = ["A", "U2", "U3", "U4"]; |
| 99 | + for (let i = 1; i < resolvers.length; i++) resolvers[i](all); |
| 100 | + await tick(); |
| 101 | + flush(); |
| 102 | + await tick(); |
| 103 | + flush(); |
| 104 | + expect(views.at(-1)).toEqual(all); |
| 105 | + dispose(); |
| 106 | +}); |
| 107 | + |
| 108 | +test("#2951: write order within the tick does not matter (optimistic write first)", async () => { |
| 109 | + const { setCount, setS, resolvers, views, dispose } = setup(); |
| 110 | + flush(); |
| 111 | + await tick(); |
| 112 | + resolvers[0](["A"]); |
| 113 | + await tick(); |
| 114 | + flush(); |
| 115 | + |
| 116 | + setS(d => { |
| 117 | + d.items.push("U2*"); |
| 118 | + }); |
| 119 | + setCount(1); |
| 120 | + flush(); |
| 121 | + await tick(); |
| 122 | + expect(views.at(-1)).toEqual(["A", "U2*"]); |
| 123 | + |
| 124 | + setS(d => { |
| 125 | + d.items.push("U3*"); |
| 126 | + }); |
| 127 | + setCount(2); |
| 128 | + flush(); |
| 129 | + await tick(); |
| 130 | + expect(views.at(-1)).toEqual(["A", "U2*", "U3*"]); |
| 131 | + |
| 132 | + const all = ["A", "U2", "U3"]; |
| 133 | + for (let i = 1; i < resolvers.length; i++) resolvers[i](all); |
| 134 | + await tick(); |
| 135 | + flush(); |
| 136 | + await tick(); |
| 137 | + flush(); |
| 138 | + expect(views.at(-1)).toEqual(all); |
| 139 | + dispose(); |
| 140 | +}); |
| 141 | + |
| 142 | +test("#2951: a bare write during an already in-flight refetch (later tick) also holds", async () => { |
| 143 | + const { setCount, setS, resolvers, views, dispose } = setup(); |
| 144 | + flush(); |
| 145 | + await tick(); |
| 146 | + resolvers[0](["A"]); |
| 147 | + await tick(); |
| 148 | + flush(); |
| 149 | + |
| 150 | + // Start the refetch alone. |
| 151 | + setCount(1); |
| 152 | + flush(); |
| 153 | + await tick(); |
| 154 | + expect(views.at(-1)).toEqual(["A"]); |
| 155 | + |
| 156 | + // A later tick's bare optimistic write must still ride the in-flight truth. |
| 157 | + setS(d => { |
| 158 | + d.items.push("U2*"); |
| 159 | + }); |
| 160 | + flush(); |
| 161 | + await tick(); |
| 162 | + expect(views.at(-1)).toEqual(["A", "U2*"]); |
| 163 | + |
| 164 | + resolvers[1](["A", "U2"]); |
| 165 | + await tick(); |
| 166 | + flush(); |
| 167 | + await tick(); |
| 168 | + flush(); |
| 169 | + expect(views.at(-1)).toEqual(["A", "U2"]); |
| 170 | + dispose(); |
| 171 | +}); |
| 172 | + |
| 173 | +test("#2951: bare write on a derived store with settled truth keeps flash semantics", async () => { |
| 174 | + const { setS, resolvers, views, dispose } = setup(); |
| 175 | + flush(); |
| 176 | + await tick(); |
| 177 | + resolvers[0](["A"]); |
| 178 | + await tick(); |
| 179 | + flush(); |
| 180 | + expect(views.at(-1)).toEqual(["A"]); |
| 181 | + |
| 182 | + // No refetch in flight: an ambient optimistic write with no transaction and |
| 183 | + // no truth on the way reverts at plain flush end, as before. |
| 184 | + setS(d => { |
| 185 | + d.items.push("X*"); |
| 186 | + }); |
| 187 | + flush(); |
| 188 | + expect(views.at(-1)).toEqual(["A"]); |
| 189 | + dispose(); |
| 190 | +}); |
0 commit comments