Skip to content

Commit e2c525a

Browse files
fix(proxy-state-tree): run object changes first to update state before running mutation changes
1 parent d84ceb1 commit e2c525a

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

packages/node_modules/proxy-state-tree/src/index.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,27 @@ describe('REACTIONS', () => {
392392
tree.flush()
393393
expect(reactionCount).toBe(1)
394394
})
395+
test('should be able to manually add a path to the current tracking', () => {
396+
let renderCount = 0
397+
const tree = new ProxyStateTree({
398+
foo: 'bar',
399+
})
400+
const state = tree.get()
401+
function render() {
402+
const trackId = tree.startPathsTracking()
403+
tree.addTrackingPath('foo')
404+
renderCount++
405+
return tree.clearPathsTracking(trackId)
406+
}
407+
const listener = tree.addMutationListener(render(), () => {
408+
listener.update(render())
409+
})
410+
tree.startMutationTracking()
411+
state.foo = 'bar2'
412+
tree.clearMutationTracking()
413+
tree.flush()
414+
expect(renderCount).toBe(2)
415+
})
395416
test('should only trigger ones when multiple paths mutated', () => {
396417
let reactionCount = 0
397418
const tree = new ProxyStateTree({

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,38 @@ class ProxyStateTree {
3434
get() {
3535
return this.proxy
3636
}
37+
addTrackingPath(path) {
38+
if (this.status === STATUS.TRACKING_PATHS) {
39+
this.paths[this.paths.length - 1].add(path)
40+
}
41+
}
3742
flush() {
38-
const pathCallbacksCalled = new Set()
43+
const pathCallbacksToCall = new Set()
3944
const mutations = this.mutations.slice()
4045

46+
for (let objectChange of this.objectChanges) {
47+
if (this.pathDependencies[objectChange]) {
48+
for (let callback of this.pathDependencies[objectChange]) {
49+
pathCallbacksToCall.add(callback)
50+
}
51+
}
52+
}
53+
4154
for (let mutation in this.mutations) {
4255
const path = this.mutations[mutation].path
4356

4457
if (this.pathDependencies[path]) {
4558
for (let callback of this.pathDependencies[path]) {
46-
if (!pathCallbacksCalled.has(callback)) {
47-
pathCallbacksCalled.add(callback)
48-
callback()
49-
}
59+
pathCallbacksToCall.add(callback)
5060
}
5161
}
5262
}
5363

54-
for (let objectChange of this.objectChanges) {
55-
if (this.pathDependencies[objectChange]) {
56-
for (let callback of this.pathDependencies[objectChange]) {
57-
if (!pathCallbacksCalled.has(callback)) {
58-
pathCallbacksCalled.add(callback)
59-
callback()
60-
}
61-
}
62-
}
64+
for (let callback of pathCallbacksToCall) {
65+
callback()
6366
}
64-
pathCallbacksCalled.clear()
67+
68+
pathCallbacksToCall.clear()
6569
this.mutations.length = 0
6670
this.objectChanges.clear()
6771

0 commit comments

Comments
 (0)