Skip to content

Commit 30edfb2

Browse files
feat(proxy-state-tree): aggregate mutations and reset on flush
1 parent a0ed8aa commit 30edfb2

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,31 @@ describe('REACTIONS', () => {
394394
tree.flush()
395395
expect(tree.pathDependencies).toEqual({})
396396
})
397+
test('should allow subsequent mutation tracking and return all on flush', () => {
398+
const tree = new ProxyStateTree({
399+
foo: 'bar',
400+
})
401+
const state = tree.get()
402+
tree.startMutationTracking()
403+
state.foo = 'bar2'
404+
tree.clearMutationTracking()
405+
tree.startMutationTracking()
406+
state.foo = 'bar3'
407+
tree.clearMutationTracking()
408+
const mutations = tree.flush()
409+
expect(mutations).toEqual([
410+
{
411+
path: 'foo',
412+
method: 'set',
413+
args: ['bar2'],
414+
},
415+
{
416+
path: 'foo',
417+
method: 'set',
418+
args: ['bar3'],
419+
},
420+
])
421+
})
397422
})
398423

399424
describe('ITERATIONS', () => {

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ProxyStateTree {
1515
options: Options
1616
pathDependencies: object
1717
mutations: Mutation[]
18+
currentMutations: Mutation[]
1819
paths: Set<string>[]
1920
status: STATUS
2021
proxy: any
@@ -23,6 +24,7 @@ class ProxyStateTree {
2324
this.options = options
2425
this.pathDependencies = {}
2526
this.mutations = []
27+
this.currentMutations = []
2628
this.paths = []
2729
this.status = STATUS.IDLE
2830
this.proxy = proxify(this, state)
@@ -32,6 +34,7 @@ class ProxyStateTree {
3234
}
3335
flush() {
3436
const pathCallbacksCalled = new Set()
37+
const mutations = this.mutations.slice()
3538

3639
for (let mutation in this.mutations) {
3740
const path = this.mutations[mutation].path
@@ -46,6 +49,9 @@ class ProxyStateTree {
4649
}
4750
}
4851
pathCallbacksCalled.clear()
52+
this.mutations.length = 0
53+
54+
return mutations
4955
}
5056
startMutationTracking() {
5157
if (this.status !== STATUS.IDLE) {
@@ -55,18 +61,16 @@ class ProxyStateTree {
5561
}`
5662
)
5763
}
58-
59-
const currentMutations = this.mutations.slice()
60-
6164
this.status = STATUS.TRACKING_MUTATIONS
62-
this.mutations.length = 0
63-
64-
return currentMutations
6565
}
6666
clearMutationTracking() {
67+
const currentMutations = this.currentMutations.slice()
68+
6769
this.status = STATUS.IDLE
70+
this.currentMutations.length = 0
71+
this.mutations = this.mutations.concat(currentMutations)
6872

69-
return this.mutations.slice()
73+
return currentMutations
7074
}
7175
startPathsTracking() {
7276
if (this.status === STATUS.TRACKING_MUTATIONS) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function createArrayProxy(tree, value, path) {
5757
)
5858
}
5959
return (...args) => {
60-
tree.mutations.push({
60+
tree.currentMutations.push({
6161
method: prop,
6262
path: path,
6363
args: args,
@@ -77,7 +77,7 @@ function createArrayProxy(tree, value, path) {
7777
)
7878
}
7979

80-
tree.mutations.push({
80+
tree.currentMutations.push({
8181
method: 'set',
8282
path: nestedPath,
8383
args: [value],
@@ -114,7 +114,7 @@ function createObjectProxy(tree, value, path) {
114114
)
115115
}
116116
if (shouldTrackMutations(tree, nestedPath)) {
117-
tree.mutations.push({
117+
tree.currentMutations.push({
118118
method: 'set',
119119
path: nestedPath,
120120
args: [value],
@@ -132,7 +132,7 @@ function createObjectProxy(tree, value, path) {
132132
)
133133
}
134134
if (shouldTrackMutations(tree, nestedPath)) {
135-
tree.mutations.push({
135+
tree.currentMutations.push({
136136
method: 'unset',
137137
path: nestedPath,
138138
args: [],

0 commit comments

Comments
 (0)