瀏覽代碼

rename: dispatch -> commit, trigger -> dispatch

Evan You 8 年之前
父節點
當前提交
5b6adc0616

+ 1 - 1
examples/chat/components/MessageSection.vue

@@ -42,7 +42,7 @@ export default {
     sendMessage (e) {
     sendMessage (e) {
       const text = e.target.value
       const text = e.target.value
       if (text.trim()) {
       if (text.trim()) {
-        this.$store.trigger('sendMessage', {
+        this.$store.dispatch('sendMessage', {
           text,
           text,
           thread: this.thread
           thread: this.thread
         })
         })

+ 1 - 1
examples/chat/components/ThreadSection.vue

@@ -40,7 +40,7 @@ export default {
   },
   },
   methods: {
   methods: {
     switchThread (id) {
     switchThread (id) {
-      this.$store.trigger('switchThread', { id })
+      this.$store.dispatch('switchThread', { id })
     }
     }
   }
   }
 }
 }

+ 6 - 6
examples/chat/vuex/actions.js

@@ -1,22 +1,22 @@
 import * as api from '../api'
 import * as api from '../api'
 import * as types from './mutation-types'
 import * as types from './mutation-types'
 
 
-export const getAllMessages = ({ dispatch }) => {
+export const getAllMessages = ({ commit }) => {
   api.getAllMessages(messages => {
   api.getAllMessages(messages => {
-    dispatch(types.RECEIVE_ALL, {
+    commit(types.RECEIVE_ALL, {
       messages
       messages
     })
     })
   })
   })
 }
 }
 
 
-export const sendMessage = ({ dispatch }, payload) => {
+export const sendMessage = ({ commit }, payload) => {
   api.createMessage(payload, message => {
   api.createMessage(payload, message => {
-    dispatch(types.RECEIVE_MESSAGE, {
+    commit(types.RECEIVE_MESSAGE, {
       message
       message
     })
     })
   })
   })
 }
 }
 
 
-export const switchThread = ({ dispatch }, payload) => {
-  dispatch(types.SWITCH_THREAD, payload)
+export const switchThread = ({ commit }, payload) => {
+  commit(types.SWITCH_THREAD, payload)
 }
 }

+ 6 - 6
examples/counter-hot/vuex/actions.js

@@ -1,14 +1,14 @@
-export const increment = ({ dispatch }) => dispatch('increment')
-export const decrement = ({ dispatch }) => dispatch('decrement')
+export const increment = ({ commit }) => commit('increment')
+export const decrement = ({ commit }) => commit('decrement')
 
 
-export const incrementIfOdd = ({ dispatch, state }) => {
+export const incrementIfOdd = ({ commit, state }) => {
   if ((state.count + 1) % 2 === 0) {
   if ((state.count + 1) % 2 === 0) {
-    dispatch('increment')
+    commit('increment')
   }
   }
 }
 }
 
 
-export const incrementAsync = ({ dispatch }) => {
+export const incrementAsync = ({ commit }) => {
   setTimeout(() => {
   setTimeout(() => {
-    dispatch('decrement')
+    commit('decrement')
   }, 1000)
   }, 1000)
 }
 }

+ 6 - 6
examples/counter/store.js

@@ -26,17 +26,17 @@ const mutations = {
 // actions are functions that causes side effects and can involve
 // actions are functions that causes side effects and can involve
 // asynchronous operations.
 // asynchronous operations.
 const actions = {
 const actions = {
-  increment: ({ dispatch }) => dispatch('increment'),
-  decrement: ({ dispatch }) => dispatch('decrement'),
-  incrementIfOdd ({ dispatch, state }) {
+  increment: ({ commit }) => commit('increment'),
+  decrement: ({ commit }) => commit('decrement'),
+  incrementIfOdd ({ commit, state }) {
     if ((state.count + 1) % 2 === 0) {
     if ((state.count + 1) % 2 === 0) {
-      dispatch('increment')
+      commit('increment')
     }
     }
   },
   },
-  incrementAsync ({ dispatch }) {
+  incrementAsync ({ commit }) {
     return new Promise((resolve, reject) => {
     return new Promise((resolve, reject) => {
       setTimeout(() => {
       setTimeout(() => {
-        dispatch('increment')
+        commit('increment')
         resolve()
         resolve()
       }, 1000)
       }, 1000)
     })
     })

+ 1 - 1
examples/shopping-cart/components/Cart.vue

@@ -30,7 +30,7 @@ export default {
   },
   },
   methods: {
   methods: {
     checkout (products) {
     checkout (products) {
-      this.$store.trigger('checkout', products)
+      this.$store.dispatch('checkout', products)
     }
     }
   }
   }
 }
 }

+ 1 - 1
examples/shopping-cart/components/ProductList.vue

@@ -23,7 +23,7 @@ export default {
     'addToCart'
     'addToCart'
   ]),
   ]),
   created () {
   created () {
-    this.$store.trigger('getAllProducts')
+    this.$store.dispatch('getAllProducts')
   }
   }
 }
 }
 </script>
 </script>

+ 8 - 8
examples/shopping-cart/vuex/actions.js

@@ -1,26 +1,26 @@
 import shop from '../api/shop'
 import shop from '../api/shop'
 import * as types from './mutation-types'
 import * as types from './mutation-types'
 
 
-export const addToCart = ({ dispatch }, product) => {
+export const addToCart = ({ commit }, product) => {
   if (product.inventory > 0) {
   if (product.inventory > 0) {
-    dispatch(types.ADD_TO_CART, {
+    commit(types.ADD_TO_CART, {
       id: product.id
       id: product.id
     })
     })
   }
   }
 }
 }
 
 
-export const checkout = ({ dispatch, state }, products) => {
+export const checkout = ({ commit, state }, products) => {
   const savedCartItems = [...state.cart.added]
   const savedCartItems = [...state.cart.added]
-  dispatch(types.CHECKOUT_REQUEST)
+  commit(types.CHECKOUT_REQUEST)
   shop.buyProducts(
   shop.buyProducts(
     products,
     products,
-    () => dispatch(types.CHECKOUT_SUCCESS),
-    () => dispatch(types.CHECKOUT_FAILURE, { savedCartItems })
+    () => commit(types.CHECKOUT_SUCCESS),
+    () => commit(types.CHECKOUT_FAILURE, { savedCartItems })
   )
   )
 }
 }
 
 
-export const getAllProducts = ({ dispatch }) => {
+export const getAllProducts = ({ commit }) => {
   shop.getProducts(products => {
   shop.getProducts(products => {
-    dispatch(types.RECEIVE_PRODUCTS, { products })
+    commit(types.RECEIVE_PRODUCTS, { products })
   })
   })
 }
 }

+ 1 - 1
examples/todomvc/components/App.vue

@@ -81,7 +81,7 @@ export default {
     addTodo (e) {
     addTodo (e) {
       var text = e.target.value
       var text = e.target.value
       if (text.trim()) {
       if (text.trim()) {
-        this.$store.trigger('addTodo', { text })
+        this.$store.dispatch('addTodo', { text })
       }
       }
       e.target.value = ''
       e.target.value = ''
     },
     },

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

@@ -6,5 +6,5 @@ export const toggleAll = makeAction('TOGGLE_ALL')
 export const clearCompleted = makeAction('CLEAR_COMPLETED')
 export const clearCompleted = makeAction('CLEAR_COMPLETED')
 
 
 function makeAction (type) {
 function makeAction (type) {
-  return ({ dispatch }, payload) => dispatch(type, payload)
+  return ({ commit }, payload) => commit(type, payload)
 }
 }

+ 1 - 1
src/helpers.js

@@ -12,7 +12,7 @@ export function mapActions (actions) {
   const res = {}
   const res = {}
   normalizeMap(actions).forEach(({ key, val }) => {
   normalizeMap(actions).forEach(({ key, val }) => {
     res[key] = function mappedAction (...args) {
     res[key] = function mappedAction (...args) {
-      return this.$store.trigger(val, ...args)
+      return this.$store.dispatch(val, ...args)
     }
     }
   })
   })
   return res
   return res

+ 27 - 30
src/index.js

@@ -27,21 +27,21 @@ class Store {
 
 
     // store internal state
     // store internal state
     this._options = options
     this._options = options
-    this._dispatching = false
+    this._committing = false
     this._events = Object.create(null)
     this._events = Object.create(null)
     this._actions = Object.create(null)
     this._actions = Object.create(null)
     this._mutations = Object.create(null)
     this._mutations = Object.create(null)
     this._subscribers = []
     this._subscribers = []
 
 
-    // bind dispatch and trigger to self
+    // bind commit and dispatch to self
     const store = this
     const store = this
-    const { trigger, dispatch } = this
-    this.trigger = function boundTrigger (type, payload) {
-      trigger.call(store, type, payload)
-    }
+    const { dispatch, commit } = this
     this.dispatch = function boundDispatch (type, payload) {
     this.dispatch = function boundDispatch (type, payload) {
       dispatch.call(store, type, payload)
       dispatch.call(store, type, payload)
     }
     }
+    this.commit = function boundCommit (type, payload) {
+      commit.call(store, type, payload)
+    }
 
 
     // init state and getters
     // init state and getters
     const getters = extractModuleGetters(options.getters, modules)
     const getters = extractModuleGetters(options.getters, modules)
@@ -66,9 +66,9 @@ class Store {
   }
   }
 
 
   replaceState (state) {
   replaceState (state) {
-    this._dispatching = true
+    this._committing = true
     this._vm.state = state
     this._vm.state = state
-    this._dispatching = false
+    this._committing = false
   }
   }
 
 
   module (path, module, hot) {
   module (path, module, hot) {
@@ -123,57 +123,54 @@ class Store {
   action (type, handler, path = []) {
   action (type, handler, path = []) {
     const entry = this._actions[type] || (this._actions[type] = [])
     const entry = this._actions[type] || (this._actions[type] = [])
     const store = this
     const store = this
-    const { trigger, dispatch } = this
+    const { dispatch, commit } = this
     entry.push(function wrappedActionHandler (payload, cb) {
     entry.push(function wrappedActionHandler (payload, cb) {
       let res = handler({
       let res = handler({
-        trigger,
         dispatch,
         dispatch,
+        commit,
         state: getNestedState(store.state, path)
         state: getNestedState(store.state, path)
       }, payload, cb)
       }, payload, cb)
       if (!isPromise(res)) {
       if (!isPromise(res)) {
         res = Promise.resolve(res)
         res = Promise.resolve(res)
       }
       }
       return res.catch(err => {
       return res.catch(err => {
-        console.warn(`[vuex] error in Promise returned from action "${type}":`)
+        console.error(`[vuex] error in Promise returned from action "${type}":`)
         console.error(err)
         console.error(err)
       })
       })
     })
     })
   }
   }
 
 
-  dispatch (type, payload) {
+  commit (type, payload) {
     const entry = this._mutations[type]
     const entry = this._mutations[type]
     if (!entry) {
     if (!entry) {
-      console.warn(`[vuex] unknown mutation type: ${type}`)
+      console.error(`[vuex] unknown mutation type: ${type}`)
       return
       return
     }
     }
-    // check object-style dispatch
+    // check object-style commit
     let mutation
     let mutation
     if (isObject(type)) {
     if (isObject(type)) {
       payload = mutation = type
       payload = mutation = type
     } else {
     } else {
       mutation = { type, payload }
       mutation = { type, payload }
     }
     }
-    this._dispatching = true
-    entry.forEach(function dispatchIterator (handler) {
+    this._committing = true
+    entry.forEach(function commitIterator (handler) {
       handler(payload)
       handler(payload)
     })
     })
-    this._dispatching = false
+    this._committing = false
     this._subscribers.forEach(sub => sub(mutation, this.state))
     this._subscribers.forEach(sub => sub(mutation, this.state))
   }
   }
 
 
-  trigger (type, payload, cb) {
+  dispatch (type, payload) {
     const entry = this._actions[type]
     const entry = this._actions[type]
     if (!entry) {
     if (!entry) {
-      console.warn(`[vuex] unknown action type: ${type}`)
+      debugger
+      console.error(`[vuex] unknown action type: ${type}`)
       return
       return
     }
     }
-    if (typeof payload === 'function') {
-      cb = payload
-      payload = undefined
-    }
     return entry.length > 1
     return entry.length > 1
       ? Promise.all(entry.map(handler => handler(payload)))
       ? Promise.all(entry.map(handler => handler(payload)))
-      : entry[0](payload)
+      : Promise.resolve(entry[0](payload))
   }
   }
 
 
   subscribe (fn) {
   subscribe (fn) {
@@ -214,11 +211,11 @@ class Store {
       if (this.strict) {
       if (this.strict) {
         enableStrictMode(this)
         enableStrictMode(this)
       }
       }
-      // trigger changes in all subscribed watchers
+      // dispatch changes in all subscribed watchers
       // to force getter re-evaluation.
       // to force getter re-evaluation.
-      this._dispatching = true
+      this._committing = true
       oldVm.state = null
       oldVm.state = null
-      this._dispatching = false
+      this._committing = false
       Vue.nextTick(() => oldVm.$destroy())
       Vue.nextTick(() => oldVm.$destroy())
     }
     }
   }
   }
@@ -257,7 +254,7 @@ function extractModuleGetters (getters = {}, modules = {}, path = []) {
       Object.keys(module.getters).forEach(getterKey => {
       Object.keys(module.getters).forEach(getterKey => {
         const rawGetter = module.getters[getterKey]
         const rawGetter = module.getters[getterKey]
         if (getters[getterKey]) {
         if (getters[getterKey]) {
-          console.warn(`[vuex] duplicate getter key: ${getterKey}`)
+          console.error(`[vuex] duplicate getter key: ${getterKey}`)
           return
           return
         }
         }
         getters[getterKey] = function wrappedGetter (state) {
         getters[getterKey] = function wrappedGetter (state) {
@@ -272,7 +269,7 @@ function extractModuleGetters (getters = {}, modules = {}, path = []) {
 
 
 function enableStrictMode (store) {
 function enableStrictMode (store) {
   store._vm.$watch('state', () => {
   store._vm.$watch('state', () => {
-    if (!store._dispatching) {
+    if (!store._committing) {
       throw new Error(
       throw new Error(
         '[vuex] Do not mutate vuex store state outside mutation handlers.'
         '[vuex] Do not mutate vuex store state outside mutation handlers.'
       )
       )
@@ -294,7 +291,7 @@ function getNestedState (state, path) {
 
 
 function install (_Vue) {
 function install (_Vue) {
   if (Vue) {
   if (Vue) {
-    console.warn(
+    console.error(
       '[vuex] already installed. Vue.use(Vuex) should be called only once.'
       '[vuex] already installed. Vue.use(Vuex) should be called only once.'
     )
     )
     return
     return