Skip to content

Instantly share code, notes, and snippets.

@smashercosmo
Last active July 2, 2016 20:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smashercosmo/d7f2feff3d6d805c824c8fe301980af4 to your computer and use it in GitHub Desktop.
Save smashercosmo/d7f2feff3d6d805c824c8fe301980af4 to your computer and use it in GitHub Desktop.
const injectMiddleware = (staticDeps, dynamicDeps) => ({ dispatch, getState }) => next => action => {
const deps = {...staticDeps};
Object.keys(dynamicDeps).forEach(key => {
deps[key] = dynamicDeps[key]();
});
return next(typeof action === 'function'
? action({ ...deps, dispatch, getState })
: action
);
}
const dependenciesMiddleware = injectDependencies(
{api},
{
analytics: () => window.analytics
}
);
const store = createStore(
reducer,
applyMiddleware(dependenciesMiddleware)
)
function fetchUser(id) {
return ({dispatch, getState, analytics, api}) => {
api.fetchUser(id).then(user => {
analytics().track(...);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment