Jelajahi Sumber

store.on(mutation) -> store.subscribe

Evan You 9 tahun lalu
induk
melakukan
031ddb5ecf
5 mengubah file dengan 24 tambahan dan 11 penghapusan
  1. 1 1
      examples/todomvc/vuex/plugins.js
  2. 19 6
      src/index.js
  3. 1 1
      src/plugins/devtool.js
  4. 1 1
      src/plugins/logger.js
  5. 2 2
      test/unit/test.js

+ 1 - 1
examples/todomvc/vuex/plugins.js

@@ -2,7 +2,7 @@ import { STORAGE_KEY } from './store'
 import createLogger from '../../../src/plugins/logger'
 
 const localStoragePlugin = store => {
-  store.on('mutation', (mutation, { todos }) => {
+  store.subscribe((mutation, { todos }) => {
     localStorage.setItem(STORAGE_KEY, JSON.stringify(todos))
   })
 }

+ 19 - 6
src/index.js

@@ -30,7 +30,7 @@ class Store {
     this._dispatching = false
     this._rootMutations = this._mutations = mutations
     this._modules = modules
-    this._events = Object.create(null)
+    this._subscribers = []
     // bind dispatch to self
     const dispatch = this.dispatch
     this.dispatch = (...args) => {
@@ -127,7 +127,7 @@ class Store {
         const mutation = isObjectStyleDispatch
           ? payload
           : { type, payload }
-        this.emit('mutation', mutation, state)
+        this._subscribers.forEach(sub => sub(mutation, state))
       }
     } else {
       console.warn(`[vuex] Unknown mutation: ${type}`)
@@ -152,6 +152,23 @@ class Store {
     return this._vm.$watch(() => fn(this.state), cb, options)
   }
 
+  /**
+   * Subscribe to state changes. Fires after every mutation.
+   */
+
+  subscribe (fn) {
+    const subs = this._subscribers
+    if (subs.indexOf(fn) < 0) {
+      subs.push(fn)
+    }
+    return () => {
+      const i = subs.indexOf(fn)
+      if (i > -1) {
+        subs.splice(i, 1)
+      }
+    }
+  }
+
   /**
    * Hot update mutations & modules.
    *
@@ -274,10 +291,6 @@ function install (_Vue) {
     return
   }
   Vue = _Vue
-  // reuse Vue's event system
-  ;['on', 'off', 'once', 'emit'].forEach(e => {
-    Store.prototype[e] = Store.prototype['$' + e] = Vue.prototype['$' + e]
-  })
   override(Vue)
 }
 

+ 1 - 1
src/plugins/devtool.js

@@ -11,7 +11,7 @@ export default function devtoolPlugin (store) {
     store.replaceState(targetState)
   })
 
-  store.on('mutation', (mutation, state) => {
+  store.subscribe((mutation, state) => {
     hook.emit('vuex:mutation', mutation, state)
   })
 }

+ 1 - 1
src/plugins/logger.js

@@ -8,7 +8,7 @@ export default function createLogger ({
   return store => {
     let prevState = JSON.parse(JSON.stringify(store.state))
 
-    store.on('mutation', (mutation, state) => {
+    store.subscribe((mutation, state) => {
       if (typeof console === 'undefined') {
         return
       }

+ 2 - 2
test/unit/test.js

@@ -285,7 +285,7 @@ describe('Vuex', () => {
       plugins: [
         store => {
           initState = store.state
-          store.on('mutation', (mut, state) => {
+          store.subscribe((mut, state) => {
             expect(state).to.equal(store.state)
             mutations.push(mut)
           })
@@ -314,7 +314,7 @@ describe('Vuex', () => {
       plugins: [
         store => {
           initState = store.state
-          store.on('mutation', (mut, state) => {
+          store.subscribe((mut, state) => {
             expect(state).to.equal(store.state)
             mutations.push(mut)
           })