Skip to content

Commit 87c7a02

Browse files
author
Guillaume Chau
committed
fix(vuex): initialState only on vuex init, closes #950
1 parent c7988db commit 87c7a02

File tree

2 files changed

+18
-40
lines changed

2 files changed

+18
-40
lines changed

src/backend/hook.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,18 @@ export function installHook (target) {
9898

9999
hook.once('vuex:init', store => {
100100
hook.store = store
101-
hook.initialStore = {
102-
state: clone(store.state),
103-
getters: store.getters
104-
}
105-
const origReplaceState = store.replaceState.bind(store)
106-
store.replaceState = state => {
107-
hook.initialStore.state = clone(state)
108-
origReplaceState(state)
109-
}
110101
// Dynamic modules
102+
let origRegister, origUnregister
111103
if (store.registerModule) {
112104
hook.storeModules = []
113-
const origRegister = store.registerModule.bind(store)
105+
origRegister = store.registerModule.bind(store)
114106
store.registerModule = (path, module, options) => {
115107
if (typeof path === 'string') path = [path]
116108
hook.storeModules.push({ path, module, options })
117109
origRegister(path, module, options)
118-
if (process.env.NODE_ENV !== 'production') console.log('early register module', path)
110+
if (process.env.NODE_ENV !== 'production') console.log('early register module', path, module, options)
119111
}
120-
const origUnregister = store.unregisterModule.bind(store)
112+
origUnregister = store.unregisterModule.bind(store)
121113
store.unregisterModule = (path) => {
122114
if (typeof path === 'string') path = [path]
123115
const key = path.join('/')
@@ -126,17 +118,13 @@ export function installHook (target) {
126118
origUnregister(path)
127119
if (process.env.NODE_ENV !== 'production') console.log('early unregister module', path)
128120
}
129-
hook.flushStoreModules = () => {
121+
}
122+
hook.flushStoreModules = () => {
123+
if (store.registerModule) {
130124
store.registerModule = origRegister
131125
store.unregisterModule = origUnregister
132-
store.replaceState = origReplaceState
133-
return hook.storeModules
134-
}
135-
} else {
136-
hook.flushStoreModules = () => {
137-
store.replaceState = origReplaceState
138-
return []
139126
}
127+
return hook.storeModules
140128
}
141129
})
142130

src/backend/vuex.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const isProd = process.env.NODE_ENV === 'production'
99
export function initVuexBackend (hook, bridge, isLegacy) {
1010
const store = hook.store
1111

12+
const earlyModules = hook.flushStoreModules()
13+
let initialState = clone(store.state)
14+
1215
let snapshotsVm = null
1316
const updateSnapshotsVm = (state) => {
1417
snapshotsVm = new Vue({
@@ -19,27 +22,14 @@ export function initVuexBackend (hook, bridge, isLegacy) {
1922
})
2023
}
2124

22-
const getStateSnapshot = (_store = store) => clone(_store.state)
23-
2425
let baseStateSnapshot, stateSnapshots, mutations, lastState
2526
let registeredModules = {}
2627
let allTimeModules = {}
2728

28-
const earlyModules = hook.flushStoreModules()
29-
30-
// Init additional state
31-
earlyModules.forEach(({ path, module, options }) => {
32-
if (!options || options.preserveState !== true) {
33-
const state = typeof module.state === 'function' ? module.state() : module.state
34-
const parentState = path.length === 1 ? hook.initialStore.state : get(hook.initialStore.state, path.slice(0, -1))
35-
set(parentState, path[path.length - 1], state)
36-
}
37-
})
38-
3929
updateSnapshotsVm()
4030

4131
function reset (stateSnapshot = null) {
42-
baseStateSnapshot = stateSnapshot || getStateSnapshot(hook.initialStore)
32+
baseStateSnapshot = stateSnapshot || clone(initialState)
4333
mutations = []
4434
resetSnapshotCache()
4535
}
@@ -199,7 +189,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
199189

200190
bridge.on('vuex:import-state', state => {
201191
const parsed = parse(state, true)
202-
hook.initialStore.state = parsed
192+
initialState = parsed
203193
reset()
204194
hook.emit('vuex:travel-to-state', parsed)
205195
bridge.send('vuex:init')
@@ -281,7 +271,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
281271
tempAddedModules.push(key)
282272
origRegisterModule(moduleInfo.path, {
283273
...moduleInfo.module,
284-
state: moduleInfo.state
274+
state: clone(moduleInfo.state)
285275
}, moduleInfo.options)
286276
updateSnapshotsVm(store.state)
287277
if (!isProd) console.log('replay register module', moduleInfo)
@@ -319,7 +309,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
319309
}
320310

321311
// Send final state after replay
322-
resultState = getStateSnapshot()
312+
resultState = clone(store.state)
323313
}
324314

325315
lastState = resultState
@@ -338,7 +328,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
338328
const { path, module, options, state } = registeredModules[m]
339329
origRegisterModule(path, {
340330
...module,
341-
state
331+
state: clone(state)
342332
}, options)
343333
if (!isProd) console.log('after replay register', m)
344334
})
@@ -364,7 +354,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
364354
function takeStateSnapshot (index) {
365355
stateSnapshots.push({
366356
index,
367-
state: getStateSnapshot()
357+
state: clone(store.state)
368358
})
369359
// Delete old cached snapshots
370360
if (stateSnapshots.length > SharedData.cacheVuexSnapshotsLimit) {
@@ -402,7 +392,7 @@ export function initVuexBackend (hook, bridge, isLegacy) {
402392
const { path, module, options, state } = data
403393
origRegisterModule(path, {
404394
...module,
405-
state
395+
state: clone(state)
406396
}, options)
407397
registeredModules[path.join('/')] = data
408398
}

0 commit comments

Comments
 (0)