|
1 | | -/** |
2 | | - * solidjs/solid-router#619, #620 — an optimistic store whose source refetches |
3 | | - * because of a write made OUTSIDE the action's transaction (a router action |
4 | | - * calls `revalidate()` after an `await`, past the action's context). |
5 | | - * |
6 | | - * The store derives synchronously from an async memo, so the refetch is not a |
7 | | - * declared flight of the store (`fam.ft` is null) and belongs to the outside |
8 | | - * write's transaction. The action's transaction parked on it and nothing |
9 | | - * re-entered it when the refetch landed: held writes (mapArray's index |
10 | | - * signals) never committed (#619), and an unchanged truth never lifted the |
11 | | - * overlay (#620). |
12 | | - */ |
| 1 | +// solidjs/solid-router#619, #620: an optimistic store refetched by a write outside the action. |
13 | 2 | import { |
14 | 3 | action, |
15 | 4 | createMemo, |
@@ -120,4 +109,68 @@ describe("optimistic store refetched by a write outside the action (router#619)" |
120 | 109 | "a:0 b:1 c:2" |
121 | 110 | ]); |
122 | 111 | }); |
| 112 | + |
| 113 | + it("keeps the refetch hold when an unrelated pending action stamped the store first", async () => { |
| 114 | + let db: Card[] = [ |
| 115 | + { id: "a", order: 0 }, |
| 116 | + { id: "b", order: 1 }, |
| 117 | + { id: "c", order: 2 } |
| 118 | + ]; |
| 119 | + const [version, setVersion] = createSignal(0); |
| 120 | + const frames: string[] = []; |
| 121 | + let stuck!: () => Promise<void>; |
| 122 | + let swap!: () => Promise<void>; |
| 123 | + createRoot(() => { |
| 124 | + const data = createMemo(async () => { |
| 125 | + version(); |
| 126 | + await tick(30); |
| 127 | + return db.map(c => ({ ...c })); |
| 128 | + }); |
| 129 | + const [cards, setCards] = createOptimisticStore(() => data(), [] as Card[]); |
| 130 | + stuck = action(function* () { |
| 131 | + setCards(list => { |
| 132 | + list[2].order = 5; |
| 133 | + }); |
| 134 | + yield new Promise<void>(() => {}); |
| 135 | + }); |
| 136 | + swap = action(function* () { |
| 137 | + setCards(list => { |
| 138 | + list[0].order = 1; |
| 139 | + list[1].order = 0; |
| 140 | + }); |
| 141 | + yield tick(5); |
| 142 | + db = [ |
| 143 | + { id: "a", order: 1 }, |
| 144 | + { id: "b", order: 0 }, |
| 145 | + { id: "c", order: 2 } |
| 146 | + ]; |
| 147 | + setVersion(v => v + 1); |
| 148 | + }); |
| 149 | + const rows = mapArray( |
| 150 | + () => [...cards].sort((x, y) => x.order - y.order), |
| 151 | + (r, i) => () => `${r.id}:${i()}` |
| 152 | + ); |
| 153 | + createRenderEffect( |
| 154 | + () => |
| 155 | + rows() |
| 156 | + .map(f => f()) |
| 157 | + .join(" "), |
| 158 | + v => { |
| 159 | + frames.push(v); |
| 160 | + } |
| 161 | + ); |
| 162 | + }); |
| 163 | + flush(); |
| 164 | + await tick(40); |
| 165 | + flush(); |
| 166 | + stuck(); |
| 167 | + flush(); |
| 168 | + await tick(5); |
| 169 | + flush(); |
| 170 | + swap(); |
| 171 | + flush(); |
| 172 | + await tick(100); |
| 173 | + flush(); |
| 174 | + expect(frames).toEqual(["a:0 b:1 c:2", "b:1 a:0 c:2", "b:0 a:1 c:2"]); |
| 175 | + }); |
123 | 176 | }); |
0 commit comments