@isograph/pico 0.0.0-main-5f205c00
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE-MIT +7 -0
- package/README.md +152 -0
- package/dist/index.cjs +1154 -0
- package/dist/index.d.cts +176 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +176 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1139 -0
- package/dist/index.mjs.map +1 -0
- package/docs/contracts.md +45 -0
- package/docs/correctness.md +233 -0
- package/docs/design.md +97 -0
- package/docs/disposing-nodes.md +171 -0
- package/docs/lifetimes.md +25 -0
- package/docs/performance.md +85 -0
- package/package.json +49 -0
- package/src/batch/batch.ts +133 -0
- package/src/batch/flush.ts +30 -0
- package/src/batch/notifications.ts +39 -0
- package/src/disposal/age-drain.ts +124 -0
- package/src/disposal/cleanup.ts +171 -0
- package/src/disposal/older-than-updates.ts +132 -0
- package/src/disposal/schedule.ts +78 -0
- package/src/disposal/take.ts +52 -0
- package/src/disposal/update-counter.ts +9 -0
- package/src/graph/edges.ts +167 -0
- package/src/graph/marking.ts +310 -0
- package/src/graph/nodes.ts +196 -0
- package/src/graph/read.ts +150 -0
- package/src/graph/recompute.ts +127 -0
- package/src/graph/refresh.ts +232 -0
- package/src/index.ts +18 -0
- package/src/retention/retain.ts +133 -0
- package/src/state.ts +39 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { MutableDerivedNode, MutableSourceNode } from './nodes';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* How dependency edges work
|
|
5
|
+
*
|
|
6
|
+
* The derived node `total` reads `count` and `pricePerItem` to compute their
|
|
7
|
+
* product. Either input can be a source node or a derived node.
|
|
8
|
+
*
|
|
9
|
+
* The edge `total → count` has `edge.dependent === total` and
|
|
10
|
+
* `edge.dependency === count`. Both nodes link to this same edge:
|
|
11
|
+
*
|
|
12
|
+
* - From `total`, start at `total.firstSourceDependency` if `count` is a
|
|
13
|
+
* source, or `total.firstDerivedDependency` if it is derived. Follow
|
|
14
|
+
* `nextDependencyEdge` to visit each input of that kind.
|
|
15
|
+
* - From `count`, start at `count.firstDependent` and follow `nextDependentEdge`
|
|
16
|
+
* to visit each computation that reads `count`.
|
|
17
|
+
*
|
|
18
|
+
* Within each dependency list, Pico reuses edges in read order. Suppose both inputs
|
|
19
|
+
* are sources and `total` reads `count` first. On its next run:
|
|
20
|
+
*
|
|
21
|
+
* - If it reads `count` and `pricePerItem` again, it reuses both edges.
|
|
22
|
+
* - If it reads `count` and a different source, `salePrice`, the second edge
|
|
23
|
+
* moves from the dependents of `pricePerItem` to the dependents of `salePrice`.
|
|
24
|
+
* - If `count` is zero and `total` returns early, Pico removes the unread
|
|
25
|
+
* price edge from both lists.
|
|
26
|
+
*
|
|
27
|
+
* Dependency lists are singly linked: recomputation walks forward and trims
|
|
28
|
+
* unused edges from the end. Dependent lists are doubly linked: a new edge
|
|
29
|
+
* goes at the head, and any edge can be removed in constant time.
|
|
30
|
+
*
|
|
31
|
+
* Marking a node `DIRTY` or `MAYBE_DIRTY` preserves its dependency edges.
|
|
32
|
+
* If verification finds no changed input, a `MAYBE_DIRTY` node becomes
|
|
33
|
+
* `CLEAN` with the same edges. Disposal unlinks all its dependency edges.
|
|
34
|
+
*/
|
|
35
|
+
export interface Edge<TDependency> {
|
|
36
|
+
/** For `total → pricePerItem`, the input being read: `pricePerItem`. */
|
|
37
|
+
dependency: TDependency;
|
|
38
|
+
/** The derived node doing the reading: `total`. */
|
|
39
|
+
readonly dependent: MutableDerivedNode<unknown>;
|
|
40
|
+
/** The next edge from `total` to an input of the same kind. */
|
|
41
|
+
nextDependencyEdge: this | undefined;
|
|
42
|
+
/** The preceding edge in the list of computations that read `pricePerItem`. */
|
|
43
|
+
previousDependentEdge: this | undefined;
|
|
44
|
+
/** The following edge in that same list. */
|
|
45
|
+
nextDependentEdge: this | undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** An edge from a derived node to a source. Its list links also point to source edges. */
|
|
49
|
+
export interface SourceEdge extends Edge<MutableSourceNode<unknown>> {
|
|
50
|
+
readonly [sourceEdgeBrand]: true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare const sourceEdgeBrand: unique symbol;
|
|
54
|
+
|
|
55
|
+
export function createSourceEdge(
|
|
56
|
+
dependency: MutableSourceNode<unknown>,
|
|
57
|
+
dependent: MutableDerivedNode<unknown>,
|
|
58
|
+
): SourceEdge {
|
|
59
|
+
const fields: Omit<SourceEdge, typeof sourceEdgeBrand> = {
|
|
60
|
+
dependency,
|
|
61
|
+
dependent,
|
|
62
|
+
nextDependencyEdge: undefined,
|
|
63
|
+
previousDependentEdge: undefined,
|
|
64
|
+
nextDependentEdge: undefined,
|
|
65
|
+
};
|
|
66
|
+
return fields as SourceEdge;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Linking and unlinking are intentionally monomorphized for performance.
|
|
70
|
+
// Each firstDependent access sees only sources or only derived nodes.
|
|
71
|
+
export function addEdgeToSourceDependents(edge: SourceEdge): void {
|
|
72
|
+
const dependency = edge.dependency;
|
|
73
|
+
const head = dependency.firstDependent;
|
|
74
|
+
edge.previousDependentEdge = undefined;
|
|
75
|
+
edge.nextDependentEdge = head;
|
|
76
|
+
if (head !== undefined) {
|
|
77
|
+
head.previousDependentEdge = edge;
|
|
78
|
+
}
|
|
79
|
+
dependency.firstDependent = edge;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function addEdgeToDerivedDependents(
|
|
83
|
+
edge: Edge<MutableDerivedNode<unknown>>,
|
|
84
|
+
): void {
|
|
85
|
+
const dependency = edge.dependency;
|
|
86
|
+
const head = dependency.firstDependent;
|
|
87
|
+
edge.previousDependentEdge = undefined;
|
|
88
|
+
edge.nextDependentEdge = head;
|
|
89
|
+
if (head !== undefined) {
|
|
90
|
+
head.previousDependentEdge = edge;
|
|
91
|
+
}
|
|
92
|
+
dependency.firstDependent = edge;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function unlinkRemainingSourceDependencies(first: SourceEdge): void {
|
|
96
|
+
let edge: SourceEdge | undefined = first;
|
|
97
|
+
do {
|
|
98
|
+
removeEdgeFromSourceDependents(edge);
|
|
99
|
+
edge = edge.nextDependencyEdge;
|
|
100
|
+
} while (edge !== undefined);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Unlinking preserves `nextDependencyEdge` so traversal can continue through the removed edge.
|
|
105
|
+
*/
|
|
106
|
+
export function removeEdgeFromSourceDependents(edge: SourceEdge): void {
|
|
107
|
+
const previous = edge.previousDependentEdge;
|
|
108
|
+
const next = edge.nextDependentEdge;
|
|
109
|
+
if (previous === undefined) {
|
|
110
|
+
edge.dependency.firstDependent = next;
|
|
111
|
+
} else {
|
|
112
|
+
previous.nextDependentEdge = next;
|
|
113
|
+
}
|
|
114
|
+
if (next !== undefined) {
|
|
115
|
+
next.previousDependentEdge = previous;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Unlinking preserves `nextDependencyEdge` so traversal can continue through the removed edge.
|
|
121
|
+
*/
|
|
122
|
+
export function removeEdgeFromDerivedDependents(
|
|
123
|
+
edge: Edge<MutableDerivedNode<unknown>>,
|
|
124
|
+
): void {
|
|
125
|
+
const dependency = edge.dependency;
|
|
126
|
+
const next = edge.nextDependentEdge;
|
|
127
|
+
if (dependency.firstKnownMarkedDependentEdge === edge) {
|
|
128
|
+
dependency.firstKnownMarkedDependentEdge = next;
|
|
129
|
+
}
|
|
130
|
+
const previous = edge.previousDependentEdge;
|
|
131
|
+
if (previous === undefined) {
|
|
132
|
+
dependency.firstDependent = next;
|
|
133
|
+
} else {
|
|
134
|
+
previous.nextDependentEdge = next;
|
|
135
|
+
}
|
|
136
|
+
if (next !== undefined) {
|
|
137
|
+
next.previousDependentEdge = previous;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Moves `edge` strictly before `edge.dependency.firstKnownMarkedDependentEdge` so `edge`'s
|
|
143
|
+
* dependent can become `CLEAN` while preserving the `firstKnownMarkedDependentEdge` invariant.
|
|
144
|
+
*
|
|
145
|
+
* Contract: `edge.dependency.firstKnownMarkedDependentEdge !== undefined`.
|
|
146
|
+
*/
|
|
147
|
+
export function moveEdgeBeforeKnownMarkedDependents(
|
|
148
|
+
edge: Edge<MutableDerivedNode<unknown>>,
|
|
149
|
+
): void {
|
|
150
|
+
const dependency = edge.dependency;
|
|
151
|
+
if (edge === dependency.firstKnownMarkedDependentEdge) {
|
|
152
|
+
dependency.firstKnownMarkedDependentEdge = edge.nextDependentEdge;
|
|
153
|
+
}
|
|
154
|
+
const previous = edge.previousDependentEdge;
|
|
155
|
+
if (previous !== undefined) {
|
|
156
|
+
const next = edge.nextDependentEdge;
|
|
157
|
+
previous.nextDependentEdge = next;
|
|
158
|
+
if (next !== undefined) {
|
|
159
|
+
next.previousDependentEdge = previous;
|
|
160
|
+
}
|
|
161
|
+
const head = dependency.firstDependent!;
|
|
162
|
+
edge.previousDependentEdge = undefined;
|
|
163
|
+
edge.nextDependentEdge = head;
|
|
164
|
+
head.previousDependentEdge = edge;
|
|
165
|
+
dependency.firstDependent = edge;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UNCOMPUTED,
|
|
3
|
+
DIRTY,
|
|
4
|
+
MAYBE_DIRTY,
|
|
5
|
+
CLEAN,
|
|
6
|
+
DEPENDENTS_KNOWN_TO_BE_ALL_DIRTY,
|
|
7
|
+
HAS_CALLBACK,
|
|
8
|
+
MARKED_BEYOND,
|
|
9
|
+
NO_FLAGS,
|
|
10
|
+
} from '../state';
|
|
11
|
+
import type { Edge, SourceEdge } from './edges';
|
|
12
|
+
import type { MutableDerivedNode, MutableSourceNode } from './nodes';
|
|
13
|
+
|
|
14
|
+
export function storeSourceValueAndFindDependentsToMark<TValue>(
|
|
15
|
+
source: MutableSourceNode<TValue>,
|
|
16
|
+
value: TValue,
|
|
17
|
+
): SourceEdge | undefined {
|
|
18
|
+
source.value = value;
|
|
19
|
+
if (source.dependentsStatus === DEPENDENTS_KNOWN_TO_BE_ALL_DIRTY) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
source.dependentsStatus = DEPENDENTS_KNOWN_TO_BE_ALL_DIRTY;
|
|
23
|
+
return source.firstDependent;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A source write makes its direct readers DIRTY. Readers farther up the
|
|
28
|
+
* graph become MAYBE_DIRTY. Queue the first node with a callback on each
|
|
29
|
+
* path. If its value changes, its refresh queues the next affected callbacks.
|
|
30
|
+
*/
|
|
31
|
+
export function markDependentsOfWrittenSource(
|
|
32
|
+
initialQueue: Array<MutableDerivedNode<unknown>> | undefined,
|
|
33
|
+
first: SourceEdge,
|
|
34
|
+
): Array<MutableDerivedNode<unknown>> | undefined {
|
|
35
|
+
let queue = initialQueue;
|
|
36
|
+
// Keep one lazy reader in a local. A single chain needs no stack.
|
|
37
|
+
// When several lazy readers have dependents, save the other branches.
|
|
38
|
+
let pendingLazyBranches: Array<MutableDerivedNode<unknown>> | undefined;
|
|
39
|
+
let firstLazyBranch: MutableDerivedNode<unknown> | undefined;
|
|
40
|
+
let beyondCallbackStack: Array<MutableDerivedNode<unknown>> | undefined;
|
|
41
|
+
let edge: SourceEdge | undefined = first;
|
|
42
|
+
while (edge !== undefined) {
|
|
43
|
+
const node = edge.dependent;
|
|
44
|
+
edge = edge.nextDependentEdge;
|
|
45
|
+
const status = node.status;
|
|
46
|
+
if (status === CLEAN) {
|
|
47
|
+
node.status = DIRTY;
|
|
48
|
+
if (node.callbackState === HAS_CALLBACK) {
|
|
49
|
+
queue = queueNodeWithCallback(queue, node);
|
|
50
|
+
const firstDependent = node.firstDependent;
|
|
51
|
+
if (
|
|
52
|
+
firstDependent !== undefined &&
|
|
53
|
+
firstDependent !== node.firstKnownMarkedDependentEdge
|
|
54
|
+
) {
|
|
55
|
+
(beyondCallbackStack ??= []).push(node);
|
|
56
|
+
}
|
|
57
|
+
} else if (node.firstDependent !== undefined) {
|
|
58
|
+
if (firstLazyBranch !== undefined) {
|
|
59
|
+
(pendingLazyBranches ??= []).push(firstLazyBranch);
|
|
60
|
+
}
|
|
61
|
+
firstLazyBranch = node;
|
|
62
|
+
} else if (node.firstRetain === undefined) {
|
|
63
|
+
recordMarkedUnneededNode(node);
|
|
64
|
+
}
|
|
65
|
+
} else if (status === MAYBE_DIRTY) {
|
|
66
|
+
// Internal invariant: a `MAYBE_DIRTY` node marked its dependents when it
|
|
67
|
+
// left `CLEAN`, or has a callback and went on the flush queue then. One with
|
|
68
|
+
// `MARKED_BEYOND` loses the bit and is marked again by the walk through
|
|
69
|
+
// lazy nodes. It goes on the flush queue if it has a callback, and the walk
|
|
70
|
+
// goes on through it if it is lazy.
|
|
71
|
+
node.status = DIRTY;
|
|
72
|
+
if ((node.traversalFlags & MARKED_BEYOND) !== NO_FLAGS) {
|
|
73
|
+
node.traversalFlags &= ~MARKED_BEYOND;
|
|
74
|
+
if (node.callbackState === HAS_CALLBACK) {
|
|
75
|
+
queue = queueNodeWithCallback(queue, node);
|
|
76
|
+
} else if (node.firstDependent !== undefined) {
|
|
77
|
+
if (firstLazyBranch !== undefined) {
|
|
78
|
+
(pendingLazyBranches ??= []).push(firstLazyBranch);
|
|
79
|
+
}
|
|
80
|
+
firstLazyBranch = node;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (firstLazyBranch !== undefined) {
|
|
86
|
+
return markTransitiveDependentsMaybeDirty(
|
|
87
|
+
queue,
|
|
88
|
+
firstLazyBranch,
|
|
89
|
+
pendingLazyBranches,
|
|
90
|
+
beyondCallbackStack,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (beyondCallbackStack !== undefined) {
|
|
94
|
+
markReadersBeyondQueuedCallbacks(beyondCallbackStack);
|
|
95
|
+
}
|
|
96
|
+
return queue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function queueNodeWithCallback(
|
|
100
|
+
queue: Array<MutableDerivedNode<unknown>> | undefined,
|
|
101
|
+
node: MutableDerivedNode<unknown>,
|
|
102
|
+
): Array<MutableDerivedNode<unknown>> {
|
|
103
|
+
if (queue === undefined) {
|
|
104
|
+
return [node];
|
|
105
|
+
}
|
|
106
|
+
queue.push(node);
|
|
107
|
+
return queue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function recordMarkedUnneededNode(node: MutableDerivedNode<unknown>): void {
|
|
111
|
+
markedUnneeded[markedUnneededCount] = node;
|
|
112
|
+
markedUnneededCount += 1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Internal invariant: the first `markedUnneededCount` entries of
|
|
117
|
+
* `markedUnneeded` are the nodes that marking took out of `CLEAN` while they
|
|
118
|
+
* were unneeded, and every later entry is `undefined`. `markedUnneededCount`
|
|
119
|
+
* is 0 when an outermost operation returns.
|
|
120
|
+
*/
|
|
121
|
+
export const markedUnneeded: Array<MutableDerivedNode<unknown> | undefined> =
|
|
122
|
+
[];
|
|
123
|
+
|
|
124
|
+
export let markedUnneededCount = 0;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Follow lazy readers until each path reaches a node with a callback.
|
|
128
|
+
* Queue that node for refresh. Collect its readers for marking beyond the
|
|
129
|
+
* callback after this walk finishes.
|
|
130
|
+
*/
|
|
131
|
+
export function markTransitiveDependentsMaybeDirty(
|
|
132
|
+
initialQueue: Array<MutableDerivedNode<unknown>> | undefined,
|
|
133
|
+
firstLazyBranch: MutableDerivedNode<unknown>,
|
|
134
|
+
initialLazyStack: Array<MutableDerivedNode<unknown>> | undefined,
|
|
135
|
+
initialBeyondCallbackStack: Array<MutableDerivedNode<unknown>> | undefined,
|
|
136
|
+
): Array<MutableDerivedNode<unknown>> | undefined {
|
|
137
|
+
let queue = initialQueue;
|
|
138
|
+
let pendingLazyBranches = initialLazyStack;
|
|
139
|
+
let beyondCallbackStack = initialBeyondCallbackStack;
|
|
140
|
+
let marked: MutableDerivedNode<unknown> | undefined = firstLazyBranch;
|
|
141
|
+
while (marked !== undefined) {
|
|
142
|
+
let dependentEdge = marked.firstDependent;
|
|
143
|
+
while (dependentEdge !== undefined) {
|
|
144
|
+
const dependent = dependentEdge.dependent;
|
|
145
|
+
dependentEdge = dependentEdge.nextDependentEdge;
|
|
146
|
+
switch (dependent.status) {
|
|
147
|
+
case CLEAN:
|
|
148
|
+
dependent.status = MAYBE_DIRTY;
|
|
149
|
+
dependent.traversalFlags &= ~MARKED_BEYOND;
|
|
150
|
+
if (dependent.callbackState === HAS_CALLBACK) {
|
|
151
|
+
queue = queueNodeWithCallback(queue, dependent);
|
|
152
|
+
const firstDependent = dependent.firstDependent;
|
|
153
|
+
if (
|
|
154
|
+
firstDependent !== undefined &&
|
|
155
|
+
firstDependent !== dependent.firstKnownMarkedDependentEdge
|
|
156
|
+
) {
|
|
157
|
+
(beyondCallbackStack ??= []).push(dependent);
|
|
158
|
+
}
|
|
159
|
+
} else if (dependent.firstDependent !== undefined) {
|
|
160
|
+
if (dependentEdge === undefined) {
|
|
161
|
+
// Last sibling: follow its readers directly.
|
|
162
|
+
dependentEdge = dependent.firstDependent;
|
|
163
|
+
} else {
|
|
164
|
+
// Finish the siblings first, then visit this reader's branch.
|
|
165
|
+
(pendingLazyBranches ??= []).push(dependent);
|
|
166
|
+
}
|
|
167
|
+
} else if (dependent.firstRetain === undefined) {
|
|
168
|
+
recordMarkedUnneededNode(dependent);
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
case MAYBE_DIRTY:
|
|
172
|
+
if ((dependent.traversalFlags & MARKED_BEYOND) !== NO_FLAGS) {
|
|
173
|
+
dependent.traversalFlags &= ~MARKED_BEYOND;
|
|
174
|
+
if (dependent.callbackState === HAS_CALLBACK) {
|
|
175
|
+
queue = queueNodeWithCallback(queue, dependent);
|
|
176
|
+
} else if (dependent.firstDependent !== undefined) {
|
|
177
|
+
if (dependentEdge === undefined) {
|
|
178
|
+
dependentEdge = dependent.firstDependent;
|
|
179
|
+
} else {
|
|
180
|
+
(pendingLazyBranches ??= []).push(dependent);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
// Internal invariant: a lazy node that is not `CLEAN` has no `CLEAN`
|
|
186
|
+
// dependents.
|
|
187
|
+
case DIRTY:
|
|
188
|
+
case UNCOMPUTED:
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
marked =
|
|
193
|
+
pendingLazyBranches === undefined ? undefined : pendingLazyBranches.pop();
|
|
194
|
+
}
|
|
195
|
+
if (beyondCallbackStack !== undefined) {
|
|
196
|
+
markReadersBeyondQueuedCallbacks(beyondCallbackStack);
|
|
197
|
+
}
|
|
198
|
+
return queue;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* A queued callback node may keep its value. Mark its readers MAYBE_DIRTY so
|
|
203
|
+
* a nested read can verify them. If the callback node changes, its refresh
|
|
204
|
+
* queues the readers that need to run.
|
|
205
|
+
*/
|
|
206
|
+
function markReadersBeyondQueuedCallbacks(
|
|
207
|
+
beyondCallbackStack: Array<MutableDerivedNode<unknown>>,
|
|
208
|
+
): void {
|
|
209
|
+
let marked = beyondCallbackStack.pop();
|
|
210
|
+
while (marked !== undefined) {
|
|
211
|
+
let dependentEdge = marked.firstDependent;
|
|
212
|
+
const stop = marked.firstKnownMarkedDependentEdge;
|
|
213
|
+
if (dependentEdge !== stop) {
|
|
214
|
+
marked.firstKnownMarkedDependentEdge = dependentEdge;
|
|
215
|
+
while (dependentEdge !== stop) {
|
|
216
|
+
const dependent = dependentEdge!.dependent;
|
|
217
|
+
dependentEdge = dependentEdge!.nextDependentEdge;
|
|
218
|
+
if (dependent.status === CLEAN) {
|
|
219
|
+
dependent.status = MAYBE_DIRTY;
|
|
220
|
+
dependent.traversalFlags |= MARKED_BEYOND;
|
|
221
|
+
const firstDependent = dependent.firstDependent;
|
|
222
|
+
if (firstDependent !== undefined) {
|
|
223
|
+
if (firstDependent !== dependent.firstKnownMarkedDependentEdge) {
|
|
224
|
+
beyondCallbackStack.push(dependent);
|
|
225
|
+
}
|
|
226
|
+
} else if (dependent.firstRetain === undefined) {
|
|
227
|
+
recordMarkedUnneededNode(dependent);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
marked = beyondCallbackStack.pop();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* A callback node changed during the flush. Its direct readers now need
|
|
238
|
+
* recomputing. A reader marked only through this callback (MARKED_BEYOND)
|
|
239
|
+
* still needs its callbacks queued. Follow its lazy readers until the
|
|
240
|
+
* next callback on each path, which will propagate its own change in turn.
|
|
241
|
+
*
|
|
242
|
+
* Every direct reader was already marked by the write. A reader reached
|
|
243
|
+
* earlier without crossing a callback already has its callbacks queued.
|
|
244
|
+
*/
|
|
245
|
+
export function markDependentsOfChangedDerivedNode(
|
|
246
|
+
flushQueue: Array<MutableDerivedNode<unknown>>,
|
|
247
|
+
node: MutableDerivedNode<unknown>,
|
|
248
|
+
): void {
|
|
249
|
+
node.firstKnownMarkedDependentEdge = undefined;
|
|
250
|
+
let pendingLazyBranches: Array<MutableDerivedNode<unknown>> | undefined;
|
|
251
|
+
let firstLazyBranch: MutableDerivedNode<unknown> | undefined;
|
|
252
|
+
let edge = node.firstDependent;
|
|
253
|
+
while (edge !== undefined) {
|
|
254
|
+
const dependent = edge.dependent;
|
|
255
|
+
edge = edge.nextDependentEdge;
|
|
256
|
+
switch (dependent.status) {
|
|
257
|
+
case MAYBE_DIRTY:
|
|
258
|
+
dependent.status = DIRTY;
|
|
259
|
+
if ((dependent.traversalFlags & MARKED_BEYOND) !== NO_FLAGS) {
|
|
260
|
+
dependent.traversalFlags &= ~MARKED_BEYOND;
|
|
261
|
+
if (dependent.callbackState === HAS_CALLBACK) {
|
|
262
|
+
flushQueue.push(dependent);
|
|
263
|
+
} else if (dependent.firstDependent !== undefined) {
|
|
264
|
+
if (firstLazyBranch !== undefined) {
|
|
265
|
+
(pendingLazyBranches ??= []).push(firstLazyBranch);
|
|
266
|
+
}
|
|
267
|
+
firstLazyBranch = dependent;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
break;
|
|
271
|
+
case CLEAN:
|
|
272
|
+
case DIRTY:
|
|
273
|
+
case UNCOMPUTED:
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (firstLazyBranch !== undefined) {
|
|
278
|
+
// The walk appends to flushQueue in place. Its return value is the
|
|
279
|
+
// same queue whenever the caller supplies one.
|
|
280
|
+
markTransitiveDependentsMaybeDirty(
|
|
281
|
+
flushQueue,
|
|
282
|
+
firstLazyBranch,
|
|
283
|
+
pendingLazyBranches,
|
|
284
|
+
undefined,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
export function markMaybeDirtyDependentsDirty(
|
|
290
|
+
first: Edge<MutableDerivedNode<unknown>> | undefined,
|
|
291
|
+
): void {
|
|
292
|
+
let edge = first;
|
|
293
|
+
while (edge !== undefined) {
|
|
294
|
+
const node = edge.dependent;
|
|
295
|
+
edge = edge.nextDependentEdge;
|
|
296
|
+
if (node.status === MAYBE_DIRTY) {
|
|
297
|
+
node.status = DIRTY;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Keep the allocated array for the next write without retaining the nodes.
|
|
304
|
+
*/
|
|
305
|
+
export function clearMarkedUnneededNodes(): void {
|
|
306
|
+
for (let index = 0; index < markedUnneededCount; index += 1) {
|
|
307
|
+
markedUnneeded[index] = undefined;
|
|
308
|
+
}
|
|
309
|
+
markedUnneededCount = 0;
|
|
310
|
+
}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UNCOMPUTED,
|
|
3
|
+
type DIRTY,
|
|
4
|
+
type MAYBE_DIRTY,
|
|
5
|
+
type CLEAN,
|
|
6
|
+
type DEPENDENTS_NOT_NECESSARILY_ALL_DIRTY,
|
|
7
|
+
DEPENDENTS_KNOWN_TO_BE_ALL_DIRTY,
|
|
8
|
+
NO_CALLBACK,
|
|
9
|
+
type HAS_CALLBACK,
|
|
10
|
+
type MARKED_BEYOND,
|
|
11
|
+
type SCAN_SEEN,
|
|
12
|
+
NO_FLAGS,
|
|
13
|
+
type ALL_TRAVERSAL_FLAGS,
|
|
14
|
+
} from '../state';
|
|
15
|
+
import type { Edge, SourceEdge } from './edges';
|
|
16
|
+
|
|
17
|
+
export function createSourceNode<TValue>(value: TValue): SourceNode<TValue> {
|
|
18
|
+
const source: Omit<MutableSourceNode<TValue>, typeof sourceBrand> = {
|
|
19
|
+
value,
|
|
20
|
+
firstDependent: undefined,
|
|
21
|
+
// A new source has no readers to invalidate.
|
|
22
|
+
dependentsStatus: DEPENDENTS_KNOWN_TO_BE_ALL_DIRTY,
|
|
23
|
+
};
|
|
24
|
+
return source as MutableSourceNode<TValue>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
* Public and internal types
|
|
29
|
+
*
|
|
30
|
+
* The public API exposes opaque handles so callers cannot edit the graph
|
|
31
|
+
* directly. Sources also expose a readonly value: writes go through
|
|
32
|
+
* `writeSource` so Pico can invalidate dependents. Internally, Pico casts
|
|
33
|
+
* the same handles to mutable types to access their fields without
|
|
34
|
+
* allocating wrapper objects.
|
|
35
|
+
*/
|
|
36
|
+
export type SourceNode<in out TValue> = {
|
|
37
|
+
/** The committed source value. Reading this field records no dependency. */
|
|
38
|
+
readonly value: TValue;
|
|
39
|
+
readonly [sourceBrand]: (value: TValue) => TValue;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
declare const sourceBrand: unique symbol;
|
|
43
|
+
|
|
44
|
+
export type MutableSourceNode<TValue> = SourceNode<TValue> & {
|
|
45
|
+
value: TValue;
|
|
46
|
+
firstDependent: SourceEdge | undefined;
|
|
47
|
+
// A write can skip marking until a read puts a clean reader back on the list.
|
|
48
|
+
dependentsStatus: DependentsStatus;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
type DependentsStatus =
|
|
52
|
+
| typeof DEPENDENTS_NOT_NECESSARILY_ALL_DIRTY
|
|
53
|
+
| typeof DEPENDENTS_KNOWN_TO_BE_ALL_DIRTY;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns an `UNCOMPUTED` derived node.
|
|
57
|
+
*
|
|
58
|
+
* Contract: `computation` is synchronous. `computation` reads other nodes
|
|
59
|
+
* only through `readSource` and `readDerived`, called with the `callingNode`
|
|
60
|
+
* that `computation` receives, and does not keep that `callingNode` for use
|
|
61
|
+
* after it returns. `computation` does not write, start or end a batch, retain,
|
|
62
|
+
* release, dispose, or call `readDerivedOutsideComputation`.
|
|
63
|
+
*/
|
|
64
|
+
export function createDerivedNode<TValue>(
|
|
65
|
+
computation: (
|
|
66
|
+
callingNode: CallingNode,
|
|
67
|
+
previous: TValue | undefined,
|
|
68
|
+
) => TValue,
|
|
69
|
+
): DerivedNode<TValue> {
|
|
70
|
+
const node: Omit<
|
|
71
|
+
MutableDerivedNode<TValue>,
|
|
72
|
+
keyof DerivedNode<TValue> | keyof CallingNode
|
|
73
|
+
> = {
|
|
74
|
+
// An `UNCOMPUTED` node has no value. Every read of `value` follows a
|
|
75
|
+
// status check that excludes `UNCOMPUTED`, so `value` is read only after a
|
|
76
|
+
// computation stored a `TValue` in it. The type cannot make `value` depend
|
|
77
|
+
// on `status`, and typing it `TValue | undefined` would put a cast at
|
|
78
|
+
// every read.
|
|
79
|
+
value: undefined as TValue,
|
|
80
|
+
firstRetain: undefined,
|
|
81
|
+
traversalFlags: NO_FLAGS,
|
|
82
|
+
status: UNCOMPUTED,
|
|
83
|
+
callbackState: NO_CALLBACK,
|
|
84
|
+
firstSourceDependency: undefined,
|
|
85
|
+
lastSourceDependency: undefined,
|
|
86
|
+
firstDerivedDependency: undefined,
|
|
87
|
+
lastDerivedDependency: undefined,
|
|
88
|
+
firstDependent: undefined,
|
|
89
|
+
firstKnownMarkedDependentEdge: undefined,
|
|
90
|
+
computation,
|
|
91
|
+
};
|
|
92
|
+
return node as MutableDerivedNode<TValue>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type CallingNode = { readonly [callingNodeBrand]: true };
|
|
96
|
+
|
|
97
|
+
declare const callingNodeBrand: unique symbol;
|
|
98
|
+
|
|
99
|
+
export type DerivedNode<TValue> = { readonly [derivedBrand]: TValue };
|
|
100
|
+
|
|
101
|
+
declare const derivedBrand: unique symbol;
|
|
102
|
+
|
|
103
|
+
export type MutableDerivedNode<TValue> = DerivedNode<TValue> &
|
|
104
|
+
CallingNode & {
|
|
105
|
+
value: TValue;
|
|
106
|
+
firstRetain: MutableRetain<TValue> | undefined;
|
|
107
|
+
// If (traversalFlags & SCAN_SEEN) !== NO_FLAGS, the cleanup scan has already
|
|
108
|
+
// kept this node's newest queue entry. It skips older entries for this node.
|
|
109
|
+
// If status is MAYBE_DIRTY and (traversalFlags & MARKED_BEYOND) !== NO_FLAGS,
|
|
110
|
+
// marking reached this node only through a dependency with a callback.
|
|
111
|
+
traversalFlags: TraversalFlags;
|
|
112
|
+
// On read, CLEAN reuses the cached value, MAYBE_DIRTY checks its inputs,
|
|
113
|
+
// DIRTY reruns the computation, and UNCOMPUTED computes from scratch.
|
|
114
|
+
status: Status;
|
|
115
|
+
// HAS_CALLBACK while any retain has an onChange callback. A flush
|
|
116
|
+
// refreshes the node before notifying those callbacks of a changed value.
|
|
117
|
+
callbackState: CallbackState;
|
|
118
|
+
firstSourceDependency: SourceEdge | undefined;
|
|
119
|
+
// The most recent source edge used so far in this computation.
|
|
120
|
+
lastSourceDependency: SourceEdge | undefined;
|
|
121
|
+
firstDerivedDependency: Edge<MutableDerivedNode<unknown>> | undefined;
|
|
122
|
+
// The corresponding cursor for reads of derived nodes.
|
|
123
|
+
lastDerivedDependency: Edge<MutableDerivedNode<unknown>> | undefined;
|
|
124
|
+
firstDependent: Edge<MutableDerivedNode<unknown>> | undefined;
|
|
125
|
+
/**
|
|
126
|
+
* Where marking can stop in this node's list of dependents. The list
|
|
127
|
+
* starts at `firstDependent` and follows `nextDependentEdge`. From this
|
|
128
|
+
* edge to the end of that list, every dependent is already marked
|
|
129
|
+
* `DIRTY` or `MAYBE_DIRTY`. Earlier dependents still need checking.
|
|
130
|
+
*
|
|
131
|
+
* Suppose three siblings read this node. In reader-list order they are
|
|
132
|
+
* R1, R2, R3. If the boundary is the edge to R2, this pass checks only
|
|
133
|
+
* R1, then moves the boundary to the edge to R1. When a reader becomes
|
|
134
|
+
* clean, its edge moves before the boundary so a later write checks it.
|
|
135
|
+
* An undefined boundary means marking must check the whole list.
|
|
136
|
+
*/
|
|
137
|
+
firstKnownMarkedDependentEdge:
|
|
138
|
+
| Edge<MutableDerivedNode<unknown>>
|
|
139
|
+
| undefined;
|
|
140
|
+
// Method syntax keeps MutableDerivedNode<TValue> assignable to MutableDerivedNode<unknown>.
|
|
141
|
+
computation(callingNode: CallingNode, previous: TValue | undefined): TValue;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/** Retains form a doubly linked list starting at the node’s firstRetain. */
|
|
145
|
+
export type MutableRetain<TValue> = Retain<TValue> & {
|
|
146
|
+
node: MutableDerivedNode<TValue> | undefined;
|
|
147
|
+
onChange: OnChange<TValue> | undefined;
|
|
148
|
+
previousRetain: MutableRetain<TValue> | undefined;
|
|
149
|
+
nextRetain: MutableRetain<TValue> | undefined;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export type Retain<TValue> = { readonly [retainBrand]: TValue };
|
|
153
|
+
|
|
154
|
+
declare const retainBrand: unique symbol;
|
|
155
|
+
|
|
156
|
+
// The flush queue holds nodes as MutableDerivedNode<unknown>, regardless
|
|
157
|
+
// of their value type.
|
|
158
|
+
// Extracting a method signature gives the callback a bivariant parameter:
|
|
159
|
+
// a number callback can travel through the queue, but Pico calls it only
|
|
160
|
+
// with the value of the number node that owns its retain.
|
|
161
|
+
type OnChange<TValue> = { onChange(value: TValue): void }['onChange'];
|
|
162
|
+
|
|
163
|
+
type TraversalFlags =
|
|
164
|
+
| typeof NO_FLAGS
|
|
165
|
+
| typeof MARKED_BEYOND
|
|
166
|
+
| typeof SCAN_SEEN
|
|
167
|
+
| typeof ALL_TRAVERSAL_FLAGS;
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* These invariants hold for a derived node between runs of its computation.
|
|
171
|
+
*
|
|
172
|
+
* - If the node is `UNCOMPUTED`, it has no cached result, dependencies, or dependents.
|
|
173
|
+
* - If the node is `CLEAN`, its cached result is current and every derived input
|
|
174
|
+
* is `CLEAN`. A read returns the cached result immediately.
|
|
175
|
+
* - If the node is `DIRTY`, one of its source inputs received a write or one
|
|
176
|
+
* of its derived inputs changed value. Pico reruns the node on its next refresh.
|
|
177
|
+
* - If the node is `MAYBE_DIRTY`, an upstream write may have changed a derived
|
|
178
|
+
* input. On the next refresh, Pico checks those inputs. If any changed value,
|
|
179
|
+
* Pico reruns the node. Otherwise it marks the node `CLEAN` and reuses its result.
|
|
180
|
+
*
|
|
181
|
+
* A `DIRTY` or `MAYBE_DIRTY` node retains its last computed result. Once marking
|
|
182
|
+
* finishes, each of its dependents is also `DIRTY` or `MAYBE_DIRTY`.
|
|
183
|
+
*/
|
|
184
|
+
export type Status =
|
|
185
|
+
| typeof UNCOMPUTED
|
|
186
|
+
| typeof DIRTY
|
|
187
|
+
| typeof MAYBE_DIRTY
|
|
188
|
+
| typeof CLEAN;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Internal invariant: a node is `HAS_CALLBACK` exactly while it has a retain with
|
|
192
|
+
* an `onChange`. Marking puts a node with a callback that it takes out of `CLEAN` on
|
|
193
|
+
* the flush queue, unless it reached the node only beyond another node
|
|
194
|
+
* with a callback. Every other node is lazy.
|
|
195
|
+
*/
|
|
196
|
+
type CallbackState = typeof NO_CALLBACK | typeof HAS_CALLBACK;
|