|
1 | | -import { $REFRESH, type Computed, type Signal } from "./core/index.js"; |
| 1 | +import { |
| 2 | + addPendingSource, |
| 3 | + forEachDependent, |
| 4 | + notifyStatus, |
| 5 | + setPendingError, |
| 6 | + settlePendingSource |
| 7 | +} from "./core/async.js"; |
| 8 | +import { STATUS_PENDING } from "./core/constants.js"; |
2 | 9 | import { emitDiagnostic } from "./core/dev.js"; |
3 | | -import { registerAffectsMark } from "./core/scheduler.js"; |
4 | | -import { $TARGET, getStoreAffectsNodes, type Store, type StoreNode } from "./store/store.js"; |
| 10 | +import { NotReadyError } from "./core/error.js"; |
| 11 | +import { $REFRESH, type Computed, type Signal } from "./core/index.js"; |
| 12 | +import { devTrackAffects } from "./core/invariants.js"; |
| 13 | +import { |
| 14 | + GlobalQueue, |
| 15 | + globalQueue, |
| 16 | + queuePendingNode, |
| 17 | + schedule, |
| 18 | + shiftAffectsMarks |
| 19 | +} from "./core/scheduler.js"; |
5 | 20 | import type { Accessor } from "./signals.js"; |
| 21 | +import { $TARGET, getStoreAffectsNodes, type Store, type StoreNode } from "./store/store.js"; |
| 22 | + |
| 23 | +type MarkedNode = Signal<any> | Computed<any>; |
| 24 | + |
| 25 | +/** |
| 26 | + * The pending-source identity of a live `affects()` mark on `node` (lazy, |
| 27 | + * one per node, shared by overlapping registrations via the refcount). |
| 28 | + * |
| 29 | + * A mark rides the SAME status rails as real in-flight async — downstream |
| 30 | + * subscribers hold the sentinel in `_pendingSources` — but under its own |
| 31 | + * identity so the two channels can't clear each other: |
| 32 | + * - `_reask` is permanently `false`: a mark is by definition a declared |
| 33 | + * value change, so `quietPending` never silences a window it participates |
| 34 | + * in — even when the mark rides over an otherwise-quiet `refresh()` |
| 35 | + * re-ask of the same node (the whole point of declaring one). |
| 36 | + * - A landing on the marked node settles only the node's OWN source entry; |
| 37 | + * the sentinel entry survives until the mark's transaction releases it. |
| 38 | + * - The sentinel itself never carries `STATUS_PENDING`, so |
| 39 | + * `transitionComplete` never counts a mark as a blocker of its own |
| 40 | + * transaction (release happens AT settle — self-blocking would deadlock), |
| 41 | + * and reads of the marked node never throw (marks are value-transparent |
| 42 | + * at the source; pendingness is what propagates). |
| 43 | + */ |
| 44 | +function getAffectsSentinel(node: MarkedNode): Computed<any> { |
| 45 | + return (node._affectsSentinel ??= { |
| 46 | + _name: __DEV__ ? "affects-sentinel" : undefined, |
| 47 | + // Brand + backref: lets the scheduler's settlement checks recognize |
| 48 | + // mark-sourced pending (which must never block its own transaction — |
| 49 | + // release happens AT settle). |
| 50 | + _affectsFor: node, |
| 51 | + _flags: 0, |
| 52 | + _statusFlags: 0, |
| 53 | + _reask: false, |
| 54 | + _error: undefined, |
| 55 | + _subs: null, |
| 56 | + _deps: null |
| 57 | + } as unknown as Computed<any>); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Push a live mark's pendingness downstream from the marked node through the |
| 62 | + * normal status rails. Runs on every registration (dedup in `notifyStatus` |
| 63 | + * stops re-descent at already-covered subscribers). Subscribers that |
| 64 | + * recompute mid-window shed this via `clearStatus` and re-acquire it through |
| 65 | + * the read path (`applyAffectsReads`) — the same shape as real async, where |
| 66 | + * the re-throw on read re-establishes the source. |
| 67 | + */ |
| 68 | +function propagateAffectsMark(node: MarkedNode): void { |
| 69 | + if (!node._subs && !(node as Computed<any>)._child) return; |
| 70 | + const sentinel = getAffectsSentinel(node); |
| 71 | + const error = new NotReadyError(sentinel); |
| 72 | + forEachDependent(node as Computed<any>, sub => { |
| 73 | + if (sub._pendingSource !== sentinel && !sub._pendingSources?.has(sentinel)) { |
| 74 | + if (!sub._transition) queuePendingNode(sub); |
| 75 | + notifyStatus(sub, STATUS_PENDING, error); |
| 76 | + } |
| 77 | + }); |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * Re-establish mark pendingness on a computed that read marked sources |
| 82 | + * during its recompute (`clearStatus` at the top of the commit path wiped |
| 83 | + * any sentinel entries it held). Called by `recompute` after the commit — |
| 84 | + * not before, because setting `_error` earlier would make the commit path |
| 85 | + * treat the node as errored and skip the value write. |
| 86 | + */ |
| 87 | +function applyAffectsReads(el: Computed<any>, sources: MarkedNode[]): void { |
| 88 | + let applied = false; |
| 89 | + for (let i = 0; i < sources.length; i++) { |
| 90 | + const src = sources[i]; |
| 91 | + if (!src._affectsCount) continue; |
| 92 | + const sentinel = getAffectsSentinel(src); |
| 93 | + if (addPendingSource(el, sentinel)) { |
| 94 | + el._statusFlags |= STATUS_PENDING; |
| 95 | + setPendingError(el, sentinel); |
| 96 | + applied = true; |
| 97 | + } |
| 98 | + } |
| 99 | + if (applied && GlobalQueue._updatePendingSignal !== null) GlobalQueue._updatePendingSignal(el); |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * The counting half of a mark, shared by direct registration and store-scope |
| 104 | + * inheritance (a node created inside a live keyless mark's identity scope): |
| 105 | + * bumps the refcount and pokes the node's verdict companions so an |
| 106 | + * already-materialized `false` flips reactively. |
| 107 | + */ |
| 108 | +function markAffects(node: MarkedNode): void { |
| 109 | + node._affectsCount = (node._affectsCount || 0) + 1; |
| 110 | + shiftAffectsMarks(1); |
| 111 | + if (__DEV__) devTrackAffects(node); |
| 112 | + // Companions only exist once the verdict layer (isPending/latest) loaded; |
| 113 | + // without them there is no materialized verdict to flip. |
| 114 | + if (node._affectsCount === 1 && GlobalQueue._updatePendingSignal !== null) |
| 115 | + GlobalQueue._updatePendingSignal(node); |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * Registers one `affects()` mark on a node: counts it, records the |
| 120 | + * registration with the current transaction (after initTransition the queue's |
| 121 | + * array aliases the active transition's, mirroring `_optimisticNodes`), and |
| 122 | + * propagates STATUS_PENDING downstream on the status rails so everything |
| 123 | + * DERIVED from the marked data reads pending too. Propagation runs on every |
| 124 | + * registration (not just the first): subscribers gained since an earlier |
| 125 | + * overlapping registration get covered, and dedup stops re-descent early. |
| 126 | + */ |
| 127 | +function registerAffectsMark(node: MarkedNode): void { |
| 128 | + markAffects(node); |
| 129 | + globalQueue._affectsNodes.push(node); |
| 130 | + propagateAffectsMark(node); |
| 131 | + schedule(); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Releases one registration. When the node's last mark drops, settles the |
| 136 | + * mark's sentinel out of every downstream `_pendingSources` (waking blocked |
| 137 | + * nodes and re-deriving verdicts along the walk). Companion writes go through |
| 138 | + * the settlement snap (committed, not transition-scoped) so releasing a mark |
| 139 | + * can't open a fresh override window that would itself need settlement. |
| 140 | + */ |
| 141 | +function releaseAffectsMark(node: MarkedNode): void { |
| 142 | + shiftAffectsMarks(-1); |
| 143 | + node._affectsCount!--; |
| 144 | + if (!node._affectsCount) { |
| 145 | + const sentinel = node._affectsSentinel; |
| 146 | + if (sentinel) settlePendingSource(node as Computed<any>, sentinel, true); |
| 147 | + GlobalQueue._snapCompanions !== null && GlobalQueue._snapCompanions(node); |
| 148 | + GlobalQueue._releaseAffectsScope?.(node); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Releases one batch of affects marks (a settling transaction's, or the |
| 154 | + * ambient batch at a plain flush end). |
| 155 | + */ |
| 156 | +function releaseAffectsMarks(nodes: MarkedNode[]): void { |
| 157 | + for (let i = 0; i < nodes.length; i++) releaseAffectsMark(nodes[i]); |
| 158 | + nodes.length = 0; |
| 159 | +} |
| 160 | + |
| 161 | +// Late installation (same pattern as `GlobalQueue._update`): the mark engine |
| 162 | +// lives with the feature so graphs that never declare a mark never ship it. |
| 163 | +// Each call site is gated by state only this module creates (`markedReads` |
| 164 | +// collection under `activeAffectsMarks`, a non-empty `_affectsNodes` batch, |
| 165 | +// a live scope in the store's `affectsScopes`), so the hooks are installed |
| 166 | +// before the first time any of them can fire. |
| 167 | +GlobalQueue._applyAffectsReads = applyAffectsReads; |
| 168 | +GlobalQueue._releaseAffectsMarks = releaseAffectsMarks; |
| 169 | +GlobalQueue._markAffects = markAffects; |
| 170 | +GlobalQueue._releaseAffectsMark = releaseAffectsMark; |
6 | 171 |
|
7 | 172 | /** |
8 | 173 | * Declares that in-flight work will change the targeted data: the named |
|
0 commit comments