8000 rename: dispatch -> commit, trigger -> dispatch · vuejs/v2.vuejs.org@5b6adc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b6adc0

Browse files
committed
rename: dispatch -> commit, trigger -> dispatch
1 parent 28df9a0 commit 5b6adc0

File tree

12 files changed

+60
-63
lines changed

12 files changed

+60
-63
lines changed

examples/chat/components/MessageSection.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default {
4242
sendMessage (e) {
4343
const text = e.target.value
4444
if (text.trim()) {
45-
this.$store.trigger('sendMessage', {
45+
this.$store.dispatch('sendMessage', {
4646
text,
4747
thread: this.thread
4848
})

examples/chat/components/ThreadSection.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default {
4040
},
4141
methods: {
4242
switchThread (id) {
43-
this.$store.trigger('switchThread', { id })
43+
this.$store.dispatch('switchThread', { id })
4444
}
4545
}
4646
}

examples/chat/vuex/actions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import * as api from '../api'
22
import * as types from './mutation-types'
33

4-
export const getAllMessages = ({ dispatch }) => {
4+
export const getAllMessages = ({ commit }) => {
55
api.getAllMessages(messages => {
6-
dispatch(types.RECEIVE_ALL, {
6+
commit(types.RECEIVE_ALL, {
77
messages
88
})
99
})
1010
}
1111

12-
export const sendMessage = ({ dispatch }, payload) => {
12+
export const sendMessage = ({ commit }, payload) => {
1313
api.createMessage(payload, message => {
14-
dispatch(types.RECEIVE_MESSAGE, {
14+
commit(types.RECEIVE_MESSAGE, {
1515
message
1616
})
1717
})
1818
}
1919

20-
export const switchThread = ({ dispatch }, payload) => {
21-
dispatch(types.SWITCH_THREAD, payload)
20+
export const switchThread = ({ commit }, payload) => {
21+
commit(types.SWITCH_THREAD, payload)
2222
}

examples/counter-hot/vuex/actions.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
export const increment = ({ dispatch }) => dispatch('increment')
2-
export const decrement = ({ dispatch }) => dispatch('decrement')
1+
export const increment = ({ commit }) => commit('increment')
2+
export const decrement = ({ commit }) => commit('decrement')
33

4-
export const incrementIfOdd = ({ dispatch, state }) => {
4+
export const incrementIfOdd = ({ commit, state }) => {
55
if ((state.count + 1) % 2 === 0) {
6-
dispatch('increment')
6+
commit('increment')
77
}
88
}
99

10-
export const incrementAsync = ({ dispatch }) => {
10+
export const incrementAsync = ({ commit }) => {
1111
setTimeout(() => {
12-
dispatch('decrement')
12+
commit('decrement')
1313
}, 1000)
1414
}

examples/counter/store.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ const mutations = {
2626
// actions are functions that causes side effects and can involve
2727
// asynchronous operations.
2828
const actions = {
29-
increment: ({ dispatch }) => dispatch('increment'),
30-
decrement: ({ dispatch }) => dispatch('decrement'),
31-
incrementIfOdd ({ dispatch, state }) {
29+
increment: ({ commit }) => commit('increment'),
30+
decrement: ({ commit }) => commit('decrement'),
31+
incrementIfOdd ({ commit, state }) {
3232
if ((state.count + 1) % 2 === 0) {
33-
dispatch('increment')
33+
commit('increment')
3434
}
3535
},
36-
incrementAsync ({ dispatch }) {
36+
incrementAsync ({ commit }) {
3737
return new Promise((resolve, reject) => {
3838
setTimeout(() => {
39-
dispatch('increment')
39+
commit('increment')
4040
resolve()
4141
}, 1000)
4242
})

examples/shopping-cart/components/Cart.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
},
3131
methods: {
3232
checkout (products) {
33-
this.$store.trigger('checkout', products)
33+
this.$store.dispatch('checkout', products)
3434
}
3535
}
3636
}

examples/shopping-cart/components/ProductList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default {
2323
'addToCart'
2424
]),
2525
created () {
26-
this.$store.trigger('getAllProducts')
26+
this.$store.dispatch('getAllProducts')
2727
}
2828
}
2929
</script>
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import shop from '../api/shop'
22
import * as types from './mutation-types'
33

4-
export const addToCart = ({ dispatch }, product) => {
4+
export const addToCart = ({ commit }, product) => {
55
if (product.inventory > 0) {
6-
dispatch(types.ADD_TO_CART, {
6+
commit(types.ADD_TO_CART, {
77
id: product.id
88
})
99
}
1010
}
1111

12-
export const checkout = ({ dispatch, state }, products) => {
12+
export const checkout = ({ commit, state }, products) => {
1313
const savedCartItems = [...state.cart.added]
14-
dispatch(types.CHECKOUT_REQUEST)
14+
commit(types.CHECKOUT_REQUEST)
1515
shop.buyProducts(
1616
products,
17-
() => dispatch(types.CHECKOUT_SUCCESS),
18-
() => dispatch(types.CHECKOUT_FAILURE, { savedCartItems })
17+
() => commit(types.CHECKOUT_SUCCESS),
18+
() => commit(types.CHECKOUT_FAILURE, { savedCartItems })
1919
)
2020
}
2121

22-
export const getAllProducts = ({ dispatch }) => {
22+
export const getAllProducts = ({ commit }) => {
2323
shop.getProducts(products => {
24-
dispatch(types.RECEIVE_PRODUCTS, { products })
24+
commit(types.RECEIVE_PRODUCTS, { products })
2525
})
2626
}

examples/todomvc/components/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export default {
8181
addTodo (e) {
8282
var text = e.target.value
8383
if (text.trim()) {
84-
this.$store.trigger('addTodo', { text })
84+
this.$store.dispatch('addTodo', { text })
8585
}
8686
e.target.value = ''
8787
},

examples/todomvc/vuex/actions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ export const toggleAll = makeAction('TOGGLE_ALL')
66
export const clearCompleted = makeAction('CLEAR_COMPLETED')
77

88
function makeAction (type) {
9-
return ({ dispatch }, payload) => dispatch(type, payload)
9+
return ({ commit }, payload) => commit(type, payload)
1010
}

src/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function mapActions (actions) {
1212
const res = {}
1313
normalizeMap(actions).forEach(({ key, val }) => {
1414
res[key] = function mappedAction (...args) {
15-
return this.$store.trigger(val, ...args)
15+
return this.$store.dispatch(val, ...args)
1616
}
1717
})
1818
return res

src/index.js

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ class Store {
2727

2828
// store internal state
2929
this._options = options
30-
this._dispatching = false
30+
this._committing = false
3131
this._events = Object.create(null)
3232
this._actions = Object.create(null)
3333
this._mutations = Object.create(null)
3434
this._subscribers = []
3535

36-
// bind dispatch and trigger to self
36+
// bind commit and dispatch to self
3737
const store = this
38-
const { trigger, dispatch } = this
39-
this.trigger = function boundTrigger (type, payload) {
40-
trigger.call(store, type, payload)
41-
}
38+
const { dispatch, commit } = this
4239
this.dispatch = function boundDispatch (type, payload) {
4340
dispatch.call(store, type, payload)
4441
}
42+
this.commit = function boundCommit (type, payload) {
43+
commit.call(store, type, payload)
44+
}
4545

4646
// init state and getters
4747
const getters = extractModuleGetters(options.getters, modules)
@@ -66,9 +66,9 @@ class Store {
6666
}
6767

6868
replaceState (state) {
69-
this._dispatching = true
69+
this._committing = true
7070
this._vm.state = state
71-
this._dispatching = false
71+
this._committing = false
7272
}
7373

7474
module (path, module, hot) {
@@ -123,57 +123,54 @@ class Store {
123123
action (type, handler, path = []) {
124124
const entry = this._actions[type] || (this._actions[type] = [])
125125
const store = this
126-
const { trigger, dispatch } = this
126+
const { dispatch, commit } = this
127127
entry.push(function wrappedActionHandler (payload, cb) {
128128
let res = handler({
129-
trigger,
130129
dispatch,
130+
commit,
131131
state: getNestedState(store.state, path)
132132
}, payload, cb)
133133
if (!isPromise(res)) {
134134
res = Promise.resolve(res)
135135
}
136136
return res.catch(err => {
137-
console.warn(`[vuex] error in Promise returned from action "${type}":`)
137+
console.error(`[vuex] error in Promise returned from action "${type}":`)
138138
console.error(err)
139139
})
140140
})
141141
}
142142

143-
dispatch (type, payload) {
143+
commit (type, payload) {
144144
const entry = this._mutations[type]
145145
if (!entry) {
146-
console.warn(`[vuex] unknown mutation type: ${type}`)
146+
console.error(`[vuex] unknown mutation type: ${type}`)
147147
return
148148
}
149-
// check object-style dispatch
149+
// check object-style commit
150150
let mutation
151151
if (isObject(type)) {
152152
payload = mutation = type
153153
} else {
154154
mutation = { type, payload }
155155
}
156-
this._dispatching = true
157-
entry.forEach(function dispatchIterator (handler) {
156+
this._committing = true
157+
entry.forEach(function commitIterator (handler) {
158158
handler(payload)
159159
})
160-
this._dispatching = false
160+
this._committing = false
161161
this._subscribers.forEach(sub => sub(mutation, this.state))
162162
}
163163

164-
trigger (type, payload, cb) {
164+
dispatch (type, payload) {
165165
const entry = this._actions[type]
166166
if (!entry) {
167-
console.warn(`[vuex] unknown action type: ${type}`)
167+
debugger
168+
console.error(`[vuex] unknown action type: ${type}`)
168169
return
169170
}
170-
if (typeof payload === 'function') {
171-
cb = payload
172-
payload = undefined
173-
}
174171
return entry.length > 1
175172
? Promise.all(entry.map(handler => handler(payload)))
176-
: entry[0](payload)
173+
: Promise.resolve(entry[0](payload))
177174
}
178175

179176
subscribe (fn) {
@@ -214,11 +211,11 @@ class Store {
214211
if (this.strict) {
215212
enableStrictMode(this)
216213
}
217-
// trigger changes in all subscribed watchers
214+
// dispatch changes in all subscribed watchers
218215
// to force getter re-evaluation.
219-
this._dispatching = true
216+
this._committing = true
220217
oldVm.state = null
221-
this._dispatching = false
218+
this._committing = false
222219
Vue.nextTick(() => oldVm.$destroy())
223220
}
224221
}
@@ -257,7 +254,7 @@ function extractModuleGetters (getters = {}, modules = {}, path = []) {
257254
Object.keys(module.getters).forEach(getterKey => {
258255
const rawGetter = module.getters[getterKey]
259256
if (getters[getterKey]) {
260-
console.warn(`[vuex] duplicate getter key: ${getterKey}`)
257+
console.error(`[vuex] duplicate getter key: ${getterKey}`)
261258
return
262259
}
263260
getters[getterKey] = function wrappedGetter (state) {
@@ -272,7 +269,7 @@ function extractModu 962E leGetters (getters = {}, modules = {}, path = []) {
272269

273270
function enableStrictMode (store) {
274271
store._vm.$watch('state', () => {
275-
if (!store._dispatching) {
272+
if (!store._committing) {
276273
throw new Error(
277274
'[vuex] Do not mutate vuex store state outside mutation handlers.'
278275
)
@@ -294,7 +291,7 @@ function getNestedState (state, path) {
294291

295292
function install (_Vue) {
296293
if (Vue) {
297-
console.warn(
294+
console.error(
298295
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
299296
)
300297
return

0 commit comments

Comments
 (0)
0