Skip to content

Commit 7f88124

Browse files
committed
WIP Seemingly actually skips unused tracked selectors correctly?
1 parent 609643a commit 7f88124

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/utils/autotracking/autotracking.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export class TrackingCache {
9898
return this._needsRecalculation
9999
}
100100

101+
/*
101102
getWithArgs = (...args: any[]) => {
102103
// console.log(
103104
// `TrackingCache value: revision = ${this.revision}, cachedRevision = ${this._cachedRevision}, value = ${this._cachedValue}`
@@ -139,7 +140,7 @@ export class TrackingCache {
139140
// Always return the cached value.
140141
return this._cachedValue
141142
}
142-
143+
*/
143144
get value() {
144145
// console.log(
145146
// `TrackingCache value: revision = ${this.revision}, cachedRevision = ${this._cachedRevision}, value = ${this._cachedValue}`
@@ -172,7 +173,7 @@ export class TrackingCache {
172173
this._cachedRevision = this.revision
173174
this._needsRecalculation = false
174175

175-
console.log('Value: ', this._cachedValue, 'deps: ', this._deps)
176+
// console.log('Value: ', this._cachedValue, 'deps: ', this._deps)
176177
// }
177178
}
178179

@@ -185,10 +186,10 @@ export class TrackingCache {
185186
}
186187

187188
get revision() {
188-
console.log('Calculating revision: ', {
189-
value: this._cachedValue,
190-
deps: this._deps.map((d) => d._name),
191-
})
189+
// console.log('Calculating revision: ', {
190+
// value: this._cachedValue,
191+
// deps: this._deps.map((d) => d._name),
192+
// })
192193
// The current revision is the max of all the dependencies' revisions.
193194
return Math.max(...this._deps.map((d) => d.revision), 0)
194195
}

test/hooks/useSelector.spec.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -521,33 +521,40 @@ describe('React', () => {
521521
expect(selector2).toHaveBeenCalledTimes(1)
522522
expect(selector3).toHaveBeenCalledTimes(1)
523523

524-
// expect(listeners[0].selectorCache!.cache.needsRecalculation()).toBe(
525-
// false
526-
// )
527-
// expect(listeners[1].selectorCache!.cache.needsRecalculation()).toBe(
528-
// false
529-
// )
530-
// expect(listeners[2].selectorCache!.cache.needsRecalculation()).toBe(
531-
// false
532-
// )
524+
expect(listeners[0].selectorCache!.cache.needsRecalculation()).toBe(
525+
false
526+
)
527+
expect(listeners[1].selectorCache!.cache.needsRecalculation()).toBe(
528+
false
529+
)
530+
expect(listeners[2].selectorCache!.cache.needsRecalculation()).toBe(
531+
false
532+
)
533533

534534
rtl.act(() => {
535535
console.log('Dispatching action')
536536
store.dispatch(countersSlice.actions.increment1())
537+
console.log('Dispatch complete')
537538

538539
expect(selector1).toHaveBeenCalledTimes(2)
539540
expect(selector2).toHaveBeenCalledTimes(1)
540541
expect(selector3).toHaveBeenCalledTimes(1)
542+
})
541543

542-
// expect(listeners[0].selectorCache!.cache.needsRecalculation()).toBe(
543-
// true
544-
// )
545-
// expect(listeners[1].selectorCache!.cache.needsRecalculation()).toBe(
546-
// false
547-
// )
548-
// expect(listeners[2].selectorCache!.cache.needsRecalculation()).toBe(
549-
// false
550-
// )
544+
rtl.act(() => {
545+
store.dispatch(countersSlice.actions.increment2())
546+
547+
expect(selector1).toHaveBeenCalledTimes(2)
548+
expect(selector2).toHaveBeenCalledTimes(2)
549+
expect(selector3).toHaveBeenCalledTimes(1)
550+
})
551+
552+
rtl.act(() => {
553+
store.dispatch(countersSlice.actions.increment3())
554+
555+
expect(selector1).toHaveBeenCalledTimes(2)
556+
expect(selector2).toHaveBeenCalledTimes(2)
557+
expect(selector3).toHaveBeenCalledTimes(2)
551558
})
552559
})
553560
})

0 commit comments

Comments
 (0)