Skip to content

Instantly share code, notes, and snippets.

@girvo
Forked from AdamMaras/redux-inject.js
Created March 8, 2016 02:45
Show Gist options
  • Save girvo/c5ae6e74d8f800bbec3d to your computer and use it in GitHub Desktop.
Save girvo/c5ae6e74d8f800bbec3d to your computer and use it in GitHub Desktop.
Redux dependency injection middleware
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