Skip to content

Commit 94a320e

Browse files
fix(proxy-state-tree): evaluate functions when setting them
1 parent a9c2137 commit 94a320e

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ describe('Derived', () => {
136136
app.actions.addDerived()
137137
expect(app.state.upperFoo).toBe('BAR')
138138
app.actions.changeFoo()
139+
139140
expect(app.state.upperFoo).toBe('BAR2')
140141
})
141142

packages/node_modules/overmind/src/derived.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export const IS_DERIVED = Symbol('IS_DERIVED')
1313

1414
export class Derived {
1515
private isDirty: boolean = true
16-
private trackStateTree: ITrackStateTree<any>
1716
private previousProxifier: any
1817
private value: any
1918
private paths: Set<string>
@@ -46,7 +45,6 @@ export class Derived {
4645
if (!this.disposeOnMutation) {
4746
this.disposeOnMutation = proxyStateTree.onMutation(
4847
(_, paths, flushId) => {
49-
// It has been removed from the tree
5048
if (typeof path.reduce((aggr, key) => aggr && aggr[key], proxyStateTree.sourceState) !== 'function') {
5149
this.disposeOnMutation()
5250
return

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

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -341,32 +341,33 @@ export class Proxifier {
341341
const mutationTree = proxifier.getMutationTree()
342342
const existingValue = target[prop]
343343

344+
// Adding functions is not considered mutations
344345
if (typeof value === 'function' && proxifier.tree.master.options.onFunction) {
345346
const result = proxifier.tree.master.options.onFunction(
346-
proxifier.tree,
347+
proxifier.getTrackingTree() || proxifier.tree,
347348
nestedPath,
348349
value
349350
)
350351

351352
value = result.func
352-
}
353-
354-
mutationTree.addMutation(
355-
{
356-
method: 'set',
357-
path: nestedPath,
358-
args: [value],
359-
hasChangedValue: value !== target[prop],
360-
revert: () => {
361-
if (existingValue === undefined) {
362-
delete proxy[prop]
363-
} else {
364-
proxy[prop] = existingValue
365-
}
353+
} else {
354+
mutationTree.addMutation(
355+
{
356+
method: 'set',
357+
path: nestedPath,
358+
args: [value],
359+
hasChangedValue: value !== target[prop],
360+
revert: () => {
361+
if (existingValue === undefined) {
362+
delete proxy[prop]
363+
} else {
364+
proxy[prop] = existingValue
365+
}
366+
},
366367
},
367-
},
368-
objectChangePath
369-
)
368+
objectChangePath
369+
)
370+
}
370371

371372
return Reflect.set(target, prop, value)
372373
},

0 commit comments

Comments
 (0)