Skip to content

Instantly share code, notes, and snippets.

@vojtatranta
Created October 2, 2016 21:38
Show Gist options
  • Save vojtatranta/511920e758a2d454b6174975ac0cef4a to your computer and use it in GitHub Desktop.
Save vojtatranta/511920e758a2d454b6174975ac0cef4a to your computer and use it in GitHub Desktop.
var currentReducer = reducer
var currentState = preloadedState
var currentListeners = []
var nextListeners = currentListeners
var isDispatching = false
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
}
var listeners = currentListeners = nextListeners
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i]
listener()
}
return action
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment