Skip to content

Commit 1a9452d

Browse files
fix(overmind): access properties on mutation to trigger derived in devtools
1 parent 94a320e commit 1a9452d

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed
File renamed without changes.

packages/node_modules/overmind/src/derived.ts

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

packages/node_modules/overmind/src/index.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ export class Overmind<ThisConfig extends IConfiguration>
384384
func: derived,
385385
value: derived(eventHub, tree, proxyStateTree, path.split('.'))
386386
}
387-
},
388-
387+
},
389388
onGetter: devmode
390389
? (path, value) => {
391390
this.eventHub.emitAsync(EventType.GETTER, {
@@ -807,6 +806,25 @@ export class Overmind<ThisConfig extends IConfiguration>
807806
data,
808807
})
809808

809+
if (eventType === EventType.MUTATIONS) {
810+
// We want to trigger property access when setting objects and arrays, as any derived set would
811+
// 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+
})
825+
})
826+
}
827+
810828
// Access the derived async, which will trigger calculation and devtools
811829
if (eventType === EventType.DERIVED_DIRTY) {
812830
data.derivedPath

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -350,25 +350,25 @@ export class Proxifier {
350350
)
351351

352352
value = result.func
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-
},
367-
},
368-
objectChangePath
369-
)
370353
}
371354

355+
mutationTree.addMutation(
356+
{
357+
method: 'set',
358+
path: nestedPath,
359+
args: [value],
360+
hasChangedValue: value !== target[prop],
361+
revert: () => {
362+
if (existingValue === undefined) {
363+
delete proxy[prop]
364+
} else {
365+
proxy[prop] = existingValue
366+
}
367+
},
368+
},
369+
objectChangePath
370+
)
371+
372372
return Reflect.set(target, prop, value)
373373
},
374374
deleteProperty(target, prop) {

0 commit comments

Comments
 (0)