@@ -22,10 +22,12 @@ export class Cell<T> {
22
22
_value : T
23
23
_lastValue : T
24
24
_isEqual : EqualityFn = tripleEq
25
+ _name : string | undefined
25
26
26
- constructor ( initialValue : T , isEqual : EqualityFn = tripleEq ) {
27
+ constructor ( initialValue : T , isEqual : EqualityFn = tripleEq , name ?: string ) {
27
28
this . _value = this . _lastValue = initialValue
28
29
this . _isEqual = isEqual
30
+ this . _name = name
29
31
}
30
32
31
33
// Whenever a storage value is read, it'll add itself to the current tracker if
@@ -60,7 +62,7 @@ function tripleEq(a: unknown, b: unknown) {
60
62
export class TrackingCache {
61
63
_cachedValue : any
62
64
_cachedRevision = - 1
63
- _deps : any [ ] = [ ]
65
+ _deps : Cell < any > [ ] = [ ]
64
66
hits = 0
65
67
_needsRecalculation = false
66
68
@@ -79,20 +81,20 @@ export class TrackingCache {
79
81
}
80
82
81
83
getValue = ( ) => {
82
- console . log ( 'TrackedCache getValue' )
84
+ // console.log('TrackedCache getValue')
83
85
return this . value
84
86
}
85
87
86
88
needsRecalculation ( ) {
87
89
if ( ! this . _needsRecalculation ) {
88
90
this . _needsRecalculation = this . revision > this . _cachedRevision
89
91
}
90
- console . log (
91
- 'Needs recalculation: ' ,
92
- this . _needsRecalculation ,
93
- this . _cachedRevision ,
94
- this . _cachedValue
95
- )
92
+ // console.log(
93
+ // 'Needs recalculation: ',
94
+ // this._needsRecalculation,
95
+ // this._cachedRevision,
96
+ // this._cachedValue
97
+ // )
96
98
return this . _needsRecalculation
97
99
}
98
100
@@ -139,9 +141,9 @@ export class TrackingCache {
139
141
}
140
142
141
143
get value ( ) {
142
- console . log (
143
- `TrackingCache value: revision = ${ this . revision } , cachedRevision = ${ this . _cachedRevision } , value = ${ this . _cachedValue } `
144
- )
144
+ // console.log(
145
+ // `TrackingCache value: revision = ${this.revision}, cachedRevision = ${this._cachedRevision}, value = ${this._cachedValue}`
146
+ // )
145
147
// When getting the value for a Cache, first we check all the dependencies of
146
148
// the cache to see what their current revision is. If the current revision is
147
149
// greater than the cached revision, then something has changed.
@@ -168,6 +170,9 @@ export class TrackingCache {
168
170
// dependencies. If any dependency changes, this number will be less
169
171
// than the new revision.
170
172
this . _cachedRevision = this . revision
173
+ this . _needsRecalculation = false
174
+
175
+ console . log ( 'Value: ' , this . _cachedValue , 'deps: ' , this . _deps )
171
176
// }
172
177
}
173
178
@@ -180,6 +185,10 @@ export class TrackingCache {
180
185
}
181
186
182
187
get revision ( ) {
188
+ console . log ( 'Calculating revision: ' , {
189
+ value : this . _cachedValue ,
190
+ deps : this . _deps . map ( ( d ) => d . _name ) ,
191
+ } )
183
192
// The current revision is the max of all the dependencies' revisions.
184
193
return Math . max ( ...this . _deps . map ( ( d ) => d . revision ) , 0 )
185
194
}
@@ -209,9 +218,10 @@ export function setValue<T extends Cell<unknown>>(
209
218
210
219
export function createCell < T = unknown > (
211
220
initialValue : T ,
212
- isEqual : EqualityFn = tripleEq
221
+ isEqual : EqualityFn = tripleEq ,
222
+ name ?: string
213
223
) : Cell < T > {
214
- return new Cell ( initialValue , isEqual )
224
+ return new Cell ( initialValue , isEqual , name )
215
225
}
216
226
217
227
export function createCache < T = unknown > (
0 commit comments