Skip to content

Commit 1c75e96

Browse files
Guillaume Chauiksim
Guillaume Chau
authored and
iksim
committed
fix(vuex): vuex 1.x compat, closes vuejs#916
1 parent 90ef6fb commit 1c75e96

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

src/backend/hook.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,25 +103,29 @@ export function installHook (target) {
103103
getters: store.getters
104104
}
105105
// Dynamic modules
106-
hook.storeModules = []
107-
const origRegister = store.registerModule.bind(store)
108-
store.registerModule = (path, module, options) => {
109-
if (typeof path === 'string') path = [path]
110-
hook.storeModules.push({ path, module, options })
111-
origRegister(path, module, options)
112-
}
113-
const origUnregister = store.unregisterModule.bind(store)
114-
store.unregisterModule = (path) => {
115-
if (typeof path === 'string') path = [path]
116-
const key = path.join('/')
117-
const index = hook.storeModules.findIndex(m => m.path.join('/') === key)
118-
if (index !== -1) hook.storeModules.splice(0, 1)
119-
origUnregister(path)
120-
}
121-
hook.flushStoreModules = () => {
122-
store.registerModule = origRegister
123-
store.unregisterModule = origUnregister
124-
return hook.storeModules
106+
if (store.registerModule) {
107+
hook.storeModules = []
108+
const origRegister = store.registerModule.bind(store)
109+
store.registerModule = (path, module, options) => {
110+
if (typeof path === 'string') path = [path]
111+
hook.storeModules.push({ path, module, options })
112+
origRegister(path, module, options)
113+
}
114+
const origUnregister = store.unregisterModule.bind(store)
115+
store.unregisterModule = (path) => {
116+
if (typeof path === 'string') path = [path]
117+
const key = path.join('/')
118+
const index = hook.storeModules.findIndex(m => m.path.join('/') === key)
119+
if (index !== -1) hook.storeModules.splice(0, 1)
120+
origUnregister(path)
121+
}
122+
hook.flushStoreModules = () => {
123+
store.registerModule = origRegister
124+
store.unregisterModule = origUnregister
125+
return hook.storeModules
126+
}
127+
} else {
128+
hook.flushStoreModules = () => []
125129
}
126130
})
127131

src/backend/vuex.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,33 @@ export function initVuexBackend (hook, bridge, isLegacy) {
7070
}
7171
}
7272

73-
const origRegisterModule = store.registerModule.bind(store)
74-
store.registerModule = (path, module, options) => {
75-
addModule(path, module, options)
76-
origRegisterModule(path, module, options)
77-
}
73+
let origRegisterModule, origUnregisterModule
7874

79-
const origUnregisterModule = store.unregisterModule.bind(store)
80-
store.unregisterModule = (path) => {
81-
if (typeof path === 'string') path = [path]
75+
if (store.registerModule) {
76+
origRegisterModule = store.registerModule.bind(store)
77+
store.registerModule = (path, module, options) => {
78+
addModule(path, module, options)
79+
origRegisterModule(path, module, options)
80+
}
8281

83-
delete registeredModules[path.join('/')]
82+
origUnregisterModule = store.unregisterModule.bind(store)
83+
store.unregisterModule = (path) => {
84+
if (typeof path === 'string') path = [path]
8485

85-
if (SharedData.recordVuex) {
86-
addMutation(`Unregister module: ${path.join('/')}`, {
87-
path
88-
}, {
89-
unregisterModule: true
90-
})
91-
}
86+
delete registeredModules[path.join('/')]
9287

93-
origUnregisterModule(path)
88+
if (SharedData.recordVuex) {
89+
addMutation(`Unregister module: ${path.join('/')}`, {
90+
path
91+
}, {
92+
unregisterModule: true
93+
})
94+
}
95+
96+
origUnregisterModule(path)
97+
}
98+
} else {
99+
origRegisterModule = origUnregisterModule = () => {}
94100
}
95101

96102
bridge.send('vuex:init')

0 commit comments

Comments
 (0)