Skip to content

Commit 0a5fae6

Browse files
feat(overmind-devtools): add re_init to update devtools with state on HMR
1 parent 8af1ed2 commit 0a5fae6

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

packages/node_modules/overmind-devtools/src/overmind/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const handleClientMessage: Operator<Message, any> = pipe(
4848
forkEachMessage({
4949
[AppMessageType.PORT_EXISTS]: setPortExists,
5050
[ExecutionType.INIT]: addState,
51+
[ExecutionType.RE_INIT]: addState,
5152
[ExecutionType.FLUSH]: addFlushAndRunMutations,
5253
[ExecutionType.DERIVED]: updateDerived,
5354
[ExecutionType.MUTATIONS]: addMutations,

packages/node_modules/overmind-devtools/src/overmind/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export type Apps = {
148148

149149
export enum ExecutionType {
150150
INIT = 'init',
151+
RE_INIT = 're_init',
151152
FLUSH = 'flush',
152153
DERIVED = 'derived',
153154
MUTATIONS = 'mutations',

packages/node_modules/overmind/src/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
deepCopy,
3838
MockedEventEmitter,
3939
makeStringifySafeMutations,
40+
mergeState,
4041
} from './utils'
4142

4243
export * from './types'
@@ -169,6 +170,7 @@ export class Overmind<ThisConfig extends IConfiguration>
169170
private nextExecutionId: number = 0
170171
private options: Options
171172
private mode: DefaultMode | TestMode | SSRMode
173+
private originalConfiguration
172174
initialized: Promise<any>
173175
eventHub: EventEmitter<Events>
174176
devtools: Devtools
@@ -206,6 +208,7 @@ export class Overmind<ThisConfig extends IConfiguration>
206208
*/
207209
const proxyStateTree = this.createProxyStateTree(configuration, eventHub)
208210

211+
this.originalConfiguration = configuration
209212
this.state = proxyStateTree.state
210213
this.effects = configuration.effects || {}
211214
this.proxyStateTree = proxyStateTree
@@ -731,15 +734,32 @@ export class Overmind<ThisConfig extends IConfiguration>
731734
})
732735
}
733736
reconfigure(configuration: IConfiguration) {
737+
const mergedConfiguration = {
738+
...configuration,
739+
state: mergeState(
740+
this.originalConfiguration.state,
741+
this.state,
742+
configuration.state
743+
),
744+
}
734745
const proxyStateTree = this.proxyStateTree as any
735-
this.proxyStateTree.sourceState = this.getState(configuration)
746+
this.proxyStateTree.sourceState = this.getState(mergedConfiguration)
736747
proxyStateTree.createTrackStateProxifier()
737748
this.state = this.proxyStateTree.state as any
738-
this.actions = this.getActions(configuration)
739-
this.effects = configuration.effects || {}
749+
this.actions = this.getActions(mergedConfiguration)
750+
this.effects = mergedConfiguration.effects || {}
740751

741752
this.proxyStateTree.forceFlush()
742753

754+
if (this.devtools) {
755+
this.devtools.send({
756+
type: 're_init',
757+
data: {
758+
state: proxyStateTree.sourceState,
759+
},
760+
})
761+
}
762+
743763
return this
744764
}
745765
}

packages/node_modules/overmind/src/utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,37 @@ export function deepCopy(obj) {
3030

3131
return obj
3232
}
33+
34+
export function mergeState(originState, oldState, nextState) {
35+
function merge(origin, old, next) {
36+
if (isPlainObject(old) && isPlainObject(next)) {
37+
const newBranch = {}
38+
39+
for (let key in next) {
40+
newBranch[key] = merge(origin[key], old[key], next[key])
41+
}
42+
43+
return newBranch
44+
}
45+
46+
if (typeof next === 'function') {
47+
return next
48+
}
49+
50+
// We return the existing array, as arrays are typically
51+
// mutated, not set with new values as initial state
52+
if (Array.isArray(old) && Array.isArray(next)) {
53+
return old
54+
}
55+
56+
// If we have changed a state from origin, keep that
57+
// changed state
58+
if (next === origin && old !== origin) {
59+
return old
60+
}
61+
62+
return next
63+
}
64+
65+
return merge(originState, oldState, nextState)
66+
}

0 commit comments

Comments
 (0)