Skip to content

Commit

Permalink
provide warning when trying to add a new module on hot reload (vuejs#316
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ktsn authored and yyx990803 committed Sep 16, 2016
1 parent 96084f5 commit 32212d9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,21 +145,7 @@ class Store {
}

hotUpdate (newOptions) {
const options = this._options
if (newOptions.actions) {
options.actions = newOptions.actions
}
if (newOptions.mutations) {
options.mutations = newOptions.mutations
}
if (newOptions.getters) {
options.getters = newOptions.getters
}
if (newOptions.modules) {
for (const key in newOptions.modules) {
options.modules[key] = newOptions.modules[key]
}
}
updateModule(this._options, newOptions)
resetStore(this)
}

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

function updateModule (targetModule, newModule) {
if (newModule.actions) {
targetModule.actions = newModule.actions
}
if (newModule.mutations) {
targetModule.mutations = newModule.mutations
}
if (newModule.getters) {
targetModule.getters = newModule.getters
}
if (newModule.modules) {
for (const key in newModule.modules) {
if (!(targetModule.modules && targetModule.modules[key])) {
console.warn(
`[vuex] trying to add a new module '${key}' on hot reloading, ` +
'manual reload is needed'
)
return
}
updateModule(targetModule.modules[key], newModule.modules[key])
}
}
}

function resetStore (store) {
store._actions = Object.create(null)
store._mutations = Object.create(null)
Expand Down
21 changes: 21 additions & 0 deletions test/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,27 @@ describe('Vuex', () => {
})
})

it('hot reload: provide warning if a new module is given', () => {
const store = new Vuex.Store({})

spyOn(console, 'warn')

store.hotUpdate({
modules: {
test: {
state: {
count: 0
}
}
}
})

expect(console.warn).toHaveBeenCalledWith(
'[vuex] trying to add a new module \'test\' on hot reloading, ' +
'manual reload is needed'
)
})

it('watch: with resetting vm', done => {
const store = new Vuex.Store({
state: {
Expand Down

0 comments on commit 32212d9

Please sign in to comment.