-
-
Save girvo/c5ae6e74d8f800bbec3d to your computer and use it in GitHub Desktop.
Redux dependency injection middleware
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
export default function createInjectMiddleware(map) { | |
return store => next => action => { | |
if (typeof action !== "object" | |
|| typeof action.payload !== "function" | |
|| action.meta == null | |
|| action.meta.inject == null) { | |
return next(action); | |
} | |
const defaultInjections = { | |
action, store, dispatch: store.dispatch, getState: store.getState | |
} | |
const injections = Object.assign({}, defaultInjections, map); | |
let argumentMap = action.meta.inject.map(k => injections[k]); | |
action.payload(...argumentMap); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment