Skip to content

Commit 7f9d2ab

Browse files
feat(overmind): reconfigure the instance on HMR
1 parent d3feb1f commit 7f9d2ab

File tree

1 file changed

+47
-22
lines changed
  • packages/node_modules/overmind/src

1 file changed

+47
-22
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class Overmind<ThisConfig extends IConfiguration>
186186

187187
if (IS_DEVELOPMENT && mode.mode === MODE_DEFAULT) {
188188
if (hotReloadingCache[name]) {
189-
return hotReloadingCache[name]
189+
return hotReloadingCache[name].reconfigure(configuration)
190190
} else {
191191
hotReloadingCache[name] = this
192192
}
@@ -204,24 +204,7 @@ export class Overmind<ThisConfig extends IConfiguration>
204204
Create the proxy state tree instance with the state and a wrapper to expose
205205
the eventHub
206206
*/
207-
const proxyStateTree = new ProxyStateTree(
208-
this.getState(configuration) as any,
209-
{
210-
devmode: IS_DEVELOPMENT,
211-
dynamicWrapper: (_, path, func) => func(eventHub, proxyStateTree, path),
212-
onGetter: IS_DEVELOPMENT
213-
? (path, value) => {
214-
// We need to let any initial values be set first
215-
setTimeout(() => {
216-
this.eventHub.emit(EventType.GETTER, {
217-
path,
218-
value: safeValue(value),
219-
})
220-
})
221-
}
222-
: undefined,
223-
}
224-
)
207+
const proxyStateTree = this.createProxyStateTree(configuration, eventHub)
225208

226209
this.state = proxyStateTree.state
227210
this.effects = configuration.effects || {}
@@ -262,7 +245,12 @@ export class Overmind<ThisConfig extends IConfiguration>
262245
? 'NoName'
263246
: document.title || 'NoName'
264247

265-
this.initializeDevtools(host, name, eventHub, proxyStateTree)
248+
this.initializeDevtools(
249+
host,
250+
name,
251+
eventHub,
252+
proxyStateTree.sourceState
253+
)
266254
} else {
267255
warning +=
268256
'\n\n - You are not running on localhost. You will have to manually define the devtools option to connect'
@@ -334,6 +322,31 @@ export class Overmind<ThisConfig extends IConfiguration>
334322
this.initialized = Promise.resolve(null)
335323
}
336324
}
325+
private createProxyStateTree(
326+
configuration: IConfiguration,
327+
eventHub: EventEmitter<any> | MockedEventEmitter
328+
) {
329+
const proxyStateTree = new ProxyStateTree(
330+
this.getState(configuration) as any,
331+
{
332+
devmode: IS_DEVELOPMENT,
333+
dynamicWrapper: (_, path, func) => func(eventHub, proxyStateTree, path),
334+
onGetter: IS_DEVELOPMENT
335+
? (path, value) => {
336+
// We need to let any initial values be set first
337+
setTimeout(() => {
338+
this.eventHub.emit(EventType.GETTER, {
339+
path,
340+
value: safeValue(value),
341+
})
342+
})
343+
}
344+
: undefined,
345+
}
346+
)
347+
348+
return proxyStateTree
349+
}
337350
private createExecution(name, action) {
338351
if (IS_PRODUCTION) {
339352
return ({
@@ -584,7 +597,7 @@ export class Overmind<ThisConfig extends IConfiguration>
584597
return result
585598
})
586599
}
587-
private initializeDevtools(host, name, eventHub, proxyStateTree) {
600+
private initializeDevtools(host, name, eventHub, initialState) {
588601
const devtools = new Devtools(name)
589602
devtools.connect(
590603
host,
@@ -605,7 +618,7 @@ export class Overmind<ThisConfig extends IConfiguration>
605618
devtools.send({
606619
type: 'init',
607620
data: {
608-
state: proxyStateTree.sourceState,
621+
state: initialState,
609622
},
610623
})
611624
this.devtools = devtools
@@ -717,6 +730,18 @@ export class Overmind<ThisConfig extends IConfiguration>
717730
}
718731
})
719732
}
733+
reconfigure(configuration: IConfiguration) {
734+
const proxyStateTree = this.proxyStateTree as any
735+
this.proxyStateTree.sourceState = this.getState(configuration)
736+
proxyStateTree.createTrackStateProxifier()
737+
this.state = this.proxyStateTree.state as any
738+
this.actions = this.getActions(configuration)
739+
this.effects = configuration.effects || {}
740+
741+
this.proxyStateTree.forceFlush()
742+
743+
return this
744+
}
720745
}
721746

722747
/*

0 commit comments

Comments
 (0)