Skip to content

Commit b7ece8c

Browse files
committed
fix(signals): keep swapped raws distinct when a held adoption pairs a row
A held adoption that moves the same raw objects between slots ([A,B] -> [B,A], or a record's {x:A,y:B} -> {x:B,y:A}) paired A's target with B as staging on the first read, so after the commit both slots read B. A raw present in both frames is its own row: skip the pairing for it. The frame values are collected once per frame, so reading every row of a large adoption during the hold stays linear. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1
1 parent 48e6325 commit b7ece8c

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,16 @@ function heldKeyChanged(target: StoreTarget, key: PropertyKey, shape = true): bo
15861586
);
15871587
}
15881588

1589+
/** The values of a held frame, built once per frame: neither the committed
1590+
* frame nor the held staging changes while the hold stands (a draft writes a
1591+
* clone), so the set stays true for every first read of the hold. */
1592+
const frameRaws = new WeakMap<object, Set<unknown>>();
1593+
function rawsOf(frame: object): Set<unknown> {
1594+
let raws = frameRaws.get(frame);
1595+
if (raws === undefined) frameRaws.set(frame, (raws = new Set(Object.values(frame))));
1596+
return raws;
1597+
}
1598+
15891599
/** The same row at a slot, as the adoption pairs rows (reconcile.ts): two
15901600
* objects whose keys agree — positionally for an unkeyed family. */
15911601
export function sameRow(pv: any, nv: any, keyFn: ((item: any) => any) | null): boolean {
@@ -1620,7 +1630,11 @@ function adoptChild(target: StoreTarget, key: PropertyKey): boolean {
16201630
!isWrappable(nv) ||
16211631
Array.isArray(ov) !== Array.isArray(nv) ||
16221632
lookupTarget(ov, target.fam) !== undefined ||
1623-
lookupTarget(nv, target.fam) !== undefined
1633+
lookupTarget(nv, target.fam) !== undefined ||
1634+
// A raw in both frames (the same objects swapped or reordered) is its
1635+
// own row: pairing it with another would alias the two after the commit.
1636+
rawsOf(old).has(nv) ||
1637+
rawsOf(k._pendingValue as object).has(ov)
16241638
)
16251639
return false;
16261640
// A raw-marked value wraps to itself, a proxy to its own target: neither

‎packages/signals/tests/adoption-lazy-child-pairing-3712.test.ts‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,70 @@ describe("#3712 edges: more", () => {
297297
dispose();
298298
});
299299
});
300+
301+
describe("#3712 edges: the same raws swapped under a hold stay distinct rows", () => {
302+
for (const key of [undefined, null] as const) {
303+
it(`an array reorder of the same raws, read during the hold (key: ${key})`, async () => {
304+
const gate = deferred();
305+
const A = { id: "a", v: 0 };
306+
const B = { id: "b", v: 0 };
307+
const [server, setServer] = createSignal([A, B]);
308+
let store!: { id: string; v: number }[];
309+
let dispose!: () => void;
310+
const [on, setOn] = createSignal(false);
311+
createRoot(d => {
312+
dispose = d;
313+
[store] = createStore(() => server(), [], key === undefined ? undefined : { key });
314+
createRenderEffect(
315+
createMemo(() => on() && store[0].id),
316+
() => {}
317+
);
318+
});
319+
flush();
320+
const p = action(function* () {
321+
setServer([B, A]);
322+
yield gate.promise;
323+
})();
324+
flush();
325+
setOn(true);
326+
flush();
327+
gate.resolve();
328+
await p;
329+
flush();
330+
expect([store[0].id, store[1].id]).toEqual(["b", "a"]);
331+
expect(store[0]).not.toBe(store[1]);
332+
dispose();
333+
});
334+
}
335+
336+
it("a record swap of the same raws, read during the hold", async () => {
337+
const gate = deferred();
338+
const A = { name: "a" };
339+
const B = { name: "b" };
340+
const [server, setServer] = createSignal<Record<string, { name: string }>>({ x: A, y: B });
341+
let store!: Record<string, { name: string }>;
342+
let dispose!: () => void;
343+
const [on, setOn] = createSignal(false);
344+
createRoot(d => {
345+
dispose = d;
346+
[store] = createStore(() => server(), {});
347+
createRenderEffect(
348+
createMemo(() => on() && store.x.name),
349+
() => {}
350+
);
351+
});
352+
flush();
353+
const p = action(function* () {
354+
setServer({ x: B, y: A });
355+
yield gate.promise;
356+
})();
357+
flush();
358+
setOn(true);
359+
flush();
360+
gate.resolve();
361+
await p;
362+
flush();
363+
expect([store.x.name, store.y.name]).toEqual(["b", "a"]);
364+
dispose();
365+
});
366+
});

0 commit comments

Comments
 (0)