-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(vuex): dynamic modules with preserve state, closes #923
- Loading branch information
Guillaume Chau
committed
Mar 26, 2019
1 parent
7bf9bad
commit 676e5e5
Showing
1 changed file
with
30 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,11 +52,23 @@ export function initVuexBackend (hook, bridge, isLegacy) { | |
function addModule (path, module, options) { | ||
if (typeof path === 'string') path = [path] | ||
|
||
let state | ||
if (options && options.preserveState) { | ||
state = get(store.state, path) | ||
} | ||
if (!state) { | ||
state = typeof module.state === 'function' ? module.state() : module.state | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
afwn90cj93201nixr2e1re
|
||
} | ||
|
||
const key = path.join('/') | ||
registeredModules[key] = allTimeModules[key] = { | ||
path, | ||
module, | ||
options | ||
options: { | ||
...options, | ||
preserveState: false | ||
}, | ||
state: stringify(state) | ||
} | ||
|
||
if (SharedData.recordVuex) { | ||
|
@@ -250,9 +262,13 @@ export function initVuexBackend (hook, bridge, isLegacy) { | |
for (let i = stateSnapshot.index + 1; i <= index; i++) { | ||
const mutation = mutations[i] | ||
if (mutation.registerModule) { | ||
const { path, module, options } = mutation.payload | ||
tempAddedModules.push(path.join('/')) | ||
origRegisterModule(path, module, options) | ||
const key = mutation.payload.path.join('/') | ||
const registeredModule = registeredModules[key] | ||
tempAddedModules.push(key) | ||
origRegisterModule(registeredModule.path, { | ||
...registeredModule.module, | ||
state: parse(registeredModule.state, true) | ||
}, registeredModule.options) | ||
updateSnapshotsVm(store.state) | ||
} else if (mutation.unregisterModule && get(store.state, mutation.payload.path) != null) { | ||
const path = mutation.payload.path | ||
|
@@ -296,8 +312,11 @@ export function initVuexBackend (hook, bridge, isLegacy) { | |
origUnregisterModule(m.split('/')) | ||
}) | ||
tempRemovedModules.sort((a, b) => a.length - b.length).forEach(m => { | ||
const { path, module, options } = registeredModules[m] | ||
origRegisterModule(path, module, options) | ||
const { path, module, options, state } = registeredModules[m] | ||
origRegisterModule(path, { | ||
...module, | ||
state: parse(state, true) | ||
}, options) | ||
}) | ||
store._vm = originalVm | ||
|
||
|
@@ -356,8 +375,11 @@ export function initVuexBackend (hook, bridge, isLegacy) { | |
if (!Object.keys(registeredModules).sort((a, b) => a.length - b.length).includes(m)) { | ||
const data = allTimeModules[m] | ||
if (data) { | ||
const { path, module, options } = data | ||
origRegisterModule(path, module, options) | ||
const { path, module, options, state } = data | ||
origRegisterModule(path, { | ||
...module, | ||
state: parse(state, true) | ||
}, options) | ||
registeredModules[path.join('/')] = data | ||
} | ||
} | ||
|
Seems like this should be
state = typeof module.state === 'function' ? get(store.state, path) : module.state
.