Skip to content

Commit f9d61d1

Browse files
committed
Remove _flags from Nodes
Use Node._version === -1 to replace the NODE_FREE flag.
1 parent 7875166 commit f9d61d1

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

packages/core/src/index.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,9 @@ const DISPOSED = 1 << 3;
1010
const HAS_ERROR = 1 << 4;
1111
const TRACKING = 1 << 5;
1212

13-
// Flags for Nodes.
14-
const NODE_FREE = 1 << 0;
15-
1613
// A linked list node used to track dependencies (sources) and dependents (targets).
1714
// Also used to remember the source's last version number that the target saw.
1815
type Node = {
19-
// A node may have the following flags:
20-
// NODE_FREE when it's unclear whether the source is still a dependency of the target
21-
_flags: number;
22-
2316
// A source whose value the target depends on.
2417
_source: Signal;
2518
_prevSource?: Node;
@@ -33,6 +26,7 @@ type Node = {
3326
// The version number of the source that target has last seen. We use version numbers
3427
// instead of storing the source value, because source values can take arbitrary amount
3528
// of memory, and computeds could hang on to them forever because they're lazily evaluated.
29+
// Use the special value -1 to mark potentially unused but recyclable nodes.
3630
_version: number;
3731

3832
// Used to remember & roll back the source's previous `._node` value when entering &
@@ -119,7 +113,6 @@ function addDependency(signal: Signal): Node | undefined {
119113
// `signal` is a new dependency. Create a new node dependency node, move it
120114
// to the front of the current context's dependency list.
121115
node = {
122-
_flags: 0,
123116
_version: 0,
124117
_source: signal,
125118
_prevSource: undefined,
@@ -138,10 +131,10 @@ function addDependency(signal: Signal): Node | undefined {
138131
signal._subscribe(node);
139132
}
140133
return node;
141-
} else if (node._flags & NODE_FREE) {
134+
} else if (node._version === -1) {
142135
// `signal` is an existing dependency from a previous evaluation. Reuse the dependency
143136
// node and move it to the front of the evaluation context's dependency list.
144-
node._flags &= ~NODE_FREE;
137+
node._version = 0;
145138

146139
const head = evalContext._sources;
147140
if (node !== head) {
@@ -172,7 +165,10 @@ declare class Signal<T = any> {
172165
/** @internal */
173166
_value: unknown;
174167

175-
/** @internal */
168+
/** @internal
169+
* Version numbers should always be >= 0, because the special value -1 is used
170+
* by Nodes to signify potentially unused but recyclable notes.
171+
*/
176172
_version: number;
177173

178174
/** @internal */
@@ -340,7 +336,7 @@ function prepareSources(target: Computed | Effect) {
340336
node._rollbackNode = rollbackNode;
341337
}
342338
node._source._node = node;
343-
node._flags |= NODE_FREE;
339+
node._version = -1;
344340
}
345341
}
346342

@@ -355,7 +351,7 @@ function cleanupSources(target: Computed | Effect) {
355351
let sources = undefined;
356352
while (node !== undefined) {
357353
const next = node._nextSource;
358-
if (node._flags & NODE_FREE) {
354+
if (node._version === -1) {
359355
node._source._unsubscribe(node);
360356
node._nextSource = undefined;
361357
} else {

0 commit comments

Comments
 (0)