@@ -22,10 +22,12 @@ export class Cell<T> {
2222 _value : T
2323 _lastValue : T
2424 _isEqual : EqualityFn = tripleEq
25+ _name : string | undefined
2526
26- constructor ( initialValue : T , isEqual : EqualityFn = tripleEq ) {
27+ constructor ( initialValue : T , isEqual : EqualityFn = tripleEq , name ?: string ) {
2728 this . _value = this . _lastValue = initialValue
2829 this . _isEqual = isEqual
30+ this . _name = name
2931 }
3032
3133 // 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) {
6062export class TrackingCache {
6163 _cachedValue : any
6264 _cachedRevision = - 1
63- _deps : any [ ] = [ ]
65+ _deps : Cell < any > [ ] = [ ]
6466 hits = 0
6567 _needsRecalculation = false
6668
@@ -79,20 +81,20 @@ export class TrackingCache {
7981 }
8082
8183 getValue = ( ) => {
82- console . log ( 'TrackedCache getValue' )
84+ // console.log('TrackedCache getValue')
8385 return this . value
8486 }
8587
8688 needsRecalculation ( ) {
8789 if ( ! this . _needsRecalculation ) {
8890 this . _needsRecalculation = this . revision > this . _cachedRevision
8991 }
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+ // )
9698 return this . _needsRecalculation
9799 }
98100
@@ -139,9 +141,9 @@ export class TrackingCache {
139141 }
140142
141143 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+ // )
145147 // When getting the value for a Cache, first we check all the dependencies of
146148 // the cache to see what their current revision is. If the current revision is
147149 // greater than the cached revision, then something has changed.
@@ -168,6 +170,9 @@ export class TrackingCache {
168170 // dependencies. If any dependency changes, this number will be less
169171 // than the new revision.
170172 this . _cachedRevision = this . revision
173+ this . _needsRecalculation = false
174+
175+ console . log ( 'Value: ' , this . _cachedValue , 'deps: ' , this . _deps )
171176 // }
172177 }
173178
@@ -180,6 +185,10 @@ export class TrackingCache {
180185 }
181186
182187 get revision ( ) {
188+ console . log ( 'Calculating revision: ' , {
189+ value : this . _cachedValue ,
190+ deps : this . _deps . map ( ( d ) => d . _name ) ,
191+ } )
183192 // The current revision is the max of all the dependencies' revisions.
184193 return Math . max ( ...this . _deps . map ( ( d ) => d . revision ) , 0 )
185194 }
@@ -209,9 +218,10 @@ export function setValue<T extends Cell<unknown>>(
209218
210219export function createCell < T = unknown > (
211220 initialValue : T ,
212- isEqual : EqualityFn = tripleEq
221+ isEqual : EqualityFn = tripleEq ,
222+ name ?: string
213223) : Cell < T > {
214- return new Cell ( initialValue , isEqual )
224+ return new Cell ( initialValue , isEqual , name )
215225}
216226
217227export function createCache < T = unknown > (
0 commit comments