Skip to content

Commit ecab102

Browse files
committed
refactor(signals): let liveTransition take null and trim the #619 test header
1 parent 9fc0b9f commit ecab102

2 files changed

Lines changed: 69 additions & 17 deletions

File tree

‎packages/signals/src/store/next/optimistic.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,8 @@ function installNextBlockedHalf(): void {
152152
// Ownership is declared (#3146): only the flight's OWN transaction
153153
// parks on the flight (the #2951 anchor routed the bare write there).
154154
// A transaction that merely brushed the store never waits for truth
155-
// it does not carry. An undeclared flight (pending through an upstream
156-
// source) belongs to the transaction its recompute ran in (router#619).
157-
const owner = fam!.ft ?? fw._transition;
158-
const ft = owner != null ? liveTransition(owner) : null;
155+
// it does not carry. An undeclared flight is owned by the firewall's stamp.
156+
const ft = liveTransition(fam!.ft ?? fw._transition);
159157
if (ft !== null && ft !== currentTransition(transition)) continue;
160158
if (familyHasLiveOverrides(fam!)) return true;
161159
}
@@ -358,7 +356,8 @@ export function createOptimisticStoreNext<T extends object = {}>(
358356

359357
/** Resolve a retained transition through its merge chain (`_done` holds the
360358
* merge target while merged, `true` once settled). Null = dead. */
361-
function liveTransition(txn: Transition): Transition | null {
359+
function liveTransition(txn: Transition | null): Transition | null {
360+
if (txn === null) return null;
362361
while (typeof txn._done === "object") txn = txn._done as Transition;
363362
return txn._done === true ? null : txn;
364363
}

‎packages/signals/tests/optimistic-outside-refetch-619.test.ts‎

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,4 @@
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.
132
import {
143
action,
154
createMemo,
@@ -120,4 +109,68 @@ describe("optimistic store refetched by a write outside the action (router#619)"
120109
"a:0 b:1 c:2"
121110
]);
122111
});
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+
});
123176
});

0 commit comments

Comments
 (0)