Forked from GuillaumeNachury/async_middleware_redux.js
Created
June 14, 2018 12:12
-
-
Save abner/c4e6dd5633396b4cca0cb52f2de840af to your computer and use it in GitHub Desktop.
Redux Async middleware - Add a convient way to dispatch new action from an action
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
const asyncDispatcMiddleware = store => next => action => { | |
let syncActivityFinished = false; | |
let actionQueue = []; | |
function flushQueue() { | |
actionQueue.forEach(a => store.dispatch(a)); | |
actionQueue = []; | |
} | |
function asyncDispatch(asyncAction) { | |
actionQueue = actionQueue.concat([asyncAction]); | |
if (syncActivityFinished) { | |
flushQueue(); | |
} | |
} | |
const actionWithAsyncDispatch = Object.assign({}, action, {asyncDispatch}); | |
next(actionWithAsyncDispatch); | |
syncActivityFinished = true; | |
flushQueue(); | |
}; | |
export default asyncDispatcMiddleware; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment