Skip to content

Instantly share code, notes, and snippets.

@g33kChris
Created September 10, 2017 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save g33kChris/ab10086da17bca7ddcd04954f11e754c to your computer and use it in GitHub Desktop.
Save g33kChris/ab10086da17bca7ddcd04954f11e754c to your computer and use it in GitHub Desktop.
Inverting the dependencies
// 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");
}
// "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;
});
// 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();
};
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