Skip to content

Commit 34f127f

Browse files
fix(overmind): fix disposing of derived during hot reloading
1 parent 211c923 commit 34f127f

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

packages/node_modules/overmind/src/derived.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ export class Derived {
1616
private value: any
1717
private paths: Set<string>
1818
private updateCount: number = 0
19+
private disposeOnMutation: () => {}
1920
constructor(private cb: (state: object, parent: object) => void) {
20-
return this.evaluate.bind(this)
21+
const boundEvaluate: any = this.evaluate.bind(this)
22+
23+
boundEvaluate.dispose = () => {
24+
this.disposeOnMutation()
25+
this.trackStateTree.dispose()
26+
}
27+
28+
return boundEvaluate
2129
}
2230
evaluate(
2331
eventHub: EventEmitter<Events>,
@@ -35,23 +43,25 @@ export class Derived {
3543
)
3644

3745
this.scope = () => this.cb(parent, this.trackStateTree.state)
38-
proxyStateTree.onMutation((_, paths, flushId) => {
39-
if (this.isDirty) {
40-
return
41-
}
42-
43-
for (let mutationPath of paths) {
44-
if (this.paths.has(mutationPath)) {
45-
this.isDirty = true
46-
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
47-
derivedPath: path,
48-
path: mutationPath,
49-
flushId,
50-
})
46+
this.disposeOnMutation = proxyStateTree.onMutation(
47+
(_, paths, flushId) => {
48+
if (this.isDirty) {
5149
return
5250
}
51+
52+
for (let mutationPath of paths) {
53+
if (this.paths.has(mutationPath)) {
54+
this.isDirty = true
55+
eventHub.emitAsync(EventType.DERIVED_DIRTY, {
56+
derivedPath: path,
57+
path: mutationPath,
58+
flushId,
59+
})
60+
return
61+
}
62+
}
5363
}
54-
})
64+
)
5565
}
5666

5767
if (

packages/node_modules/overmind/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export class Overmind<ThisConfig extends IConfiguration>
194194
private nextExecutionId: number = 0
195195
private mode: DefaultMode | TestMode | SSRMode
196196
private originalConfiguration
197+
private derivedReferences: Derived[] = []
197198
initialized: Promise<any>
198199
eventHub: EventEmitter<Events>
199200
devtools: Devtools
@@ -792,6 +793,10 @@ export class Overmind<ThisConfig extends IConfiguration>
792793
aggr[key] = this.processState(value)
793794
} else if (typeof value === 'function') {
794795
aggr[key] = new Derived(value)
796+
797+
if (IS_DEVELOPMENT) {
798+
this.derivedReferences.push(aggr[key])
799+
}
795800
} else {
796801
Object.defineProperty(aggr, key, originalDescriptor as any)
797802
}
@@ -832,6 +837,9 @@ export class Overmind<ThisConfig extends IConfiguration>
832837
return this.proxyStateTree.onFlush(cb)
833838
}
834839
reconfigure(configuration: IConfiguration) {
840+
this.derivedReferences.forEach((derived) => {
841+
derived.dispose()
842+
})
835843
const mergedConfiguration = {
836844
...configuration,
837845
state: mergeState(

0 commit comments

Comments
 (0)