Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add batch dispatch #3414

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 51 additions & 20 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,29 +181,60 @@ export default function createStore(reducer, preloadedState, enhancer) {
* return something else (for example, a Promise you can await).
*/
function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
if (Array.isArray(action)) {
let actions = action;
if (actions.length <= 1) {
throw new Error('Actions list must not empty.')
}
for (let _action of actions) {
if (!isPlainObject(_action)) {
throw new Error(
'Action in actions list must be plain objects. ' +
'Use custom middleware for async actions.'
)
}

if (typeof _action.type === 'undefined') {
throw new Error(
'Action in actions list may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}
}

if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}
try {
isDispatching = true
for (let _action of actions) {
currentState = currentReducer(currentState, _action)
}
} finally {
isDispatching = false
}
} else {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}

if (isDispatching) {
throw new Error('Reducers may not dispatch actions.')
}
if (typeof action.type === 'undefined') {
throw new Error(
'Actions may not have an undefined "type" property. ' +
'Have you misspelled a constant?'
)
}

if (isDispatching) {
throw new Error('Reducers may not dispatch actions.')
}

try {
isDispatching = true
currentState = currentReducer(currentState, action)
} finally {
isDispatching = false
try {
isDispatching = true
currentState = currentReducer(currentState, action)
} finally {
isDispatching = false
}
}

const listeners = (currentListeners = nextListeners)
Expand Down