Skip to content

Commit 492a84b

Browse files
committed
feat(createDeferred): carry #3710's pins; a held re-ask joins nothing (D11)
Ports #3710's signals tests (D1-D11, mizulu's graph), its web and hydration specs and the deferred-settled-element harness scenario, and makes them pass: - D11: an answered deferred node carries CONFIG_LAGS; recompute's T4 arm does not join its held re-pass to the hold, and the wrapper serves the staged landing as the pass's value, so the write commits mainline and the hold keeps the landing it took. - until over a derivation is released by an equal-value landing: the mark's repoll re-runs truth readers too (not through a derived store, whose leaves re-run them at the derive's write). - The lag walk hops a store slot to its family's derive. The disposal pin is rewritten to this PR's behaviour (the mark drops at disposal). Treeshake pins cover the deferred module and the mark core. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01S2HrYgWeGJX6hKw7bD5LQN
1 parent 35a24e0 commit 492a84b

12 files changed

Lines changed: 1758 additions & 24 deletions

‎packages/signals/docs/RULES-DERIVED.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,12 +345,13 @@ An `<Errored>` flipping to its fallback is a boundary going back to its fallback
345345
- **Nothing waits for it.** A change that re-asks it holds nothing on its account: its readers keep the answer they show, and the rest of the change shows now. An unready source it reads (`createDeferred(() => query())`) is the same: the node keeps its answer, nothing pends through it.
346346
- **Its landing is a change of its own.** It shows when it lands, unless it lands into a hold (next bullet); an unrelated hold does not keep it back.
347347
- **It never leads.** A re-ask that read a held value derived from that hold's world, so its answer lands into that hold and shows with its commit (F-7). Its readers that also read a held value see the committed one, as any reader does.
348+
- **A held answer stays held.** Once a hold has taken its landing (a reader downstream is pending and staged it), a change that re-asks it still holds nothing: the change shows now, and the hold keeps the landing it took and shows it at its commit.
348349
- **The verdict sees it.** `isPending` of the node, and of everything derived from it (a memo over it, a derived store's keys), is `true` from the re-ask until the landing (F-9: a new question). A `refresh()` re-ask is quiet. The verdict and the landed value change in one frame.
349350
- **Truth readers wait for it.** `await refresh(d)` delivers the landing; `until`'s predicate does not see the stale answer of a source it derives from (the reader's own declaration: `resolve` takes the first settled value and does not wait).
350351
- **Errors are not lag.** A rejected re-ask errors like any memo (F-12); the stale answer never masks it.
351352
- **On the server it is `createMemo`** (everything is first load there).
352353

353-
**Collapses:** `docs/create-deferred.md` D1–D10 (2026-09-08; its lane-landing mechanism is superseded, see its §6). **Status:** standing, built 2026-10-14 (`tests/create-deferred.test.ts`).
354+
**Collapses:** `docs/create-deferred.md` D1–D11 (2026-09-08, D11 ruled 2026-10-04; its lane-landing mechanism is superseded, see its §6). **Status:** standing, built 2026-10-14 (`tests/create-deferred.test.ts`, `tests/create-deferred-3710.test.ts`).
354355

355356
---
356357

‎packages/signals/docs/RULES-INDEX.md‎

Lines changed: 7 additions & 7 deletions
Large diffs are not rendered by default.

‎packages/signals/docs/create-deferred.md‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,11 @@ heavy chart lags) is real but narrow. Not forbidden, not advertised.
415415

416416
### 6.0 As built (2026-10-14, L2)
417417

418-
`src/deferred.ts`, pay-for-use; core carries two small edits (`recompute`
419-
keeps `REACTIVE_REASK` through the pass, and `watch` asks `_lagging` for
420-
truth readers).
418+
`src/deferred.ts`, pay-for-use; core carries three small edits (`recompute`
419+
keeps `REACTIVE_REASK` through the pass and skips the T4 join for a
420+
`CONFIG_LAGS` node, and `watch` asks `_lagging` for truth readers). It
421+
carries #3710's pins (`tests/create-deferred-3710.test.ts`, D1–D11), and
422+
supersedes that PR.
421423

422424
- **Read side: the loading window.** Once the node has a real answer, every
423425
pass opens `_loading` before it asks, so a flight serves the committed
@@ -431,14 +433,22 @@ truth readers).
431433
a held one means the pass read the held world (an input changed after the
432434
pass would have superseded the flight), so the node is put on that hold
433435
(`holdNode`) and `setSignal` stages the answer there.
434-
- **Verdict: the mark core** (`src/marks.ts`, split out of `affects.ts`):
436+
- **D11: a held re-ask joins nothing.** When a downstream hold staged the
437+
node's landing, a write that re-asks it re-passes a held node. The node
438+
carries `CONFIG_LAGS` once answered, so `recompute` does not join the
439+
pass to the hold (the write commits mainline), and the wrapper serves the
440+
staged landing as the pass's value, so it is not restaged over with the
441+
committed one.
442+
- **Verdict: the mark core** (`src/affects-marks.ts`, split out of `affects.ts`):
435443
a mark whose lifetime is the flight, not counted while a `refresh()`
436444
re-ask is in flight. Coverage is the probe-time dependency walk, which
437445
now hops a store slot to its family's derive; the repoll reaches a derived
438-
store's leaf readers through the derive's `_family`.
446+
store's leaf readers through the derive's `family`.
439447
- **Truth readers** (`refresh`'s waiter, `until`): `watch` throws
440448
`NotReadyError` for a deferred flight the value derives from
441-
(`GlobalQueue._lagging`), so they park and the landing re-runs them.
449+
(`GlobalQueue._lagging`), so they park; the mark's repoll at the landing
450+
re-runs them even when the landing equals the committed value (not through
451+
a derived store, whose leaves re-run them when its derive writes).
442452
- **Streams:** the flight ends at the first yield.
443453

444454
The sections below are the original sketch, kept for the reasoning.

‎packages/signals/src/affects-marks.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// start and end (`repoll`): the only notification a mark makes.
1414

1515
import {
16+
CONFIG_AUTHORITATIVE,
1617
CONFIG_SLOT_NODE,
1718
CONFIG_VERDICT,
1819
REACTIVE_RECOMPUTING_DEPS,
@@ -30,16 +31,23 @@ let active = 0;
3031

3132
/** The verdict readers downstream of `node` — through derivations, stopping
3233
* at effects — re-derive: the mark's one notification (a probe's answer
33-
* changed with no value notifying anyone). */
34-
export function repoll(node: Marked, seen = new Set<Computed<any>>()): void {
34+
* changed with no value notifying anyone). Truth readers (`until`, the
35+
* `refresh` waiter) too, which a lagging flight parked (deferred.ts) — but
36+
* not through a derived store, whose leaves change only once its derive
37+
* re-runs: they re-derive at that write. */
38+
export function repoll(
39+
node: Marked,
40+
seen = new Set<Computed<any>>(),
41+
readers = CONFIG_VERDICT | CONFIG_AUTHORITATIVE
42+
): void {
3543
for (let s = node._subs; s !== null; s = s._nextSub) {
3644
const sub = s._sub;
3745
if (seen.has(sub)) continue;
3846
seen.add(sub);
39-
if (sub._config & CONFIG_VERDICT) enqueueSub(sub);
40-
if (!(sub as any)._type) repoll(sub, seen);
47+
if (sub._config & readers) enqueueSub(sub);
48+
if (!(sub as any)._type) repoll(sub, seen, readers);
4149
// A derived store's derive: its leaves' readers derive from it too.
42-
(sub as any).family?.((n: Marked) => repoll(n, seen));
50+
(sub as any).family?.((n: Marked) => repoll(n, seen, CONFIG_VERDICT));
4351
}
4452
}
4553

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export const CONFIG_VERDICT = 1 << 14;
140140
* promise over an expression (`resolve`/`until`, signals.ts: the pass reads
141141
* the error and rejects, or re-asks a pending source). */
142142
export const CONFIG_REDERIVE = 1 << 15;
143+
/** An answered `createDeferred` node (deferred.ts): held because a
144+
* downstream hold staged its landing, its re-pass is a lagging question,
145+
* not an answer, and joins no transaction (D-40). */
146+
export const CONFIG_LAGS = 1 << 19;
143147
/** The reader wants the truth, not the lane's guess (`until`, signals.ts):
144148
* a read of displayed optimism serves the base the guess covers, makes the
145149
* pass no lane's, and the truth landing wakes it even when it confirms the

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
CONFIG_PROMOTED,
2929
CONFIG_STAGED,
3030
CONFIG_HELD_FRAME,
31+
CONFIG_LAGS,
3132
CAUGHT_REARMED,
3233
CONFIG_SYNC,
3334
CONFIG_TRANSPARENT,
@@ -324,7 +325,9 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
324325
// Held by a blocked lane: the frame joins nothing — a lane never holds
325326
// a sync write (#3460). The pass is the lane's if it reads the lane's
326327
// world (`read`), and has left it otherwise.
327-
if (tx._lane) {
328+
// An answered deferred node's re-pass is a lagging question (D-40):
329+
// it joins nothing, so the write that reached it commits mainline.
330+
if (tx._lane || el._config & CONFIG_LAGS) {
328331
} else if (isEffect !== EFFECT_RENDER && !(el._config & CONFIG_VERDICT))
329332
joinPassTx(tx, GlobalQueue._fresh?.(el, el._statusFlags & STATUS_UNINITIALIZED));
330333
else if (

‎packages/signals/src/deferred.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@
2828

2929
import {
3030
CONFIG_HELD,
31+
CONFIG_LAGS,
3132
CONFIG_REDERIVE,
33+
CONFIG_SLOT_NODE,
34+
NOT_PENDING,
3235
REACTIVE_DISPOSED,
3336
REACTIVE_REASK,
3437
REACTIVE_RECOMPUTING_DEPS,
3538
STATUS_UNINITIALIZED
3639
} from "./core/constants.js";
37-
import { isThenable } from "./core/async.js";
40+
import { handleAsync, isThenable } from "./core/async.js";
3841
import { cleanup, computed, getOwner, NotReadyError } from "./core/index.js";
3942
import { GlobalQueue, holdNode, schedule, txOf } from "./core/scheduler.js";
4043
import type { Computed } from "./core/types.js";
@@ -50,6 +53,7 @@ function lagOf(c: Computed<any>, seen: Set<Computed<any>>): Computed<any> | unde
5053
if (lagging.has(c)) return c;
5154
if (seen.has(c)) return;
5255
seen.add(c);
56+
if (c._config & CONFIG_SLOT_NODE) return lagOf((c as any)._host?.fam?.node ?? c, seen);
5357
const tail = c._flags & REACTIVE_RECOMPUTING_DEPS ? c._depsTail : undefined;
5458
if (tail !== null)
5559
for (let d = c._deps ?? null; d !== null; d = d._nextDep) {
@@ -123,6 +127,16 @@ export function createDeferred<T>(
123127
repoll(node);
124128
}
125129
};
130+
/** Registers the flight and serves the pass's value: the committed one,
131+
* or over a held landing the landing itself, which a lagging question
132+
* does not replace (D-40) — restaged as the committed value, the hold's
133+
* readers would re-derive against the old input. */
134+
const serve = (r: PromiseLike<T> | AsyncIterable<T>): T => {
135+
const v = handleAsync(node, r);
136+
return node._config & CONFIG_HELD && node._pendingValue !== NOT_PENDING
137+
? (node._pendingValue as T)
138+
: v;
139+
};
126140
const end = () => node._flags & REACTIVE_DISPOSED && land(flight);
127141
return accessor<T>(
128142
computed<T>(prev => {
@@ -133,7 +147,7 @@ export function createDeferred<T>(
133147
// not a `loadingValue` window still open (both are loading, F-9).
134148
// Sticky, and read before this wrapper ever opens the window itself.
135149
const open = (answered ||= !(node._statusFlags & STATUS_UNINITIALIZED) && !node._loading);
136-
if (open) node._loading = true;
150+
if (open) ((node._loading = true), (node._config |= CONFIG_LAGS));
137151
let r: T | PromiseLike<T> | AsyncIterable<T>;
138152
try {
139153
r = fn(prev);
@@ -148,7 +162,7 @@ export function createDeferred<T>(
148162
ask(r);
149163
const done = () => land(r, true);
150164
(r as PromiseLike<T>).then(done, done);
151-
return r;
165+
return serve(r as PromiseLike<T>);
152166
}
153167
const iterate = (r as AsyncIterable<T>)[Symbol.asyncIterator];
154168
if (iterate) {
@@ -167,7 +181,7 @@ export function createDeferred<T>(
167181
return: v => it.return?.(v) ?? Promise.resolve({ done: true, value: v })
168182
};
169183
ask(s);
170-
return { [Symbol.asyncIterator]: () => s };
184+
return serve({ [Symbol.asyncIterator]: () => s });
171185
}
172186
}
173187
land(flight);

0 commit comments

Comments
 (0)