8000 provide warning when trying to add a new module on hot reload (#316) · vuejs-ar/ar.vuejs.org@32212d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32212d9

Browse files
ktsnyyx990803
authored andcommitted
provide warning when trying to add a new module on hot reload (vuejs#316)
1 parent 96084f5 commit 32212d9

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

src/index.js

Lines changed: 25 additions & 15 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,7 @@ class Store {
145145
}
146146

147147
hotUpdate (newOptions) {
148-
const options = this._options
149-
if (newOptions.actions) {
150-
options.actions = newOptions.actions
151-
}
152-
if (newOptions.mutations) {
153-
options.mutations = newOptions.mutations
154-
}
155-
if (newOptions.getters) {
156-
options.getters = newOptions.getters
157-
}
158-
if (newOptions.modules) {
159-
for (const key in newOptions.modules) {
160-
options.modules[key] = newOptions.modules[key]
161-
}
162-
}
148+
updateModule(this._options, newOptions)
163149
resetStore(this)
164150
}
165151

@@ -175,6 +161,30 @@ function assert (condition, msg) {
175161
if (!condition) throw new Error(`[vuex] ${msg}`)
176162
}
177163

164+
function updateModule (targetModule, newModule) {
165+
if (newModule.actions) {
166+
targetModule.actions = newModule.actions
167+
}
168+
if (newModule.mutations) {
169+
targetModule.mutations = newModule.mutations
170+
}
171+
if (newModule.getters) {
172+
targetModule.getters = newModule.getters
173+
}
174+
if (newModule.modules) {
175+
for (const key in newModule.modules) {
176+
if (!(targetModule.modules && targetModule.modules[key])) {
177+
console.warn(
178+
`[vuex] trying to add a new module '${key}' on hot reloading, ` +
179+
'manual reload is needed'
180+
)
181+
return
182+
}
183+
updateModule(targetModule.modules[key], newModule.modules[key])
184+
}
185+
}
186+
}
187+
178188
function resetStore (store) {
179189
store._actions = Object.create(null)
180190
store._mutations = Object.create(null)

test/unit/test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,27 @@ describe('Vuex', () => {
970970
})
971971
})
972972

973+
it('hot reload: provide warning if a new module is given', () => {
974+
const store = new Vuex.Store({})
975+
976+
spyOn(console, 'warn')
977+
978+
store.hotUpdate({
979+
modules: {
980+
test: {
981+
state: {
982+
count: 0
983+
}
984+
}
985+
}
986+
})
987+
988+
expect(console.warn).toHaveBeenCalledWith(
989+
'[vuex] trying to add a new module \'test\' on hot reloading, ' +
990+
'manual reload is needed'
991+
)
992+
})
993+
973994
it('watch: with resetting vm', done => {
974995
const store = new Vuex.Store({
975996
state: {

0 commit comments

Comments
 (0)
0