Skip to content

Commit ac28157

Browse files
ryansolidclaude
andauthored
fix(signals): optimistic store guesses survive re-asks, answer latest() and stay per-action (#4329) (#4330)
* 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 * fix(signals): optimistic store corrections answer latest() and keep actions independent (#4329) - The draft path's pairing reuses reconcile: the container above a guessed row reconciles the new object at its slot, so both landing paths share one keyed pairing and one leaf diff. - reconcile judges a key under a guess against the guess too, so a truth equal to the covered value still answers it: latest() reads the truth, the screen keeps the guess until the commit (D-34). - supersede no longer joins the landing's tick to the parent hold; the parent takes the node, and passes deriving from the held truth join on their own. One re-ask answering two actions' guesses no longer ties them together (D-33). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * chore(size): raise the store scenario cap for #4329's fix Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent d2323b0 commit ac28157

8 files changed

Lines changed: 492 additions & 13 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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 at that row's slot is reconciled into the guessed row, as a returned landing is: the row keeps its proxy and the screen shows the guess until the action commits.
6+
7+
On both landing paths, a guessed key is judged against its guess, so `latest()` answers the truth even when the truth equals the value the guess covered. A correction or confirmation no longer joins the whole landing to the guess's hold. When one re-ask answers guesses from two actions, they stay independent (D-33): each row reverts or commits when its own action settles, and rows with no guess show their new truth at the landing.

‎packages/signals/src/core/lanes.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,11 @@ export function laneStage(
380380
* at the parent's landing — while the display keeps what it shows until
381381
* then. A confirmation is silent (A17): the truth stays staged beneath the
382382
* guess for the parent's commit, the guess stays the value; only a reader
383-
* of the truth (CONFIG_AUTHORITATIVE) is told its view changed. */
383+
* of the truth (CONFIG_AUTHORITATIVE) is told its view changed. Either way
384+
* the parent takes this node, not the tick that carried the truth (D-33):
385+
* one landing answering two actions' guesses (a store re-ask over two
386+
* rows) must not tie them together; a pass that derives from the held
387+
* truth joins the parent on its own (`joinPass`). */
384388
/** Observe: the correction round is replacing a guess with the value it
385389
* covered (no landing beneath it) — `optimisticReverted`'s "reverted". */
386390
let reverting = false;
@@ -416,7 +420,6 @@ export function supersede(n: Signal<any> | Computed<any>, value: unknown, change
416420
GlobalQueue._confirmed?.(l);
417421
}
418422
n._pendingValue = value;
419-
joinFuture(parent);
420423
}
421424

422425
/** A lane's end, two ways. `into === null`: the parent landed — the lane's

‎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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
import type { Computed, Signal } from "../core/types.js";
5252
import type { Refreshable } from "../core/index.js";
5353
import { runProjectionComputed } from "./projection.js";
54+
import { reconcileState } from "./reconcile.js";
5455
import {
5556
cloneRaw,
5657
committed,
@@ -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,31 @@ 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`). The container on the way down to each guessed row
469+
* reconciles the new object at its slot — in a keyed list, the one with
470+
* the row's key — exactly as a returned landing would: rows match by key,
471+
* a guessed row keeps its target, and its leaves are judged against their
472+
* guesses. Rows nothing guessed are left as the draft wrote them. */
473+
function pairRows(t: StoreTarget, pb: Record<PropertyKey, any>): void {
474+
const keyFn = t.fam!.key;
475+
for (let c of t.fam!.overlaid ?? []) {
476+
// The guessed row, or its ancestor directly under `t` (none: skip).
477+
while (c.u !== t) if ((c = c.u!) === null) break;
478+
if (c === null) continue;
479+
const k = keyFn?.(c.v);
480+
const nv = Array.isArray(pb)
481+
? k !== undefined && pb.find(r => isWrappable(r) && sameKey(keyFn!(r), k))
482+
: pb[c.pk!];
483+
// A new object only: not `c`'s own, nor one another row serves
484+
// (pairing it would alias the two).
485+
if (isWrappable(nv) && !lookupTarget(nv, t.fam)) reconcileState(nv, c.px, keyFn, true);
486+
}
487+
}
488+
465489
installOptHooks({
490+
pair: pairRows,
466491
draft: optimisticDraft,
467492
writes: notifyOptimisticWrites,
468493
arrangement,

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,10 @@ function applyAdopt(t: StoreTarget, incoming: any, keyFn: KeyFn | null, proj = f
225225
let nodesHit = 0;
226226
for (const k in incoming) {
227227
const nv = (incoming as any)[k];
228-
const ov = (old as any)[k];
228+
let ov = (old as any)[k];
229+
// A key under a guess is judged against the guess too (D-34): a truth
230+
// equal to the value the guess covered still answers it.
231+
if (optHooks && ov === nv && prevView !== old) ov = (prevView as any)[k];
229232
const isObj = nv !== null && typeof nv === "object";
230233
if (
231234
ov === nv &&

‎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)