8000 avoid destroy watch callback even if resetting inner vm (#281) · poorprogrammer/vuejs.org@b3ba637 · GitHub
[go: up one dir, main page]

Skip to content

Commit b3ba637

Browse files
ktsnyyx990803
authored andcommitted
avoid destroy watch callback even if resetting inner vm (vuejs#281)
1 parent faac7ca commit b3ba637

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Store {
2323
this._wrappedGetters = Object.create(null)
2424
this._runtimeModules = Object.create(null)
2525
this._subscribers = []
26+
this._watcherVM = new Vue()
2627

2728
// bind commit and dispatch to self
2829
const store = this
@@ -109,7 +110,7 @@ class Store {
109110

110111
watch (getter, cb, options) {
111112
assert(typeof getter === 'function', `store.watch only accepts a function.`)
112-
return this._vm.$watch(() => getter(this.state), cb, options)
113+
return this._watcherVM.$watch(() => getter(this.state), cb, options)
113114
}
114115

115116
replaceState (state) {

test/unit/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,4 +928,31 @@ describe('Vuex', () => {
928928
done()
929929
})
930930
})
931+
932+
it('watch: with resetting vm', done => {
933+
const store = new Vuex.Store({
934+
state: {
935+
count: 0
936+
},
937+
mutations: {
938+
[TEST]: state => state.count++
939+
}
940+
})
941+
942+
const spy = jasmine.createSpy()
943+
store.watch(state => state.count, spy)
944+
945+
// reset store vm
946+
store.registerModule('test', {})
947+
948+
Vue.nextTick(() => {
949+
store.commit(TEST)
950+
expect(store.state.count).toBe(1)
951+
952+
Vue.nextTick(() => {
953+
expect(spy).toHaveBeenCalled()
954+
done()
955+
})
956+
})
957+
})
931958
})

0 commit comments

Comments
 (0)
0