Skip to content

Commit 83a9b87

Browse files
committed
fix(signals): a per-key optimistic guess survives a re-ask landing while its action is pending (#4329)
A derive that writes a keyed list through its draft (`d.length = 0; d.push(...rows)`, or `d.issues = rows`) replaced the guessed row with a fresh object, so the guess, which lives on the old row's target, left the screen before its action settled. The draft's new object at a guessed row's slot (by key in a keyed list) is now adopted by that row's target, as a reconcile landing already does: the guess stays until the commit (D-34) and guesses on other rows stay independent (D-33). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent d2323b0 commit 83a9b87

5 files changed

Lines changed: 399 additions & 8 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+
A per-key guess on `createOptimisticStore` stays on screen through a re-ask that lands while its action is pending (D-34, #4329). When the derive writes a keyed list through its draft (`d.length = 0; d.push(...rows)`, or `d.issues = rows`) under a row that carries a guess, the new object with that row's key is adopted by the guessed row, as a returned or reconciled landing already does: the row keeps its proxy, the guess stays over the new truth, and the screen shows the guess until the action commits. Guesses on different rows stay independent (D-33).

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export interface OptHooks {
3434
/** A user's `reconcile` on an optimistic family: the keyed diff written
3535
* into the draft. */
3636
reconcile(draft: any, incoming: any, keyFn: ((item: any) => any) | null): void;
37+
/** The derive's draft wrote `pb` on `t`: the new object at a guessed
38+
* row's slot (by key in a keyed list) is adopted by that row's target. */
39+
pair(t: StoreTarget, pb: Record<PropertyKey, any>): void;
3740
}
3841
// Set before any family is optimistic (`fam.opt`): a call site checks the
3942
// hooks first, so a build that does not import optimistic.ts folds the arm

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import type { Computed, Signal } from "../core/types.js";
5252
import type { Refreshable } from "../core/index.js";
5353
import { runProjectionComputed } from "./projection.js";
5454
import {
55+
adoptPB,
5556
cloneRaw,
5657
committed,
5758
getContainerNode,
@@ -67,7 +68,7 @@ import {
6768
wrap
6869
} from "./store.js";
6970
import { installOptHooks } from "./hooks.js";
70-
import { $OWNER, type StoreFamily, type StoreTarget } from "./target.js";
71+
import { $OWNER, lookupTarget, type StoreFamily, type StoreTarget } from "./target.js";
7172
import {
7273
$TARGET,
7374
$TRACK,
@@ -462,7 +463,40 @@ function laneSet(node: Signal<any>, value: unknown, lane: ReturnType<typeof txOf
462463
}
463464
}
464465

466+
/** D-34 for a per-key guess (#4329): the derive's draft wrote new objects
467+
* over a target holding guessed rows (`d.length = 0; d.push(...rows)`, or
468+
* `d.issues = rows`). Each container on the way down to a guessed row
469+
* adopts the new object at its slot — in a keyed list, the one with its
470+
* key — as a reconcile landing pairs them: the row keeps its target, its
471+
* leaves take the truth beneath their guesses, and the screen keeps the
472+
* guess until its hold commits. Rows nothing guessed are left as the
473+
* draft wrote them. */
474+
function pairRows(t: StoreTarget, pb: Record<PropertyKey, any>): void {
475+
const fam = t.fam!;
476+
const keyFn = fam.key;
477+
if (fam.overlaid)
478+
for (const ot of fam.overlaid) {
479+
let c: StoreTarget | null = ot;
480+
while (c !== null && c.u !== t) c = c.u;
481+
if (c === null) continue;
482+
const k = keyFn?.(c.v);
483+
const nv = Array.isArray(pb)
484+
? k !== undefined && pb.find(r => isWrappable(r) && sameKey(keyFn!(r), k))
485+
: pb[c.pk!];
486+
if (
487+
nv !== c.v &&
488+
isWrappable(nv) &&
489+
Array.isArray(nv) === Array.isArray(c.v) &&
490+
lookupTarget(nv, fam) === undefined
491+
) {
492+
if (c !== ot) pairRows(c, nv);
493+
adoptPB(c, nv);
494+
}
495+
}
496+
}
497+
465498
installOptHooks({
499+
pair: pairRows,
466500
draft: optimisticDraft,
467501
writes: notifyOptimisticWrites,
468502
arrangement,

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,13 +1142,18 @@ function notifyWrites(t: StoreTarget): void {
11421142
// Optimistic channel: a user's writes on an optimistic family become
11431143
// guesses on the nodes (lanes); the draft is discarded, the committed
11441144
// backing never touched.
1145-
if (optHooks && userWrite && t.fam?.opt === true) {
1146-
// The draft is discarded first: the diff reads the target (its nodes
1147-
// are born from the committed frame, not the draft).
1148-
t.pb = null;
1149-
t.wk = null;
1150-
t.pb = optHooks.writes(t, pb);
1151-
return;
1145+
if (optHooks && t.fam?.opt === true) {
1146+
if (userWrite) {
1147+
// The draft is discarded first: the diff reads the target (its nodes
1148+
// are born from the committed frame, not the draft).
1149+
t.pb = null;
1150+
t.wk = null;
1151+
t.pb = optHooks.writes(t, pb);
1152+
return;
1153+
}
1154+
// The derive's draft: new rows under guessed rows are paired with them
1155+
// first (D-34, #4329).
1156+
optHooks.pair(t, pb);
11521157
}
11531158
const old = t.v;
11541159
// Devtools mutation hook: full-key diff (dev-only cost) so unobserved

0 commit comments

Comments
 (0)