Last active
July 2, 2016 20:16
-
-
Save smashercosmo/d7f2feff3d6d805c824c8fe301980af4 to your computer and use it in GitHub Desktop.
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
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 | |
); | |
} |
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
const dependenciesMiddleware = injectDependencies( | |
{api}, | |
{ | |
analytics: () => window.analytics | |
} | |
); | |
const store = createStore( | |
reducer, | |
applyMiddleware(dependenciesMiddleware) | |
) |
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
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