Skip to content

Commit 271c428

Browse files
fix(proxy-state-tree): mutations now trigger after they are performed
BREAKING CHANGE: mutation events properly trigger after the mutation is performed
1 parent 1a9452d commit 271c428

File tree

5 files changed

+40
-36
lines changed

5 files changed

+40
-36
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ lib
1212
.cache
1313
.DS_Store
1414
devtoolsDist
15-
packages/overmind-website
15+
packages/overmind-website/**/*
1616

1717
# Runtime data
1818
pids

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

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

packages/node_modules/overmind/src/derived.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ export class Derived {
4545
if (!this.disposeOnMutation) {
4646
this.disposeOnMutation = proxyStateTree.onMutation(
4747
(_, paths, flushId) => {
48-
if (this.isDirty) {
49-
return
50-
}
51-
52-
// We only check this when it is not dirty, as being dirty indicates
53-
// that it was just added
5448
if (typeof path.reduce((aggr, key) => aggr && aggr[key], proxyStateTree.sourceState) !== 'function') {
5549
this.disposeOnMutation()
5650
return
5751
}
52+
53+
if (this.isDirty) {
54+
return
55+
}
5856

5957
for (let mutationPath of paths) {
6058
if (this.paths.has(mutationPath)) {

packages/node_modules/overmind/src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -809,19 +809,17 @@ export class Overmind<ThisConfig extends IConfiguration>
809809
if (eventType === EventType.MUTATIONS) {
810810
// We want to trigger property access when setting objects and arrays, as any derived set would
811811
// then trigger and update the devtools
812-
setTimeout(() => {
813-
data.mutations.forEach((mutation) => {
814-
const value = mutation.path.split('.').reduce((aggr, key) => aggr[key], this.proxyStateTree.state)
815-
if (isPlainObject(value)) {
816-
Object.keys(value).forEach((key) => value[key])
817-
} else if (Array.isArray(value)) {
818-
value.forEach((item) => {
819-
if (isPlainObject(item)) {
820-
Object.keys(item).forEach((key) => item[key])
821-
}
822-
})
823-
}
824-
})
812+
data.mutations.forEach((mutation) => {
813+
const value = mutation.path.split('.').reduce((aggr, key) => aggr[key], this.proxyStateTree.state)
814+
if (isPlainObject(value)) {
815+
Object.keys(value).forEach((key) => value[key])
816+
} else if (Array.isArray(value)) {
817+
value.forEach((item) => {
818+
if (isPlainObject(item)) {
819+
Object.keys(item).forEach((key) => item[key])
820+
}
821+
})
822+
}
825823
})
826824
}
827825

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

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,25 +193,30 @@ export class Proxifier {
193193
return (...args) => {
194194
const mutationTree = proxifier.getMutationTree()
195195

196-
mutationTree.addMutation({
197-
method,
198-
path: path,
199-
args: args,
200-
hasChangedValue: true,
201-
revert: getArrayMutationRevert(method, proxy, args),
202-
})
196+
let result
197+
203198

204199
if (process.env.NODE_ENV === 'production') {
205-
return target[prop](...args)
200+
result = target[prop](...args)
206201
} else {
207-
return target[prop](
202+
result = target[prop](
208203
...args.map((arg) =>
209204
/* @__PURE__ */ proxifier.ensureValueDosntExistInStateTreeElsewhere(
210205
arg
211206
)
212207
)
213208
)
214209
}
210+
211+
mutationTree.addMutation({
212+
method,
213+
path: path,
214+
args: args,
215+
hasChangedValue: true,
216+
revert: getArrayMutationRevert(method, proxy, args),
217+
})
218+
219+
return result
215220
}
216221
}
217222

@@ -231,6 +236,7 @@ export class Proxifier {
231236

232237
const mutationTree = proxifier.getMutationTree()
233238
const existingValue = target[prop]
239+
const result = Reflect.set(target, prop, value)
234240

235241
mutationTree.addMutation({
236242
method: 'set',
@@ -246,7 +252,7 @@ export class Proxifier {
246252
},
247253
})
248254

249-
return Reflect.set(target, prop, value)
255+
return result
250256
},
251257
})
252258

@@ -341,7 +347,6 @@ export class Proxifier {
341347
const mutationTree = proxifier.getMutationTree()
342348
const existingValue = target[prop]
343349

344-
// Adding functions is not considered mutations
345350
if (typeof value === 'function' && proxifier.tree.master.options.onFunction) {
346351
const result = proxifier.tree.master.options.onFunction(
347352
proxifier.getTrackingTree() || proxifier.tree,
@@ -352,12 +357,15 @@ export class Proxifier {
352357
value = result.func
353358
}
354359

360+
const hasChangedValue = value !== target[prop]
361+
const result = Reflect.set(target, prop, value)
362+
355363
mutationTree.addMutation(
356364
{
357365
method: 'set',
358366
path: nestedPath,
359367
args: [value],
360-
hasChangedValue: value !== target[prop],
368+
hasChangedValue,
361369
revert: () => {
362370
if (existingValue === undefined) {
363371
delete proxy[prop]
@@ -369,7 +377,7 @@ export class Proxifier {
369377
objectChangePath
370378
)
371379

372-
return Reflect.set(target, prop, value)
380+
return result
373381
},
374382
deleteProperty(target, prop) {
375383
const nestedPath = proxifier.concat(path, prop)
@@ -384,6 +392,9 @@ export class Proxifier {
384392
const mutationTree = proxifier.getMutationTree()
385393
const existingValue = target[prop]
386394

395+
396+
delete target[prop]
397+
387398
mutationTree.addMutation(
388399
{
389400
method: 'unset',
@@ -397,8 +408,6 @@ export class Proxifier {
397408
objectChangePath
398409
)
399410

400-
delete target[prop]
401-
402411
return true
403412
},
404413
})

0 commit comments

Comments
 (0)