Skip to content

Commit 2a350be

Browse files
feat(proxy-state-tree): only notify about change when value actually changed
1 parent 52bb017 commit 2a350be

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

packages/node_modules/overmind/src/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ describe('Overmind', () => {
216216
args: ['bar2'],
217217
method: 'set',
218218
path: 'foo',
219+
hasChangedValue: true,
219220
},
220221
],
221222
executionId: 0,
@@ -239,6 +240,7 @@ describe('Overmind', () => {
239240
args: ['bar2'],
240241
method: 'set',
241242
path: 'foo',
243+
hasChangedValue: true,
242244
},
243245
],
244246
executionId: 0,
@@ -262,6 +264,7 @@ describe('Overmind', () => {
262264
args: ['bar2'],
263265
method: 'set',
264266
path: 'foo',
267+
hasChangedValue: true,
265268
},
266269
],
267270
executionId: 0,

packages/node_modules/overmind/src/mock.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('Mock', () => {
3737
method: 'set',
3838
path: 'foo',
3939
args: ['bar2!!!'],
40+
hasChangedValue: true,
4041
},
4142
])
4243
})
@@ -73,6 +74,7 @@ describe('Mock', () => {
7374
method: 'set',
7475
path: 'foo',
7576
args: ['bar!'],
77+
hasChangedValue: true,
7678
},
7779
])
7880
})

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export class Proxifier {
201201
method,
202202
path: path,
203203
args: args,
204+
hasChangedValue: true,
204205
})
205206

206207
return target[prop](...args)
@@ -229,6 +230,7 @@ export class Proxifier {
229230
method: 'set',
230231
path: nestedPath,
231232
args: [value],
233+
hasChangedValue: true,
232234
})
233235

234236
return Reflect.set(target, prop, value)
@@ -328,6 +330,7 @@ export class Proxifier {
328330
method: 'set',
329331
path: nestedPath,
330332
args: [value],
333+
hasChangedValue: value !== target[prop],
331334
},
332335
objectChangePath
333336
)
@@ -359,6 +362,7 @@ export class Proxifier {
359362
method: 'unset',
360363
path: nestedPath,
361364
args: [],
365+
hasChangedValue: true,
362366
},
363367
objectChangePath
364368
)

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ describe('TrackStateAccessTree', () => {
5959
method: 'set',
6060
path: 'foo',
6161
args: ['bar2'],
62+
hasChangedValue: true,
6263
})
6364
})
6465

@@ -184,7 +185,28 @@ describe('OBJECTS', () => {
184185
mutationTree.state.foo = mutationTree.state.foo // eslint-disable-line
185186
}).toThrowError(/exists in the state tree on path "foo"/i)
186187
})
187-
188+
test('should not notify changes to same value', () => {
189+
let renderCount = 0
190+
const state = {
191+
foo: 'bar',
192+
}
193+
const tree = new ProxyStateTree(state)
194+
const mutationTree = tree.getMutationTree()
195+
const accessTree = tree.getTrackStateTree().track(() => {
196+
renderCount++
197+
})
198+
accessTree.addTrackingPath('foo')
199+
mutationTree.state.foo = 'bar'
200+
expect(mutationTree.mutations).toEqual([
201+
{
202+
method: 'set',
203+
path: 'foo',
204+
args: ['bar'],
205+
hasChangedValue: false,
206+
},
207+
])
208+
expect(renderCount).toBe(0)
209+
})
188210
test('should track SET mutations', () => {
189211
const state = {
190212
foo: 'bar',
@@ -197,6 +219,7 @@ describe('OBJECTS', () => {
197219
method: 'set',
198220
path: 'foo',
199221
args: ['bar2'],
222+
hasChangedValue: true,
200223
},
201224
])
202225
expect(mutationTree.state.foo).toBe('bar2')
@@ -215,6 +238,7 @@ describe('OBJECTS', () => {
215238
method: 'unset',
216239
path: 'foo',
217240
args: [],
241+
hasChangedValue: true,
218242
},
219243
])
220244
expect(mutationTree.state.foo).toBe(undefined)
@@ -394,6 +418,7 @@ describe('ARRAYS', () => {
394418
method: 'push',
395419
path: 'foo',
396420
args: ['bar'],
421+
hasChangedValue: true,
397422
},
398423
])
399424

@@ -412,6 +437,7 @@ describe('ARRAYS', () => {
412437
method: 'pop',
413438
path: 'foo',
414439
args: [],
440+
hasChangedValue: true,
415441
},
416442
])
417443

@@ -431,6 +457,7 @@ describe('ARRAYS', () => {
431457
method: 'shift',
432458
path: 'foo',
433459
args: [],
460+
hasChangedValue: true,
434461
},
435462
])
436463

@@ -449,6 +476,7 @@ describe('ARRAYS', () => {
449476
method: 'unshift',
450477
path: 'foo',
451478
args: ['foo'],
479+
hasChangedValue: true,
452480
},
453481
])
454482

@@ -467,6 +495,7 @@ describe('ARRAYS', () => {
467495
method: 'splice',
468496
path: 'foo',
469497
args: [0, 1, 'bar'],
498+
hasChangedValue: true,
470499
},
471500
])
472501

@@ -487,6 +516,7 @@ describe('ARRAYS', () => {
487516
method: 'set',
488517
path: 'foo',
489518
args: [['foo', 'bar']],
519+
hasChangedValue: true,
490520
},
491521
])
492522

@@ -616,6 +646,7 @@ describe('REACTIONS', () => {
616646
method: 'set',
617647
path: 'foo',
618648
args: ['bar2'],
649+
hasChangedValue: true,
619650
})
620651
})
621652

@@ -721,11 +752,13 @@ describe('REACTIONS', () => {
721752
path: 'foo',
722753
method: 'set',
723754
args: ['bar2'],
755+
hasChangedValue: true,
724756
},
725757
{
726758
path: 'foo',
727759
method: 'set',
728760
args: ['bar3'],
761+
hasChangedValue: true,
729762
},
730763
],
731764
})

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
190190
}
191191

192192
for (let mutation of changes.mutations) {
193-
paths.add(mutation.path)
193+
if (mutation.hasChangedValue) {
194+
paths.add(mutation.path)
195+
}
194196
}
195197

196198
// Sort so that parent paths are called first

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface IMutation {
88
method: string
99
path: string
1010
args: any[]
11+
hasChangedValue: boolean
1112
}
1213

1314
export interface IMutationCallback {

0 commit comments

Comments
 (0)