Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Last active January 19, 2017 08:13
Show Gist options
  • Save andycarrell/b0f4863d2a83503d94a3fe3bab079d62 to your computer and use it in GitHub Desktop.
Save andycarrell/b0f4863d2a83503d94a3fe3bab079d62 to your computer and use it in GitHub Desktop.
// Returns 'a' middleware
const combineMiddlewares = (...middlewares) => store => {
// Map over applying store
const stored = middlewares.map(mw => mw(store))
return next => {
// Map over applying the next as 'next'
const [first, ...rest] = stored
const nextMiddlewares = [...rest, next]
// Should this be done in reverse?
// (apply 'next', then through the chain & mutate locally?)
const nexted = stored
.map((mw, i) => mw(nextMiddlewares[i]))
.reverse()
return action => compose(...nexted)(action)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment