MCPcopy
hub / github.com/vuejs/vuex / dispatch

Method dispatch

src/store.js:132–191  ·  view source on GitHub ↗
(_type, _payload)

Source from the content-addressed store, hash-verified

130 }
131
132 dispatch (_type, _payload) {
133 // check object-style dispatch
134 const {
135 type,
136 payload
137 } = unifyObjectStyle(_type, _payload)
138
139 const action = { type, payload }
140 const entry = this._actions[type]
141 if (!entry) {
142 if (__DEV__) {
143 console.error(`[vuex] unknown action type: ${type}`)
144 }
145 return
146 }
147
148 try {
149 this._actionSubscribers
150 .slice() // shallow copy to prevent iterator invalidation if subscriber synchronously calls unsubscribe
151 .filter(sub => sub.before)
152 .forEach(sub => sub.before(action, this.state))
153 } catch (e) {
154 if (__DEV__) {
155 console.warn(`[vuex] error in before action subscribers: `)
156 console.error(e)
157 }
158 }
159
160 const result = entry.length > 1
161 ? Promise.all(entry.map(handler => handler(payload)))
162 : entry[0](payload)
163
164 return new Promise((resolve, reject) => {
165 result.then(res => {
166 try {
167 this._actionSubscribers
168 .filter(sub => sub.after)
169 .forEach(sub => sub.after(action, this.state))
170 } catch (e) {
171 if (__DEV__) {
172 console.warn(`[vuex] error in after action subscribers: `)
173 console.error(e)
174 }
175 }
176 resolve(res)
177 }, error => {
178 try {
179 this._actionSubscribers
180 .filter(sub => sub.error)
181 .forEach(sub => sub.error(action, this.state, error))
182 } catch (e) {
183 if (__DEV__) {
184 console.warn(`[vuex] error in error action subscribers: `)
185 console.error(e)
186 }
187 }
188 reject(error)
189 })

Callers 5

index.tsFile · 0.80
modules.spec.jsFile · 0.80
store.spec.jsFile · 0.80
hot-reload.spec.jsFile · 0.80
makeLocalContextFunction · 0.80

Calls 2

unifyObjectStyleFunction · 0.90
handlerFunction · 0.50

Tested by

no test coverage detected