Skip to content

Commit c410709

Browse files
ryansolidClaude via Cursor
andcommitted
fix(signals): an optimistic add/delete survives its only structural observer leaving (S7, structural half; review of #3523)
An optimistic add or delete lives on the key's PRESENCE node (target.h, getHasNode) as its membership override. Its unobserved callback released the node the moment the last structural observer left — with the override on it — so `in`, Object.keys and descriptors fell back to committed structure while the action was live: the structural twin of the value-slot bug S7 fixed. The presence node now defers its release through deferSlotRelease while it carries an override or a staged value; the transient-node sweep runs its callback once the state resolves. Pinned for optimistic add and delete: the only structural observer gates away, in / Object.keys / getOwnPropertyDescriptor keep the optimistic structure while the action is live, settlement restores committed structure and releases target.h[key]. Suite green; oracles and matrix identical; +35 B minified store. Co-authored-by: Claude via Cursor <[email protected]>
1 parent 9db33cf commit c410709

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
An optimistic add or delete on a store now survives its only structural observer leaving: the key's presence node was released with the membership override on it, so `in`, `Object.keys` and property descriptors fell back to the committed structure while the action was still live. The release now waits for the flush that resolves the override, as the value slot's does.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ export function getHasNode(
477477
equals: isEqual,
478478
unobserved() {
479479
if ((created as any)._x?._affectsCount) return;
480+
// The structural twin of the value slot's rule (setSlotUnobserved,
481+
// S7): an optimistic add/delete lives on this presence node as its
482+
// override — releasing it with the override on would let `in`,
483+
// `Object.keys` and descriptors fall back to committed structure
484+
// while the action is live. Defer to the flush that resolves it.
485+
if (hasActiveOverride(created) || created._pendingValue !== NOT_PENDING)
486+
return deferSlotRelease(created);
480487
if (target.h && target.h[key] === created) {
481488
delete target.h[key];
482489
unlinkFirewallChild(created);

‎packages/signals/tests/posture-store-parity.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,53 @@ describe("S7 — optimistic override, the only reader gated away: the override i
479479
});
480480
});
481481

482+
/** S7, the structural half (review of #3523): an optimistic add or delete
483+
* lives on the key's PRESENCE node (`target.h`, getHasNode) as its override.
484+
* Its unobserved callback released the node the moment the last structural
485+
* observer left — with the override on it — so `in`, `Object.keys` and
486+
* descriptors fell back to committed structure while the action was live.
487+
* The presence node now defers its release like the value slot does. */
488+
describe("S7 (structural) — optimistic add/delete survives the only structural observer gating away", () => {
489+
for (const kind of ["add", "delete"] as const) {
490+
it(`optimistic ${kind}: in / Object.keys / descriptor keep the optimistic structure while live; settle restores committed and releases the presence node`, async () => {
491+
const [s, setS] = createOptimisticStore<Record<string, number>>(
492+
kind === "add" ? {} : { k: 1 }
493+
);
494+
const [show, setShow] = createSignal(true);
495+
createRoot(() => {
496+
createRenderEffect(
497+
() => (show() ? "k" in s : "gated"),
498+
() => {}
499+
);
500+
});
501+
flush();
502+
let release!: () => void;
503+
action(function* () {
504+
setS(d => {
505+
if (kind === "add") d.k = 5;
506+
else delete d.k;
507+
});
508+
yield new Promise<void>(res => (release = res));
509+
})();
510+
flush();
511+
const live = kind === "add";
512+
const structure = () => [
513+
"k" in s,
514+
Object.keys(s).includes("k"),
515+
Object.getOwnPropertyDescriptor(s, "k") !== undefined
516+
];
517+
expect(structure()).toEqual([live, live, live]);
518+
setShow(false); // the only structural observer gates away
519+
flush();
520+
expect(structure()).toEqual([live, live, live]);
521+
release();
522+
await settle();
523+
expect(structure()).toEqual([!live, !live, !live]);
524+
expect(((s as any)[$TARGET].h ?? {}).k).toBeUndefined();
525+
});
526+
}
527+
});
528+
482529
/** S8 (fixed, 3b step 6c): A18 supersession for a store node read UNTRACKED
483530
* inside a derivation. A derived optimistic store's own truth landed (2)
484531
* while an action's edit (3) is displayed: the signal serves a deriving

0 commit comments

Comments
 (0)