Last active
May 1, 2017 00:24
-
-
Save jdjkelly/baf07859d963ebabc06b32e08a157dd9 to your computer and use it in GitHub Desktop.
dispatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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