Created
January 16, 2017 04:11
-
-
Save Gethyl/2871793be22ac5020434ab0fc4b4882d to your computer and use it in GitHub Desktop.
Understanding redux-thunk
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
//compose from redux/compose.js | |
function compose(...funcs) { | |
console.info("$$$ compose function ") | |
console.dir( funcs) | |
if (funcs.length === 0) { | |
return arg => arg | |
} | |
if (funcs.length === 1) { | |
return funcs[0] | |
} | |
return funcs.reduce((a, b) => (...args) => a(b(...args))) | |
} | |
//applyMiddleware from redux/applyMiddleware.js | |
export const applyMiddleware = function applyMiddleware(...middlewares) { | |
//console.info("applyMiddleware") | |
return function (createStore) { | |
return function (reducer, preloadedState, enhancer) { | |
const store = createStore(reducer, preloadedState, enhancer) | |
let dispatch = store.dispatch | |
let chain = [] | |
const middlewareAPI = { | |
getState: store.getState, | |
dispatch: (action) => dispatch(action) | |
} | |
chain = middlewares.map(middleware => { | |
console.info("~~~~~~middleware ") | |
console.dir( middleware) | |
const middlewareReturn = middleware(middlewareAPI) | |
console.info("~~~~~~middlewareReturn ") | |
console.dir( middlewareReturn) | |
return middlewareReturn | |
}) | |
console.info("~~~chain ") | |
console.dir( chain) | |
// console.info("~~~store.dispatch ") | |
// console.dir( store.dispatch) | |
//dispatch = compose(...chain)(store.dispatch) | |
let composedFunc = compose(...chain) | |
console.info("~~~composedFunc ") | |
console.dir( composedFunc) | |
dispatch = composedFunc(store.dispatch) | |
console.info("~~~new dispatch AFTER compose i.e composedFunc RETURNED ") | |
console.dir( dispatch) | |
return { | |
...store, | |
dispatch | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment