Skip to content

Commit e537c46

Browse files
feat(proxy-state-tree): pass information on mutation on how to revert
1 parent 59a9618 commit e537c46

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ const arrayMutations = new Set([
2121
const getValue = (proxyOrValue) =>
2222
proxyOrValue && proxyOrValue[IS_PROXY] ? proxyOrValue[VALUE] : proxyOrValue
2323

24+
const getArrayRevertValue = (method: string, target: any[], args: any[]) => {
25+
switch (method) {
26+
case 'push': {
27+
return null
28+
}
29+
case 'shift': {
30+
return target[0]
31+
}
32+
case 'pop': {
33+
return target[target.length - 1]
34+
}
35+
case 'unshift': {
36+
return null
37+
}
38+
case 'splice': {
39+
return target.slice(args[0], args[1])
40+
}
41+
case 'reverse': {
42+
return null
43+
}
44+
case 'sort': {
45+
return null
46+
}
47+
case 'copyWithin': {
48+
return null
49+
}
50+
}
51+
}
52+
2453
export class Proxifier {
2554
CACHED_PROXY = Symbol('CACHED_PROXY')
2655
constructor(private tree: TTree) {}
@@ -145,6 +174,7 @@ export class Proxifier {
145174
path: path,
146175
args: args,
147176
hasChangedValue: true,
177+
revertValue: getArrayRevertValue(method, target, args),
148178
})
149179

150180
if (process.env.NODE_ENV === 'production') {
@@ -277,6 +307,7 @@ export class Proxifier {
277307
path: nestedPath,
278308
args: [value],
279309
hasChangedValue: value !== target[prop],
310+
revertValue: target[prop],
280311
},
281312
objectChangePath
282313
)

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ describe('TrackStateAccessTree', () => {
6060
path: 'foo',
6161
args: ['bar2'],
6262
hasChangedValue: true,
63+
revertValue: 'bar',
6364
})
6465
})
6566

@@ -203,6 +204,7 @@ describe('OBJECTS', () => {
203204
path: 'foo',
204205
args: ['bar'],
205206
hasChangedValue: false,
207+
revertValue: 'bar',
206208
},
207209
])
208210
expect(renderCount).toBe(0)
@@ -220,6 +222,7 @@ describe('OBJECTS', () => {
220222
path: 'foo',
221223
args: ['bar2'],
222224
hasChangedValue: true,
225+
revertValue: 'bar',
223226
},
224227
])
225228
expect(mutationTree.state.foo).toBe('bar2')
@@ -420,6 +423,7 @@ describe('ARRAYS', () => {
420423
path: 'foo',
421424
args: ['bar'],
422425
hasChangedValue: true,
426+
revertValue: null,
423427
},
424428
])
425429

@@ -439,13 +443,14 @@ describe('ARRAYS', () => {
439443
path: 'foo',
440444
args: [],
441445
hasChangedValue: true,
446+
revertValue: 'foo',
442447
},
443448
])
444449

445450
expect(state.foo.length).toBe(0)
446451
})
447452

448-
test('should track POP mutations', () => {
453+
test('should track SHIFT mutations', () => {
449454
const state = {
450455
foo: ['foo'],
451456
}
@@ -459,6 +464,7 @@ describe('ARRAYS', () => {
459464
path: 'foo',
460465
args: [],
461466
hasChangedValue: true,
467+
revertValue: 'foo',
462468
},
463469
])
464470

@@ -478,6 +484,7 @@ describe('ARRAYS', () => {
478484
path: 'foo',
479485
args: ['foo'],
480486
hasChangedValue: true,
487+
revertValue: null,
481488
},
482489
])
483490

@@ -497,6 +504,7 @@ describe('ARRAYS', () => {
497504
path: 'foo',
498505
args: [0, 1, 'bar'],
499506
hasChangedValue: true,
507+
revertValue: ['foo'],
500508
},
501509
])
502510

@@ -639,6 +647,7 @@ describe('REACTIONS', () => {
639647
path: 'foo',
640648
args: ['bar2'],
641649
hasChangedValue: true,
650+
revertValue: 'bar',
642651
})
643652
})
644653

@@ -745,12 +754,14 @@ describe('REACTIONS', () => {
745754
method: 'set',
746755
args: ['bar2'],
747756
hasChangedValue: true,
757+
revertValue: 'bar',
748758
},
749759
{
750760
path: 'foo',
751761
method: 'set',
752762
args: ['bar3'],
753763
hasChangedValue: true,
764+
revertValue: 'bar2',
754765
},
755766
],
756767
})

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
path: string
99
args: any[]
1010
hasChangedValue: boolean
11+
revertValue?: any
1112
}
1213

1314
export interface IMutationCallback {

0 commit comments

Comments
 (0)