Forráskód Böngészése

avoid destroy watch callback even if resetting inner vm (#281)

katashin 8 éve
szülő
commit
b3ba637e03
2 módosított fájl, 29 hozzáadás és 1 törlés
  1. 2 1
      src/index.js
  2. 27 0
      test/unit/test.js

+ 2 - 1
src/index.js

@@ -23,6 +23,7 @@ class Store {
     this._wrappedGetters = Object.create(null)
     this._runtimeModules = Object.create(null)
     this._subscribers = []
+    this._watcherVM = new Vue()
 
     // bind commit and dispatch to self
     const store = this
@@ -109,7 +110,7 @@ class Store {
 
   watch (getter, cb, options) {
     assert(typeof getter === 'function', `store.watch only accepts a function.`)
-    return this._vm.$watch(() => getter(this.state), cb, options)
+    return this._watcherVM.$watch(() => getter(this.state), cb, options)
   }
 
   replaceState (state) {

+ 27 - 0
test/unit/test.js

@@ -928,4 +928,31 @@ describe('Vuex', () => {
       done()
     })
   })
+
+  it('watch: with resetting vm', done => {
+    const store = new Vuex.Store({
+      state: {
+        count: 0
+      },
+      mutations: {
+        [TEST]: state => state.count++
+      }
+    })
+
+    const spy = jasmine.createSpy()
+    store.watch(state => state.count, spy)
+
+    // reset store vm
+    store.registerModule('test', {})
+
+    Vue.nextTick(() => {
+      store.commit(TEST)
+      expect(store.state.count).toBe(1)
+
+      Vue.nextTick(() => {
+        expect(spy).toHaveBeenCalled()
+        done()
+      })
+    })
+  })
 })