Created
September 10, 2017 20:37
-
-
Save g33kChris/ab10086da17bca7ddcd04954f11e754c to your computer and use it in GitHub Desktop.
Inverting the dependencies
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
// Is depdendent on 'baz', and a dependency of foo! | |
// Note how it no longer passes down an api client! | |
export const bar = (myLogger, baz) => () => { | |
myLogger.debug('lets make a request!'); | |
return baz("http://somehardcodedurl.com"); | |
} |
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
// "Lowest common denominator, is a dependency of bar! | |
export const baz = (myLogger, myApiClient) => url => | |
apiClient.get(url) | |
.then((response) => { | |
myLogger.info('You got a response!', response); | |
return response; | |
}); |
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
// Is dependant on two instances of 'bar' (but it doesn't care what bar1 and bar 2 do!) | |
export const foo = (bar1, bar2) => () => { | |
bar1(); | |
bar2(); | |
}; |
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
import foo from './foo'; | |
import baz from './baz'; | |
import bar from './bar'; | |
import VendorSuppliedApiClient from 'vendor-supplied-api-client'; | |
import VendorSuppliedLogger from 'vendor-supplied-logger'; | |
const main = () => { | |
const mySingleApiClientInstance = new VendorSuppliedApiClient('myrandomapikey'); | |
const myOtherApiClientInstance = new VendorSuppliedApiClient('mydeveloperapikey'); | |
const mySingleLoggerInstance = new VendorSuppliedLogger('myrandomapikey', 'filestream'); | |
return foo( | |
bar(mySingleLoggerInstance, baz(mySingleLoggerInstance, mySingleApiClientInstance)) | |
bar(mySingleLoggerInstance, baz(mySingleLoggerInstance, myOtherApiClientInstance)) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment