Skip to content

Commit 1ebb364

Browse files
fix(overmind): correctly track on current tree
1 parent e0c0dd4 commit 1ebb364

File tree

5 files changed

+42
-61
lines changed

5 files changed

+42
-61
lines changed

packages/node_modules/overmind/src/derived.ts

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { EventType, Events } from './internalTypes'
1414
export class Derived {
1515
private isDirty: boolean = true
1616
private trackStateTree: ITrackStateTree<any>
17-
private previousTreeAccess: any
17+
private previousProxifier: any
1818
private value: any
1919
private paths: Set<string>
2020
private updateCount: number = 0
@@ -31,12 +31,12 @@ export class Derived {
3131

3232
return boundEvaluate
3333
}
34-
private createScope(tree, path) {
34+
private runScope(tree, path) {
3535
const pathAsArray = path.split('.')
3636
pathAsArray.pop()
3737
const parent = pathAsArray.reduce((curr, key) => curr[key], tree.state)
3838

39-
return () => this.cb(parent, tree.state)
39+
return this.cb(parent, tree.state)
4040
}
4141
evaluate(
4242
eventHub: EventEmitter<Events>,
@@ -47,50 +47,33 @@ export class Derived {
4747
if (!this.trackStateTree) {
4848
this.trackStateTree = proxyStateTree.getTrackStateTree()
4949
this.trackStateTree.shouldTrack = true
50-
this.trackStateTree.callback = () => {
51-
this.isDirty = true
52-
}
50+
this.disposeOnMutation = proxyStateTree.onMutation(
51+
(_, paths, flushId) => {
52+
if (this.isDirty) {
53+
return
54+
}
5355

54-
if (process.env.NODE_ENV === 'development') {
55-
this.disposeOnMutation = proxyStateTree.onMutation(
56-
(_, paths, flushId) => {
57-
if (this.isDirty) {
56+
for (let mutationPath of paths) {
57+
if (this.paths.has(mutationPath)) {
58+
this.isDirty = true
59+
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
60+
derivedPath: path,
61+
path: mutationPath,
62+
flushId,
63+
})
5864
return
5965
}
60-
61-
for (let mutationPath of paths) {
62-
if (this.paths.has(mutationPath)) {
63-
this.isDirty = true
64-
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
65-
derivedPath: path,
66-
path: mutationPath,
67-
flushId,
68-
})
69-
return
70-
}
71-
}
7266
}
73-
)
74-
}
75-
}
76-
77-
let isDirty
78-
79-
// During development we need to re-evaluate the derived so that
80-
// it tracks on the mutation tree passed in, in case of mutations
81-
// of the returned derived
82-
if (process.env.NODE_ENV === 'development') {
83-
isDirty =
84-
this.isDirty ||
85-
(tree instanceof MutationTree && this.previousTreeAccess !== tree)
86-
} else {
87-
isDirty = this.isDirty
67+
}
68+
)
8869
}
8970

90-
if (isDirty) {
71+
// During development we need to move the ownership of whatever state is returned from
72+
// the derived to track it correctly. In production we only have one proxifier, so no worries
73+
if (this.isDirty || this.previousProxifier !== tree.proxifier) {
9174
const getPaths = tree.trackPaths()
9275

93-
this.value = this.createScope(tree, path)()
76+
this.value = this.runScope(tree, path)
9477
this.isDirty = false
9578
this.paths = getPaths()
9679

@@ -118,7 +101,7 @@ export class Derived {
118101
}
119102
}
120103

121-
this.previousTreeAccess = tree
104+
this.previousProxifier = tree.proxifier
122105

123106
return this.value
124107
}

packages/node_modules/proxy-state-tree/src/MutationTree.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
1515
mutations: IMutation[] = []
1616
objectChanges = new Set<string>()
1717
isTracking: boolean = false
18-
onPathAccess: (path: string) => void
18+
trackPathListeners: Array<(path: string) => void> = []
1919
constructor(master: IProxyStateTree<T>, proxifier?: IProxifier<T>) {
2020
this.isTracking = true
2121
this.master = master
@@ -24,11 +24,14 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
2424
}
2525
trackPaths() {
2626
const paths = new Set<string>()
27-
this.onPathAccess = (path) => {
27+
const listener = (path) => {
2828
paths.add(path)
2929
}
30+
const index = this.trackPathListeners.push(listener) - 1
31+
3032
return () => {
31-
this.onPathAccess = null
33+
this.trackPathListeners.splice(index, 1)
34+
3235
return paths
3336
}
3437
}

packages/node_modules/proxy-state-tree/src/Proxyfier.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export class Proxifier {
110110

111111
const trackingTree = proxifier.getTrackingTree()
112112
const nestedPath = proxifier.concat(path, prop)
113+
const currentTree = trackingTree || proxifier.tree
113114

114115
trackingTree && trackingTree.proxifier.trackPath(nestedPath)
115-
116-
proxifier.tree.onPathAccess && proxifier.tree.onPathAccess(nestedPath)
116+
currentTree.trackPathListeners.forEach((cb) => cb(nestedPath))
117117

118118
const method = String(prop)
119119

@@ -203,25 +203,19 @@ export class Proxifier {
203203
const trackingTree = proxifier.getTrackingTree()
204204
const targetValue = target[prop]
205205
const nestedPath = proxifier.concat(path, prop)
206+
const currentTree = trackingTree || proxifier.tree
206207

207208
trackingTree && trackingTree.proxifier.trackPath(nestedPath)
208-
209-
proxifier.tree.onPathAccess && proxifier.tree.onPathAccess(nestedPath)
209+
currentTree.trackPathListeners.forEach((cb) => cb(nestedPath))
210210

211211
if (typeof targetValue === 'function') {
212-
const dynamicValue = proxifier.tree.master.options.dynamicWrapper
212+
return proxifier.tree.master.options.dynamicWrapper
213213
? proxifier.tree.master.options.dynamicWrapper(
214-
proxifier.tree,
214+
trackingTree || proxifier.tree,
215215
nestedPath,
216216
targetValue
217217
)
218218
: targetValue(proxifier.tree, nestedPath)
219-
220-
if (dynamicValue && dynamicValue[IS_PROXY]) {
221-
return proxifier.proxify(dynamicValue[VALUE], dynamicValue[PATH])
222-
}
223-
224-
return dynamicValue
225219
}
226220

227221
if (targetValue === undefined) {

packages/node_modules/proxy-state-tree/src/TrackStateTree.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
1313
shouldTrack: boolean = false
1414
state: T
1515
proxifier: IProxifier<T>
16-
onPathAccess: (path: string) => void
16+
trackPathListeners: Array<(path: string) => void> = []
1717
constructor(master: IProxyStateTree<T>) {
1818
this.master = master
1919
this.proxifier = master.proxifier
2020
this.state = master.state
2121
}
2222
trackPaths() {
2323
const paths = new Set<string>()
24-
25-
this.onPathAccess = (path) => {
24+
const listener = (path) => {
2625
paths.add(path)
2726
}
27+
const index = this.trackPathListeners.push(listener) - 1
2828

2929
return () => {
30-
this.onPathAccess = null
30+
this.trackPathListeners.splice(index, 1)
31+
3132
return paths
3233
}
3334
}

packages/node_modules/proxy-state-tree/src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface IMutationTree<T extends object> {
3434
proxifier: IProxifier<T>
3535
mutations: IMutation[]
3636
state: T
37-
onPathAccess: (path: string) => void
37+
trackPathListeners: Array<(path: string) => void>
3838
}
3939

4040
export interface ITrackCallback {
@@ -66,7 +66,7 @@ export interface ITrackStateTree<T extends object> {
6666
state: T
6767
pathDependencies: Set<string>
6868
callback: ITrackCallback
69-
onPathAccess: (path: string) => void
69+
trackPathListeners: Array<(path: string) => void>
7070
}
7171

7272
export interface IOptions {

0 commit comments

Comments
 (0)