Skip to content

Commit bc64679

Browse files
author
gaoyong
committed
add batch dispatch
1 parent 0274bbe commit bc64679

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/createStore.js

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -181,29 +181,60 @@ export default function createStore(reducer, preloadedState, enhancer) {
181181
* return something else (for example, a Promise you can await).
182182
*/
183183
function dispatch(action) {
184-
if (!isPlainObject(action)) {
185-
throw new Error(
186-
'Actions must be plain objects. ' +
187-
'Use custom middleware for async actions.'
188-
)
189-
}
184+
if (Array.isArray(action)) {
185+
let actions = action;
186+
if (actions.length <= 1) {
187+
throw new Error('Actions list must not empty.')
188+
}
189+
for (let _action of actions) {
190+
if (!isPlainObject(_action)) {
191+
throw new Error(
192+
'Action in actions list must be plain objects. ' +
193+
'Use custom middleware for async actions.'
194+
)
195+
}
196+
197+
if (typeof _action.type === 'undefined') {
198+
throw new Error(
199+
'Action in actions list may not have an undefined "type" property. ' +
200+
'Have you misspelled a constant?'
201+
)
202+
}
203+
}
190204

191-
if (typeof action.type === 'undefined') {
192-
throw new Error(
193-
'Actions may not have an undefined "type" property. ' +
194-
'Have you misspelled a constant?'
195-
)
196-
}
205+
try {
206+
isDispatching = true
207+
for (let _action of actions) {
208+
currentState = currentReducer(currentState, _action)
209+
}
210+
} finally {
211+
isDispatching = false
212+
}
213+
} else {
214+
if (!isPlainObject(action)) {
215+
throw new Error(
216+
'Actions must be plain objects. ' +
217+
'Use custom middleware for async actions.'
218+
)
219+
}
197220

198-
if (isDispatching) {
199-
throw new Error('Reducers may not dispatch actions.')
200-
}
221+
if (typeof action.type === 'undefined') {
222+
throw new Error(
223+
'Actions may not have an undefined "type" property. ' +
224+
'Have you misspelled a constant?'
225+
)
226+
}
227+
228+
if (isDispatching) {
229+
throw new Error('Reducers may not dispatch actions.')
230+
}
201231

202-
try {
203-
isDispatching = true
204-
currentState = currentReducer(currentState, action)
205-
} finally {
206-
isDispatching = false
232+
try {
233+
isDispatching = true
234+
currentState = currentReducer(currentState, action)
235+
} finally {
236+
isDispatching = false
237+
}
207238
}
208239

209240
const listeners = (currentListeners = nextListeners)

0 commit comments

Comments
 (0)