Skip to content

Commit d8efe07

Browse files
fix(overmind): reset proxy state tree on hot reload
1 parent af30335 commit d8efe07

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
operatorStopped,
3737
} from './operator'
3838
import { proxifyEffects } from './proxyfyEffects'
39+
import { rehydrate } from './rehydrate'
3940
import {
4041
IAction,
4142
IConfiguration,
@@ -61,7 +62,7 @@ import {
6162
isPromise,
6263
processState,
6364
} from './utils'
64-
import { rehydrate } from './rehydrate'
65+
6566
export * from './types'
6667

6768
export { createOperator, createMutationOperator, ResolveState, ResolveActions }
@@ -944,9 +945,7 @@ export class Overmind<ThisConfig extends IConfiguration>
944945
return this.proxyStateTree.onFlush(cb)
945946
}
946947
reconfigure(configuration: IConfiguration) {
947-
const proxyStateTree = this.proxyStateTree as any
948-
this.proxyStateTree.sourceState = this.getState(configuration)
949-
proxyStateTree.createTrackStateProxifier()
948+
this.proxyStateTree.reset(configuration.state || {})
950949
this.state = this.proxyStateTree.state as any
951950
this.actions = this.getActions(configuration.actions)
952951
this.effects = configuration.effects || {}
@@ -968,7 +967,7 @@ export class Overmind<ThisConfig extends IConfiguration>
968967
this.devtools.send({
969968
type: 're_init',
970969
data: {
971-
state: proxyStateTree.state,
970+
state: this.state,
972971
actions: getActionPaths(configuration.actions),
973972
},
974973
})

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99

1010
export class MutationTree<T extends object> implements IMutationTree<T> {
1111
private mutationCallbacks: IMutationCallback[] = []
12+
private disposeOnReset: Function
1213
master: IProxyStateTree<T>
1314
state: T
1415
proxifier: IProxifier<T>
@@ -22,6 +23,9 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
2223
this.master = master
2324
this.proxifier = proxifier || new Proxifier(this)
2425
this.state = this.proxifier.proxify(master.sourceState, '')
26+
this.disposeOnReset = master.onReset(() => {
27+
this.state = this.proxifier.proxify(master.sourceState, '')
28+
})
2529
}
2630
trackPaths() {
2731
const paths = new Set<string>()
@@ -101,6 +105,7 @@ export class MutationTree<T extends object> implements IMutationTree<T> {
101105
this.isBlocking = false
102106
}
103107
dispose() {
108+
this.disposeOnReset()
104109
this.isTracking = false
105110
this.mutationCallbacks.length = 0
106111
this.proxifier = this.master.proxifier

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {
2+
IProxifier,
23
IProxyStateTree,
3-
ITrackStateTree,
44
ITrackCallback,
5-
IProxifier,
65
ITrackScopedCallback,
6+
ITrackStateTree,
77
} from './types'
88

99
export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
10+
private disposeOnReset: Function
1011
master: IProxyStateTree<T>
1112
pathDependencies: Set<string> = new Set()
1213
callback: ITrackCallback
@@ -18,6 +19,9 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
1819
this.master = master
1920
this.proxifier = master.proxifier
2021
this.state = master.state
22+
this.disposeOnReset = this.master.onReset(() => {
23+
this.state = master.state
24+
})
2125
}
2226
trackPaths() {
2327
const paths = new Set<string>()
@@ -93,6 +97,7 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
9397
return result
9498
}
9599
dispose() {
100+
this.disposeOnReset()
96101
if (!this.callback) {
97102
this.pathDependencies.clear()
98103

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
3636
mutationTree: [] as IMutationTree<T>[],
3737
trackStateTree: [] as ITrackStateTree<T>[],
3838
}
39+
private onResetCallbacks: Function[] = []
3940
flushCallbacks: IFlushCallback[] = []
4041
mutationCallbacks: IMutationCallback[] = []
4142
currentFlushId: number = 0
@@ -85,6 +86,13 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
8586
''
8687
)
8788
}
89+
onReset(cb: Function) {
90+
this.onResetCallbacks.push(cb)
91+
92+
return () => {
93+
this.onResetCallbacks.splice(this.onResetCallbacks.indexOf(cb), 1)
94+
}
95+
}
8896
getMutationTree(): IMutationTree<T> {
8997
if (!this.options.devmode) {
9098
return (this.mutationTree =
@@ -241,6 +249,11 @@ export class ProxyStateTree<T extends object> implements IProxyStateTree<T> {
241249
delete this.pathDependencies[path]
242250
}
243251
}
252+
reset(state: T) {
253+
this.sourceState = state
254+
this.createTrackStateProxifier()
255+
this.onResetCallbacks.forEach((cb) => cb())
256+
}
244257
toJSON() {
245258
return this.sourceState
246259
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface IRemoveProxyCallback {
9494
}
9595

9696
export interface IProxyStateTree<T extends object> {
97+
onReset(cb: Function): () => void
9798
addPathDependency(path: string, callback: ITrackCallback): void
9899
removePathDependency(path: string, callback: ITrackCallback): void
99100
getTrackStateTree(): ITrackStateTree<T>

0 commit comments

Comments
 (0)