Comparing two different ways to do dependency injection
Simple factory (straight forward)
const createStuff = (dependencies) => {
const newStuff = {
//... using the dependencies
}
| import algoliasearch from 'algoliasearch'; | |
| const client = algoliasearch('applicationId', 'apiKey'); | |
| const index = client.initIndex('indexName'); | |
| index.setSettings({ | |
| attributesForFaceting: ['someDate'] | |
| // other settings | |
| }); |
Comparing two different ways to do dependency injection
Simple factory (straight forward)
const createStuff = (dependencies) => {
const newStuff = {
//... using the dependencies
}
| const { | |
| GraphQLObjectType, | |
| GraphQLScalarType, | |
| TypeInfo, | |
| visit, | |
| visitWithTypeInfo, | |
| findDeprecatedUsages, | |
| parse, | |
| validate, | |
| execute, |
| import knex from 'knex'; | |
| import userQueries from './user'; | |
| function DB(qb) { | |
| this.qb = qb; | |
| } | |
| DB.prototype.startTransaction = function(fn) { | |
| this.knex.transaction(trx => { |
| const proto = { | |
| timeout: null, | |
| promise: null, | |
| racer: null, | |
| then(fn) { | |
| this.promise = this.promise.then(result => { | |
| return Promise.race([this.racer, fn(result)]); | |
| }); | |
| return this; |
| // no need to load any addon here only register | |
| storiesOf('newAPI', module) | |
| .add('story', ({ knob , note, state: { counter, setCounter } }, context) => { | |
| note('myNotes') | |
| return <div onClick={() => setCounter(n => n + 1)}>{knob.text('name', 'Alex')} - {counter} </div> | |
| }); |
| const definedProto = { | |
| $$typeof: Symbol.for('jest.asymmetricMatcher'), | |
| asymmetricMatch(o) { | |
| return typeof o !== undefined; | |
| }, | |
| toAsymmetricMatcher() { | |
| return 'Defined'; | |
| }, |
| const stories = storiesOf('Button'); | |
| // this will apply the decorator to every story | |
| stories.addDecorator(withKnobs) | |
| // this will call the function with the final storyFn computed in `add` | |
| stories.addMiddleware(fn) | |
| stories.addMiddleware(fn(options)) |
| // db/utils/buildSaver.js | |
| const buildEntitySaver = (tableName, entityToDB) => { | |
| function create(entity) { | |
| return queryBuilder(tableName) | |
| .insert(entityToDB(entity)) | |
| .returning('id') | |
| .then(([id]) => id); |
| const mapping = { | |
| userId: 'user_id', | |
| comment: 'comment', | |
| date: 'date', | |
| id: 'id', | |
| createdAt: 'created_at', | |
| updatedAt: 'updated_at', | |
| }; |