Last active
May 11, 2017 10:38
-
-
Save kishanio/c9846fd95db9d226ec7f623aa5ddc531 to your computer and use it in GitHub Desktop.
JS Functional Programming : Currying Function 2
This file contains 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
// Dependency : http://redux.js.org/ | |
// Dependency : https://egghead.io/lessons/javascript-redux-the-middleware-chain | |
// following store resolves promises before by injecting middlewares to store dispatch. | |
const promise = (store) => (next) => (action) => { | |
if ( typeof action.then === 'function' ) { | |
return action.then(next); | |
} | |
return next(action); | |
}; | |
const logger = (store) => (next) => ( action ) => { | |
console.group(action.type); | |
console.log('prev state', store.getState()); | |
console.log('action', action); | |
const returnValue = next(action); | |
console.log('next state', store.getState()); | |
console.groupEnd(action.type); | |
return returnValue; | |
}; | |
const wrapDispatchWithMiddleWares = (store, middlewares) => { | |
// enum in reverse in order to call function in series. | |
middlewares.slice().reverse().forEach( middleware => | |
store.dispatch = middleware(store)(store.dispatch) | |
); | |
}; | |
const configureStore = () => { | |
const store = createStore(todoApp); | |
const middlewares = [promise]; | |
middlewares.push(logger); | |
wrapDispatchWithMiddleWares(store, middlewares); | |
return store; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment