Skip to content

Commit

Permalink
avoid to fire unrelated watchers with registerModule (fix vuejs#524) (v…
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn authored and yyx990803 committed Feb 6, 2017
1 parent 5954b4b commit 507d2a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class Store {

hotUpdate (newOptions) {
this._modules.update(newOptions)
resetStore(this)
resetStore(this, true)
}

_withCommit (fn) {
Expand All @@ -164,7 +164,7 @@ class Store {
}
}

function resetStore (store) {
function resetStore (store, hot) {
store._actions = Object.create(null)
store._mutations = Object.create(null)
store._wrappedGetters = Object.create(null)
Expand All @@ -173,10 +173,10 @@ function resetStore (store) {
// init all modules
installModule(store, state, [], store._modules.root, true)
// reset vm
resetStoreVM(store, state)
resetStoreVM(store, state, hot)
}

function resetStoreVM (store, state) {
function resetStoreVM (store, state, hot) {
const oldVm = store._vm

// bind store public getters
Expand Down Expand Up @@ -209,11 +209,13 @@ function resetStoreVM (store, state) {
}

if (oldVm) {
// dispatch changes in all subscribed watchers
// to force getter re-evaluation.
store._withCommit(() => {
oldVm.state = null
})
if (hot) {
// dispatch changes in all subscribed watchers
// to force getter re-evaluation for hot reloading.
store._withCommit(() => {
oldVm.state = null
})
}
Vue.nextTick(() => oldVm.$destroy())
}
}
Expand Down
22 changes: 22 additions & 0 deletions test/unit/modules.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,28 @@ describe('Modules', () => {
})
})

// #524
it('should not fire an unrelated watcher', done => {
const spy = jasmine.createSpy()
const store = new Vuex.Store({
modules: {
a: {
state: { value: 1 }
},
b: {}
}
})

store.watch(state => state.a, spy)
store.registerModule(['b', 'c'], {
state: { value: 2 }
})
Vue.nextTick(() => {
expect(spy).not.toHaveBeenCalled()
done()
})
})

describe('modules usage', () => {
it('module: mutation', function () {
const mutations = {
Expand Down

0 comments on commit 507d2a5

Please sign in to comment.