From 30093cad4169c1310d27c296b273b13390dbc00a Mon Sep 17 00:00:00 2001 From: Kenny Lavender <5175120+kennylavender@users.noreply.github.com> Date: Wed, 5 Dec 2018 11:48:33 -0800 Subject: [PATCH] refactor: Drop InversifyJS: BatchWriter (#857) --- src/BatchWriter/BatchWriter.ts | 66 +++++++++++++++++------------- src/BatchWriter/ClaimController.ts | 19 +++++++-- src/BatchWriter/FileDAO.ts | 16 ++++++-- src/BatchWriter/IPFS.ts | 10 +++-- src/BatchWriter/Router.ts | 27 ++++++++---- src/BatchWriter/Service.ts | 27 ++++++++---- 6 files changed, 111 insertions(+), 54 deletions(-) diff --git a/src/BatchWriter/BatchWriter.ts b/src/BatchWriter/BatchWriter.ts index eacf4214..e85bcecf 100644 --- a/src/BatchWriter/BatchWriter.ts +++ b/src/BatchWriter/BatchWriter.ts @@ -1,4 +1,3 @@ -import { injectable, Container } from 'inversify' import { Db, MongoClient, Collection } from 'mongodb' import * as Pino from 'pino' import { pick } from 'ramda' @@ -20,11 +19,9 @@ export interface BatchWriterConfiguration extends LoggingConfiguration, ServiceC readonly exchanges: ExchangeConfiguration } -@injectable() export class BatchWriter { private readonly logger: Pino.Logger private readonly configuration: BatchWriterConfiguration - private readonly container = new Container() private mongoClient: MongoClient private dbConnection: Db private router: Router @@ -45,15 +42,48 @@ export class BatchWriter { this.messaging = new Messaging(this.configuration.rabbitmqUrl, exchangesMessaging) await this.messaging.start() - this.initializeContainer() + const ipfs = new IPFS({ + configuration: { + ipfsUrl: this.configuration.ipfsUrl, + }, + }) + + const fileCollection: Collection = this.dbConnection.collection('batchWriter') + + const fileDAO = new FileDAO({ + dependencies: { + fileCollection, + }, + }) - this.router = this.container.get('Router') + const claimController = new ClaimController({ + dependencies: { + fileDAO, + ipfs, + }, + }) + + this.router = new Router({ + dependencies: { + logger: this.logger, + messaging: this.messaging, + claimController, + }, + exchange: this.configuration.exchanges, + }) await this.router.start() - this.service = this.container.get('Service') + this.service = new Service({ + dependencies: { + logger: this.logger, + messaging: this.messaging, + }, + configuration: { + batchCreationIntervalInSeconds: this.configuration.batchCreationIntervalInSeconds, + }, + exchange: this.configuration.exchanges, + }) await this.service.start() - const fileDAO: FileDAO = this.container.get('FileDAO') - await fileDAO.start() this.logger.info('Batcher Writer Started') } @@ -65,24 +95,4 @@ export class BatchWriter { await this.mongoClient.close() await this.router.stop() } - - initializeContainer() { - this.container.bind('Logger').toConstantValue(this.logger) - this.container.bind('ClaimController').to(ClaimController) - this.container.bind('DB').toConstantValue(this.dbConnection) - this.container.bind('FileDAO').to(FileDAO) - this.container.bind('fileCollection').toConstantValue(this.dbConnection.collection('batchWriter')) - this.container.bind('IPFS').to(IPFS) - this.container.bind('IPFSConfiguration').toConstantValue({ - ipfsUrl: this.configuration.ipfsUrl, - }) - this.container.bind('Router').to(Router) - this.container.bind('Messaging').toConstantValue(this.messaging) - this.container.bind('Service').to(Service) - this.container.bind('ServiceConfiguration').toConstantValue({ - batchCreationIntervalInSeconds: this.configuration.batchCreationIntervalInSeconds, - }) - - this.container.bind('ExchangeConfiguration').toConstantValue(this.configuration.exchanges) - } } diff --git a/src/BatchWriter/ClaimController.ts b/src/BatchWriter/ClaimController.ts index 01619616..00b01b5e 100644 --- a/src/BatchWriter/ClaimController.ts +++ b/src/BatchWriter/ClaimController.ts @@ -1,16 +1,27 @@ -import { inject, injectable } from 'inversify' - import { NoMoreEntriesException } from 'Exceptions' import { FileDAO } from './FileDAO' import { IPFS } from './IPFS' -@injectable() +export interface Dependencies { + readonly fileDAO: FileDAO + readonly ipfs: IPFS +} + +export interface Arguments { + readonly dependencies: Dependencies +} + export class ClaimController { private readonly fileDAO: FileDAO private readonly ipfs: IPFS - constructor(@inject('FileDAO') fileDAO: FileDAO, @inject('IPFS') ipfs: IPFS) { + constructor({ + dependencies: { + fileDAO, + ipfs, + }, + }: Arguments) { this.fileDAO = fileDAO this.ipfs = ipfs } diff --git a/src/BatchWriter/FileDAO.ts b/src/BatchWriter/FileDAO.ts index 4326ad35..e4dad3b0 100644 --- a/src/BatchWriter/FileDAO.ts +++ b/src/BatchWriter/FileDAO.ts @@ -1,4 +1,3 @@ -import { inject, injectable } from 'inversify' import { Collection, InsertOneWriteOpResult, UpdateWriteOpResult } from 'mongodb' export interface Entry { @@ -18,11 +17,22 @@ type completeEntry = (x: Entry) => Promise type completeEntries = (xs: ReadonlyArray) => Promise> -@injectable() +export interface Dependencies { + readonly fileCollection: Collection +} + +export interface Arguments { + readonly dependencies: Dependencies +} + export class FileDAO { private readonly fileCollection: Collection - constructor(@inject('fileCollection') fileCollection: Collection) { + constructor({ + dependencies: { + fileCollection, + }, + }: Arguments) { this.fileCollection = fileCollection } diff --git a/src/BatchWriter/IPFS.ts b/src/BatchWriter/IPFS.ts index 731ff529..4a508eb9 100644 --- a/src/BatchWriter/IPFS.ts +++ b/src/BatchWriter/IPFS.ts @@ -1,4 +1,3 @@ -import { inject, injectable } from 'inversify' import fetch from 'node-fetch' type addFileToDirectory = (directoryhash: string, filehash: string) => Promise @@ -11,11 +10,16 @@ export interface IPFSConfiguration { readonly ipfsUrl: string } -@injectable() +export interface Arguments { + readonly configuration: IPFSConfiguration +} + export class IPFS { private readonly url: string - constructor(@inject('IPFSConfiguration') configuration: IPFSConfiguration) { + constructor({ + configuration, + }: Arguments) { this.url = configuration.ipfsUrl } diff --git a/src/BatchWriter/Router.ts b/src/BatchWriter/Router.ts index 5c974322..43af2dbf 100644 --- a/src/BatchWriter/Router.ts +++ b/src/BatchWriter/Router.ts @@ -1,4 +1,3 @@ -import { inject, injectable } from 'inversify' import * as Pino from 'pino' import { NoMoreEntriesException } from 'Exceptions' @@ -8,19 +7,31 @@ import { Messaging } from 'Messaging/Messaging' import { ClaimController } from './ClaimController' import { ExchangeConfiguration } from './ExchangeConfiguration' -@injectable() +export interface Dependencies { + readonly logger: Pino.Logger + readonly messaging: Messaging + readonly claimController: ClaimController +} + +export interface Arguments { + readonly dependencies: Dependencies + readonly exchange: ExchangeConfiguration +} + export class Router { private readonly logger: Pino.Logger private readonly messaging: Messaging private readonly claimController: ClaimController private readonly exchange: ExchangeConfiguration - constructor( - @inject('Logger') logger: Pino.Logger, - @inject('Messaging') messaging: Messaging, - @inject('ClaimController') claimController: ClaimController, - @inject('ExchangeConfiguration') exchange: ExchangeConfiguration, - ) { + constructor({ + dependencies: { + logger, + messaging, + claimController, + }, + exchange, + }: Arguments) { this.logger = childWithFileName(logger, __filename) this.messaging = messaging this.claimController = claimController diff --git a/src/BatchWriter/Service.ts b/src/BatchWriter/Service.ts index 54a1ff35..16b7812b 100644 --- a/src/BatchWriter/Service.ts +++ b/src/BatchWriter/Service.ts @@ -1,5 +1,4 @@ import { Interval } from '@po.et/poet-js' -import { inject, injectable } from 'inversify' import * as Pino from 'pino' import { childWithFileName } from 'Helpers/Logging' @@ -12,19 +11,31 @@ export interface ServiceConfiguration { readonly batchCreationIntervalInSeconds: number } -@injectable() +export interface Dependencies { + readonly messaging: Messaging + readonly logger: Pino.Logger +} + +export interface Arguments { + readonly dependencies: Dependencies + readonly configuration: ServiceConfiguration + readonly exchange: ExchangeConfiguration +} + export class Service { private readonly interval: Interval private readonly messaging: Messaging private readonly logger: Pino.Logger private readonly exchange: ExchangeConfiguration - constructor( - @inject('Logger') logger: Pino.Logger, - @inject('ServiceConfiguration') configuration: ServiceConfiguration, - @inject('Messaging') messaging: Messaging, - @inject('ExchangeConfiguration') exchange: ExchangeConfiguration, - ) { + constructor({ + dependencies: { + logger, + messaging, + }, + configuration, + exchange, + }: Arguments) { this.logger = childWithFileName(logger, __filename) this.messaging = messaging this.exchange = exchange