Skip to content

Commit 609643a

Browse files
committed
WIP fixed recalc bug?
1 parent 20caf40 commit 609643a

File tree

6 files changed

+152
-43
lines changed

6 files changed

+152
-43
lines changed

src/hooks/useSelector.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,16 @@ export function createSelectorHook(context = ReactReduxContext): UseSelector {
142142

143143
const latestWrappedSelectorRef = useRef(wrappedSelector)
144144

145-
console.log(
146-
'Writing latest selector. Same reference? ',
147-
wrappedSelector === latestWrappedSelectorRef.current
148-
)
145+
// console.log(
146+
// 'Writing latest selector. Same reference? ',
147+
// wrappedSelector === latestWrappedSelectorRef.current
148+
// )
149149
latestWrappedSelectorRef.current = wrappedSelector
150150

151151
const cache = useMemo(() => {
152+
console.log('Recreating cache')
152153
const cache = createCache(() => {
153-
console.log('Wrapper cache called: ', store.getState())
154+
// console.log('Wrapper cache called: ', store.getState())
154155
//return latestWrappedSelectorRef.current(trackingNode.proxy as TState)
155156
return wrappedSelector(trackingNode.proxy as TState)
156157
})

src/utils/Subscription.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ function createListenerCollection() {
4646
//console.log('Listener: ', listener)
4747
if (listener.trigger == 'tracked') {
4848
if (listener.selectorCache!.cache.needsRecalculation()) {
49-
console.log('Calling subscriber due to recalc need')
50-
// console.log(
51-
// 'Calling subscriber due to recalc. Revision before: ',
52-
// $REVISION
53-
// )
49+
//console.log('Calling subscriber due to recalc need')
50+
console.log(
51+
'Calling subscriber due to recalc. Revision before: ',
52+
$REVISION
53+
)
5454
listener.callback()
5555
//console.log('Revision after: ', $REVISION)
5656
} else {
@@ -169,7 +169,7 @@ export function createSubscription(
169169

170170
function notifyNestedSubs() {
171171
if (store && trackingNode) {
172-
//console.log('Updating node in notifyNestedSubs')
172+
console.log('Updating node in notifyNestedSubs')
173173
updateNode(trackingNode, store.getState())
174174
}
175175
listeners.notify()

src/utils/autotracking/autotracking.ts

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {
6062
export 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

210219
export 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

217227
export function createCache<T = unknown>(

src/utils/autotracking/proxy.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const proto = Object.getPrototypeOf({})
1919

2020
class ObjectTreeNode<T extends Record<string, unknown>> implements Node<T> {
2121
proxy: T = new Proxy(this, objectProxyHandler) as unknown as T
22-
tag = createTag()
22+
tag = createTag('object')
2323
tags = {} as Record<string, Tag>
2424
children = {} as Record<string, Node>
2525
collectionTag = null
@@ -33,7 +33,7 @@ class ObjectTreeNode<T extends Record<string, unknown>> implements Node<T> {
3333

3434
const objectProxyHandler = {
3535
get(node: Node, key: string | symbol): unknown {
36-
console.log('Reading key: ', key, node.value)
36+
//console.log('Reading key: ', key, node.value)
3737

3838
function calculateResult() {
3939
const { value } = node
@@ -64,7 +64,7 @@ const objectProxyHandler = {
6464
let tag = node.tags[key]
6565

6666
if (tag === undefined) {
67-
tag = node.tags[key] = createTag()
67+
tag = node.tags[key] = createTag(key)
6868
tag.value = childValue
6969
}
7070

@@ -96,7 +96,7 @@ const objectProxyHandler = {
9696

9797
class ArrayTreeNode<T extends Array<unknown>> implements Node<T> {
9898
proxy: T = new Proxy([this], arrayProxyHandler) as unknown as T
99-
tag = createTag()
99+
tag = createTag('array')
100100
tags = {}
101101
children = {}
102102
collectionTag = null
@@ -191,7 +191,7 @@ export function updateNode<T extends Array<unknown> | Record<string, unknown>>(
191191
}
192192

193193
for (const key in tags) {
194-
console.log('Checking tag: ', key)
194+
//console.log('Checking tag: ', key)
195195
const childValue = (value as Record<string, unknown>)[key]
196196
const newChildValue = (newValue as Record<string, unknown>)[key]
197197

@@ -206,7 +206,7 @@ export function updateNode<T extends Array<unknown> | Record<string, unknown>>(
206206
}
207207

208208
for (const key in children) {
209-
console.log(`Checking node: key = ${key}, value = ${children[key]}`)
209+
//console.log(`Checking node: key = ${key}, value = ${children[key]}`)
210210
const childNode = children[key]
211211
const newChildValue = (newValue as Record<string, unknown>)[key]
212212

src/utils/autotracking/tracking.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import {
22
createCell as createStorage,
33
getValue as consumeTag,
44
setValue,
5-
Cell
5+
Cell,
66
} from './autotracking'
77

88
export type Tag = Cell<unknown>
99

1010
const neverEq = (a: any, b: any): boolean => false
1111

12-
export function createTag(): Tag {
13-
return createStorage(null, neverEq)
12+
export function createTag(name?: string): Tag {
13+
return createStorage(null, neverEq, name)
1414
}
1515
export { consumeTag }
1616
export function dirtyTag(tag: Tag, value: any): void {
@@ -35,7 +35,7 @@ export const consumeCollection = (node: Node): void => {
3535
let tag = node.collectionTag
3636

3737
if (tag === null) {
38-
tag = node.collectionTag = createTag()
38+
tag = node.collectionTag = createTag(node.collectionTag?._name || 'Unknown')
3939
}
4040

4141
consumeTag(tag)

0 commit comments

Comments
 (0)