Skip to content

Commit e7d070e

Browse files
feat(proxy-state-tree): refactor to new flushing approach
BREAKING CHANGE: flush methods have changed
1 parent f174d1a commit e7d070e

File tree

5 files changed

+171
-89
lines changed

5 files changed

+171
-89
lines changed

packages/node_modules/proxy-state-tree/README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,32 @@ const tree = new ProxyStateTree({
127127

128128
const mutationTree = tree.getMutationTree()
129129

130-
mutationTree.state.foo = "bar"
130+
mutationTree.state.foo = "bar2"
131131

132-
tree.flush()
132+
mutationTree.flush()
133133
```
134134

135135
To notify trees tracking state about mutations made run the **flush** method. This allows you to control when the trackers should actually be notified.
136136

137+
You can also flush out multiple mutation trees. This can be useful in development.
138+
139+
```js
140+
const tree = new ProxyStateTree({
141+
foo: 'bar',
142+
bar: 'baz'
143+
})
144+
145+
const mutationTree = tree.getMutationTree()
146+
const mutationTree2 = tree.getMutationTree()
147+
148+
mutationTree.state.foo = "bar2"
149+
mutationTree2.state.bar = "baz2"
150+
151+
tree.flush([mutationTree, mutationTree2])
152+
```
153+
154+
A flush returns what mutations has been flushed out, also with a *flushId*.
155+
137156
## onMutation
138157

139158
```js

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,61 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
1212
master: IProxyStateTree<T>
1313
state: T
1414
proxifier: IProxifier<T>
15+
mutations: IMutation[] = []
16+
objectChanges = new Set<string>()
1517
isTracking: boolean = false
1618
constructor(master: IProxyStateTree<T>, proxifier?: IProxifier<T>) {
1719
this.isTracking = true
1820
this.master = master
1921
this.proxifier = proxifier || new Proxifier(this)
2022
this.state = this.proxifier.proxify(master.sourceState, '')
2123
}
22-
addMutation(mutation: IMutation, objectChangePath: string) {
23-
this.master.addMutation(mutation, objectChangePath)
24+
getMutations() {
25+
const mutations = this.mutations.slice()
26+
27+
this.mutations.length = 0
28+
29+
return mutations
30+
}
31+
getObjectChanges() {
32+
const objectChanges = new Set([...this.objectChanges])
33+
34+
this.objectChanges.clear()
35+
36+
return objectChanges
37+
}
38+
addMutation(mutation: IMutation, objectChangePath?: string) {
39+
const currentFlushId = this.master.currentFlushId
40+
41+
this.mutations.push(mutation)
42+
43+
if (objectChangePath) {
44+
this.objectChanges.add(objectChangePath)
45+
}
46+
47+
for (let cb of this.master.mutationCallbacks) {
48+
cb(
49+
mutation,
50+
new Set(
51+
objectChangePath ? [mutation.path, objectChangePath] : [mutation.path]
52+
),
53+
currentFlushId
54+
)
55+
}
2456

2557
for (let callback of this.mutationCallbacks) {
2658
callback(
2759
mutation,
2860
new Set(
2961
objectChangePath ? [mutation.path, objectChangePath] : [mutation.path]
3062
),
31-
this.master.currentFlushId
63+
currentFlushId
3264
)
3365
}
3466
}
67+
flush(isAsync: boolean = false) {
68+
return this.master.flush(this, isAsync)
69+
}
3570
onMutation(callback: IMutationCallback) {
3671
this.mutationCallbacks.push(callback)
3772
}

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('TrackStateAccessTree', () => {
4444

4545
mutationTree.state.foo = 'bar2'
4646

47-
tree.flush()
47+
mutationTree.flush()
4848
expect(reactionCount).toBe(1)
4949
})
5050
test('should allow tracking by mutation', () => {
@@ -141,7 +141,7 @@ describe('OBJECTS', () => {
141141
const tree = new ProxyStateTree(state)
142142
const mutationTree = tree.getMutationTree()
143143
mutationTree.state.foo = 'bar2'
144-
expect((tree as any).mutations).toEqual([
144+
expect(mutationTree.mutations).toEqual([
145145
{
146146
method: 'set',
147147
path: 'foo',
@@ -159,7 +159,7 @@ describe('OBJECTS', () => {
159159

160160
delete mutationTree.state.foo
161161

162-
expect((tree as any).mutations).toEqual([
162+
expect(mutationTree.mutations).toEqual([
163163
{
164164
method: 'unset',
165165
path: 'foo',
@@ -280,16 +280,16 @@ describe('ARRAYS', () => {
280280
title: 'foo',
281281
})
282282
const currentProxy = mutationTree.state.items[0]
283-
tree.flush()
283+
mutationTree.flush()
284284

285285
mutationTree.state.items.unshift({
286286
title: 'bar',
287287
})
288288

289-
tree.flush()
289+
mutationTree.flush()
290290
mutationTree.state.items[1].title = 'wapah'
291291
const newProxy = mutationTree.state.items[1]
292-
tree.flush()
292+
mutationTree.flush()
293293
expect(currentProxy).not.toBe(newProxy)
294294
})
295295

@@ -338,7 +338,7 @@ describe('ARRAYS', () => {
338338
const tree = new ProxyStateTree(state)
339339
const mutationTree = tree.getMutationTree()
340340
mutationTree.state.foo.push('bar')
341-
expect(tree.mutations).toEqual([
341+
expect(mutationTree.mutations).toEqual([
342342
{
343343
method: 'push',
344344
path: 'foo',
@@ -356,7 +356,7 @@ describe('ARRAYS', () => {
356356
const tree = new ProxyStateTree(state)
357357
const mutationTree = tree.getMutationTree()
358358
mutationTree.state.foo.pop()
359-
expect(tree.mutations).toEqual([
359+
expect(mutationTree.mutations).toEqual([
360360
{
361361
method: 'pop',
362362
path: 'foo',
@@ -375,7 +375,7 @@ describe('ARRAYS', () => {
375375
const mutationTree = tree.getMutationTree()
376376
mutationTree.state.foo.shift()
377377

378-
expect(tree.mutations).toEqual([
378+
expect(mutationTree.mutations).toEqual([
379379
{
380380
method: 'shift',
381381
path: 'foo',
@@ -393,7 +393,7 @@ describe('ARRAYS', () => {
393393
const mutationTree = tree.getMutationTree()
394394
mutationTree.state.foo.unshift('foo')
395395

396-
expect(tree.mutations).toEqual([
396+
expect(mutationTree.mutations).toEqual([
397397
{
398398
method: 'unshift',
399399
path: 'foo',
@@ -411,7 +411,7 @@ describe('ARRAYS', () => {
411411
const mutationTree = tree.getMutationTree()
412412
mutationTree.state.foo.splice(0, 1, 'bar')
413413

414-
expect(tree.mutations).toEqual([
414+
expect(mutationTree.mutations).toEqual([
415415
{
416416
method: 'splice',
417417
path: 'foo',
@@ -431,7 +431,7 @@ describe('ARRAYS', () => {
431431
const mutationTree = tree.getMutationTree()
432432
mutationTree.state.foo = mutationTree.state.foo.concat('bar')
433433

434-
expect(tree.mutations).toEqual([
434+
expect(mutationTree.mutations).toEqual([
435435
{
436436
method: 'set',
437437
path: 'foo',
@@ -527,7 +527,7 @@ describe('FUNCTIONS', () => {
527527
mutationTree.state.items.push({
528528
title: 'bar',
529529
})
530-
tree.flush()
530+
mutationTree.flush()
531531
expect(reactionCount).toBe(1)
532532
})
533533
})
@@ -549,7 +549,7 @@ describe('REACTIONS', () => {
549549
accessTree.state.foo // eslint-disable-line
550550

551551
mutationTree.state.foo = 'bar2'
552-
tree.flush()
552+
mutationTree.flush()
553553
expect(reactionCount).toBe(1)
554554
})
555555

@@ -569,7 +569,7 @@ describe('REACTIONS', () => {
569569
})
570570

571571
mutationTree.state.foo = 'bar2'
572-
tree.flush()
572+
mutationTree.flush()
573573
})
574574

575575
test('should be able to manually add a path to the current tracking', () => {
@@ -586,7 +586,7 @@ describe('REACTIONS', () => {
586586
const accessTree = tree.getTrackStateTree().track(render)
587587
accessTree.addTrackingPath('foo')
588588
mutationTree.state.foo = 'bar2'
589-
tree.flush()
589+
mutationTree.flush()
590590
expect(renderCount).toBe(1)
591591
})
592592

@@ -607,7 +607,7 @@ describe('REACTIONS', () => {
607607
mutationTree.state.foo = 'bar2'
608608
mutationTree.state.bar = 'baz2'
609609

610-
tree.flush()
610+
mutationTree.flush()
611611
expect(reactionCount).toBe(1)
612612
})
613613

@@ -617,7 +617,7 @@ describe('REACTIONS', () => {
617617
bar: 'baz',
618618
})
619619
const accessTree = tree.getTrackStateTree().track(render)
620-
const mutationtree = tree.getMutationTree()
620+
const mutationTree = tree.getMutationTree()
621621
function render() {
622622
if (accessTree.state.foo === 'bar') {
623623
return
@@ -627,8 +627,8 @@ describe('REACTIONS', () => {
627627
}
628628

629629
render()
630-
mutationtree.state.foo = 'bar2'
631-
tree.flush()
630+
mutationTree.state.foo = 'bar2'
631+
mutationTree.flush()
632632
expect((tree.pathDependencies as any).foo.size).toBe(1)
633633
expect((tree.pathDependencies as any).bar.size).toBe(1)
634634
})
@@ -649,11 +649,10 @@ describe('REACTIONS', () => {
649649

650650
render()
651651
mutationTree.state.foo = 'bar2'
652-
tree.flush()
652+
mutationTree.flush()
653653
tree.disposeTree(accessTree)
654654
expect(tree.pathDependencies).toEqual({})
655655
})
656-
657656
test('should allow subsequent mutation tracking and return all on flush', () => {
658657
const tree = new ProxyStateTree({
659658
foo: 'bar',
@@ -663,7 +662,7 @@ describe('REACTIONS', () => {
663662
mutationTree.state.foo = 'bar2'
664663
mutationTree2.state.foo = 'bar3'
665664

666-
const flushResult = tree.flush()
665+
const flushResult = tree.flush([mutationTree, mutationTree2])
667666
expect(flushResult).toEqual({
668667
flushId: 0,
669668
mutations: [
@@ -748,11 +747,11 @@ describe('ITERATIONS', () => {
748747

749748
update()
750749
mutationTree.state.items.foo = 'bar'
751-
tree.flush()
750+
mutationTree.flush()
752751
mutationTree.state.items.bar = 'baz'
753-
tree.flush()
752+
mutationTree.flush()
754753
delete mutationTree.state.items.foo
755-
tree.flush()
754+
mutationTree.flush()
756755
})
757756

758757
test('should react to array mutation methods', () => {
@@ -780,7 +779,7 @@ describe('ITERATIONS', () => {
780779
mutationTree.state.items.push({
781780
title: 'mip',
782781
})
783-
tree.flush()
782+
mutationTree.flush()
784783
expect(reactionCount).toBe(1)
785784
})
786785

@@ -806,7 +805,7 @@ describe('ITERATIONS', () => {
806805
new Set(['items', 'items.0', 'items.0.title', 'items.1', 'items.1.title'])
807806
)
808807
mutationTree.state.items[0].title = 'baz'
809-
tree.flush()
808+
mutationTree.flush()
810809
expect(reactionCount).toBe(1)
811810
})
812811

@@ -825,7 +824,7 @@ describe('ITERATIONS', () => {
825824
new Set(['items', 'items.0', 'items.1'])
826825
)
827826
mutationTree.state.items[0] = 99
828-
tree.flush()
827+
mutationTree.flush()
829828
expect(reactionCount).toBe(1)
830829
})
831830
})
@@ -855,7 +854,7 @@ describe('PRODUCTION', () => {
855854
mutationTree.state.items[0].title = 'foo1'
856855
delete mutationTree.state.items[0].title
857856
mutationTree.state.items[1].title = 'bar2' // this mutation should not be tracked
858-
expect(tree.mutations).toEqual([
857+
expect(mutationTree.mutations).toEqual([
859858
{
860859
method: 'set',
861860
path: 'items.0.title',
@@ -913,7 +912,7 @@ describe('GETTER', () => {
913912
render()
914913

915914
mutationTree.state.user.firstName = 'Bob2'
916-
tree.flush()
915+
mutationTree.flush()
917916
expect(renderCount).toBe(2)
918917
})
919918
})

0 commit comments

Comments
 (0)