Skip to content

Commit

Permalink
fix(vuex): dynamic modules with preserve state, closes #923
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Chau committed Mar 26, 2019
1 parent 7bf9bad commit 676e5e5
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions src/backend/vuex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
@afwn90cj93201nixr2e1re

afwn90cj93201nixr2e1re Sep 1, 2019

Seems like this should be state = typeof module.state === 'function' ? get(store.state, path) : module.state.

This comment has been minimized.

Copy link
@Akryum

Akryum Sep 1, 2019

Member

Why?

This comment has been minimized.

Copy link
@afwn90cj93201nixr2e1re

This comment has been minimized.

Copy link
@afwn90cj93201nixr2e1re

afwn90cj93201nixr2e1re Sep 1, 2019

As @LinusBorg said, preserveState only for ssr, but im not using ssr as is, so, also if im gonna use preserveState, then the vuex gonna skip state initializing again, but i need to initialize it.

This comment has been minimized.

Copy link
@afwn90cj93201nixr2e1re

afwn90cj93201nixr2e1re Sep 1, 2019

i posted fix too.
@Akryum ping.

}

const key = path.join('/')
registeredModules[key] = allTimeModules[key] = {
path,
module,
options
options: {
...options,
preserveState: false
},
state: stringify(state)
}

if (SharedData.recordVuex) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 676e5e5

Please sign in to comment.