Skip to content

Instantly share code, notes, and snippets.

@kishanio
Last active May 11, 2017 10:38
Show Gist options
  • Save kishanio/c9846fd95db9d226ec7f623aa5ddc531 to your computer and use it in GitHub Desktop.
Save kishanio/c9846fd95db9d226ec7f623aa5ddc531 to your computer and use it in GitHub Desktop.
JS Functional Programming : Currying Function 2
// 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