@@ -10,16 +10,9 @@ const DISPOSED = 1 << 3;
10
10
const HAS_ERROR = 1 << 4 ;
11
11
const TRACKING = 1 << 5 ;
12
12
13
- // Flags for Nodes.
14
- const NODE_FREE = 1 << 0 ;
15
-
16
13
// A linked list node used to track dependencies (sources) and dependents (targets).
17
14
// Also used to remember the source's last version number that the target saw.
18
15
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
-
23
16
// A source whose value the target depends on.
24
17
_source : Signal ;
25
18
_prevSource ?: Node ;
@@ -33,6 +26,7 @@ type Node = {
33
26
// The version number of the source that target has last seen. We use version numbers
34
27
// instead of storing the source value, because source values can take arbitrary amount
35
28
// 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.
36
30
_version : number ;
37
31
38
32
// Used to remember & roll back the source's previous `._node` value when entering &
@@ -119,7 +113,6 @@ function addDependency(signal: Signal): Node | undefined {
119
113
// `signal` is a new dependency. Create a new node dependency node, move it
120
114
// to the front of the current context's dependency list.
121
115
node = {
122
- _flags : 0 ,
123
116
_version : 0 ,
124
117
_source : signal ,
125
118
_prevSource : undefined ,
@@ -138,10 +131,10 @@ function addDependency(signal: Signal): Node | undefined {
138
131
signal . _subscribe ( node ) ;
139
132
}
140
133
return node ;
141
- } else if ( node . _flags & NODE_FREE ) {
134
+ } else if ( node . _version === - 1 ) {
142
135
// `signal` is an existing dependency from a previous evaluation. Reuse the dependency
143
136
// node and move it to the front of the evaluation context's dependency list.
144
- node . _flags &= ~ NODE_FREE ;
137
+ node . _version = 0 ;
145
138
146
139
const head = evalContext . _sources ;
147
140
if ( node !== head ) {
@@ -172,7 +165,10 @@ declare class Signal<T = any> {
172
165
/** @internal */
173
166
_value : unknown ;
174
167
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
+ */
176
172
_version : number ;
177
173
178
174
/** @internal */
@@ -340,7 +336,7 @@ function prepareSources(target: Computed | Effect) {
340
336
node . _rollbackNode = rollbackNode ;
341
337
}
342
338
node . _source . _node = node ;
343
- node . _flags |= NODE_FREE ;
339
+ node . _version = - 1 ;
344
340
}
345
341
}
346
342
@@ -355,7 +351,7 @@ function cleanupSources(target: Computed | Effect) {
355
351
let sources = undefined ;
356
352
while ( node !== undefined ) {
357
353
const next = node . _nextSource ;
358
- if ( node . _flags & NODE_FREE ) {
354
+ if ( node . _version === - 1 ) {
359
355
node . _source . _unsubscribe ( node ) ;
360
356
node . _nextSource = undefined ;
361
357
} else {
0 commit comments