Skip to content

Instantly share code, notes, and snippets.

@Shuumatsu
Last active March 27, 2017 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shuumatsu/5d3b23c9aef1e15a95ad3d6242ca5fe5 to your computer and use it in GitHub Desktop.
Save Shuumatsu/5d3b23c9aef1e15a95ad3d6242ca5fe5 to your computer and use it in GitHub Desktop.
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