-
-
Save Shuumatsu/5d3b23c9aef1e15a95ad3d6242ca5fe5 to your computer and use it in GitHub Desktop.
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
default function applyMiddleware(...middlewares) { | |
return (createStore) => (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 => middleware(middlewareAPI)) | |
dispatch = compose(...chain)(store.dispatch) | |
return { | |
...store, | |
dispatch | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment