Skip to content

Instantly share code, notes, and snippets.

@jdjkelly
Last active May 1, 2017 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdjkelly/baf07859d963ebabc06b32e08a157dd9 to your computer and use it in GitHub Desktop.
Save jdjkelly/baf07859d963ebabc06b32e08a157dd9 to your computer and use it in GitHub Desktop.
dispatch
function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async 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
}
const listeners = currentListeners = nextListeners
for (let i = 0; i < listeners.length; i++) {
const listener = listeners[i]
listener()
}
return action
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment