Skip to content

Commit 30093ca

Browse files
refactor: Drop InversifyJS: BatchWriter (#857)
1 parent f0f594c commit 30093ca

File tree

6 files changed

+111
-54
lines changed

6 files changed

+111
-54
lines changed

src/BatchWriter/BatchWriter.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { injectable, Container } from 'inversify'
21
import { Db, MongoClient, Collection } from 'mongodb'
32
import * as Pino from 'pino'
43
import { pick } from 'ramda'
@@ -20,11 +19,9 @@ export interface BatchWriterConfiguration extends LoggingConfiguration, ServiceC
2019
readonly exchanges: ExchangeConfiguration
2120
}
2221

23-
@injectable()
2422
export class BatchWriter {
2523
private readonly logger: Pino.Logger
2624
private readonly configuration: BatchWriterConfiguration
27-
private readonly container = new Container()
2825
private mongoClient: MongoClient
2926
private dbConnection: Db
3027
private router: Router
@@ -45,15 +42,48 @@ export class BatchWriter {
4542
this.messaging = new Messaging(this.configuration.rabbitmqUrl, exchangesMessaging)
4643
await this.messaging.start()
4744

48-
this.initializeContainer()
45+
const ipfs = new IPFS({
46+
configuration: {
47+
ipfsUrl: this.configuration.ipfsUrl,
48+
},
49+
})
50+
51+
const fileCollection: Collection = this.dbConnection.collection('batchWriter')
52+
53+
const fileDAO = new FileDAO({
54+
dependencies: {
55+
fileCollection,
56+
},
57+
})
4958

50-
this.router = this.container.get('Router')
59+
const claimController = new ClaimController({
60+
dependencies: {
61+
fileDAO,
62+
ipfs,
63+
},
64+
})
65+
66+
this.router = new Router({
67+
dependencies: {
68+
logger: this.logger,
69+
messaging: this.messaging,
70+
claimController,
71+
},
72+
exchange: this.configuration.exchanges,
73+
})
5174
await this.router.start()
5275

53-
this.service = this.container.get('Service')
76+
this.service = new Service({
77+
dependencies: {
78+
logger: this.logger,
79+
messaging: this.messaging,
80+
},
81+
configuration: {
82+
batchCreationIntervalInSeconds: this.configuration.batchCreationIntervalInSeconds,
83+
},
84+
exchange: this.configuration.exchanges,
85+
})
5486
await this.service.start()
55-
const fileDAO: FileDAO = this.container.get('FileDAO')
56-
await fileDAO.start()
5787

5888
this.logger.info('Batcher Writer Started')
5989
}
@@ -65,24 +95,4 @@ export class BatchWriter {
6595
await this.mongoClient.close()
6696
await this.router.stop()
6797
}
68-
69-
initializeContainer() {
70-
this.container.bind<Pino.Logger>('Logger').toConstantValue(this.logger)
71-
this.container.bind<ClaimController>('ClaimController').to(ClaimController)
72-
this.container.bind<Db>('DB').toConstantValue(this.dbConnection)
73-
this.container.bind<FileDAO>('FileDAO').to(FileDAO)
74-
this.container.bind<Collection>('fileCollection').toConstantValue(this.dbConnection.collection('batchWriter'))
75-
this.container.bind<IPFS>('IPFS').to(IPFS)
76-
this.container.bind<IPFSConfiguration>('IPFSConfiguration').toConstantValue({
77-
ipfsUrl: this.configuration.ipfsUrl,
78-
})
79-
this.container.bind<Router>('Router').to(Router)
80-
this.container.bind<Messaging>('Messaging').toConstantValue(this.messaging)
81-
this.container.bind<Service>('Service').to(Service)
82-
this.container.bind<ServiceConfiguration>('ServiceConfiguration').toConstantValue({
83-
batchCreationIntervalInSeconds: this.configuration.batchCreationIntervalInSeconds,
84-
})
85-
86-
this.container.bind<ExchangeConfiguration>('ExchangeConfiguration').toConstantValue(this.configuration.exchanges)
87-
}
8898
}

src/BatchWriter/ClaimController.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
import { inject, injectable } from 'inversify'
2-
31
import { NoMoreEntriesException } from 'Exceptions'
42

53
import { FileDAO } from './FileDAO'
64
import { IPFS } from './IPFS'
75

8-
@injectable()
6+
export interface Dependencies {
7+
readonly fileDAO: FileDAO
8+
readonly ipfs: IPFS
9+
}
10+
11+
export interface Arguments {
12+
readonly dependencies: Dependencies
13+
}
14+
915
export class ClaimController {
1016
private readonly fileDAO: FileDAO
1117
private readonly ipfs: IPFS
1218

13-
constructor(@inject('FileDAO') fileDAO: FileDAO, @inject('IPFS') ipfs: IPFS) {
19+
constructor({
20+
dependencies: {
21+
fileDAO,
22+
ipfs,
23+
},
24+
}: Arguments) {
1425
this.fileDAO = fileDAO
1526
this.ipfs = ipfs
1627
}

src/BatchWriter/FileDAO.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { inject, injectable } from 'inversify'
21
import { Collection, InsertOneWriteOpResult, UpdateWriteOpResult } from 'mongodb'
32

43
export interface Entry {
@@ -18,11 +17,22 @@ type completeEntry = (x: Entry) => Promise<UpdateWriteOpResult>
1817

1918
type completeEntries = (xs: ReadonlyArray<Entry>) => Promise<ReadonlyArray<UpdateWriteOpResult>>
2019

21-
@injectable()
20+
export interface Dependencies {
21+
readonly fileCollection: Collection
22+
}
23+
24+
export interface Arguments {
25+
readonly dependencies: Dependencies
26+
}
27+
2228
export class FileDAO {
2329
private readonly fileCollection: Collection
2430

25-
constructor(@inject('fileCollection') fileCollection: Collection) {
31+
constructor({
32+
dependencies: {
33+
fileCollection,
34+
},
35+
}: Arguments) {
2636
this.fileCollection = fileCollection
2737
}
2838

src/BatchWriter/IPFS.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { inject, injectable } from 'inversify'
21
import fetch from 'node-fetch'
32

43
type addFileToDirectory = (directoryhash: string, filehash: string) => Promise<string>
@@ -11,11 +10,16 @@ export interface IPFSConfiguration {
1110
readonly ipfsUrl: string
1211
}
1312

14-
@injectable()
13+
export interface Arguments {
14+
readonly configuration: IPFSConfiguration
15+
}
16+
1517
export class IPFS {
1618
private readonly url: string
1719

18-
constructor(@inject('IPFSConfiguration') configuration: IPFSConfiguration) {
20+
constructor({
21+
configuration,
22+
}: Arguments) {
1923
this.url = configuration.ipfsUrl
2024
}
2125

src/BatchWriter/Router.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { inject, injectable } from 'inversify'
21
import * as Pino from 'pino'
32

43
import { NoMoreEntriesException } from 'Exceptions'
@@ -8,19 +7,31 @@ import { Messaging } from 'Messaging/Messaging'
87
import { ClaimController } from './ClaimController'
98
import { ExchangeConfiguration } from './ExchangeConfiguration'
109

11-
@injectable()
10+
export interface Dependencies {
11+
readonly logger: Pino.Logger
12+
readonly messaging: Messaging
13+
readonly claimController: ClaimController
14+
}
15+
16+
export interface Arguments {
17+
readonly dependencies: Dependencies
18+
readonly exchange: ExchangeConfiguration
19+
}
20+
1221
export class Router {
1322
private readonly logger: Pino.Logger
1423
private readonly messaging: Messaging
1524
private readonly claimController: ClaimController
1625
private readonly exchange: ExchangeConfiguration
1726

18-
constructor(
19-
@inject('Logger') logger: Pino.Logger,
20-
@inject('Messaging') messaging: Messaging,
21-
@inject('ClaimController') claimController: ClaimController,
22-
@inject('ExchangeConfiguration') exchange: ExchangeConfiguration,
23-
) {
27+
constructor({
28+
dependencies: {
29+
logger,
30+
messaging,
31+
claimController,
32+
},
33+
exchange,
34+
}: Arguments) {
2435
this.logger = childWithFileName(logger, __filename)
2536
this.messaging = messaging
2637
this.claimController = claimController

src/BatchWriter/Service.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Interval } from '@po.et/poet-js'
2-
import { inject, injectable } from 'inversify'
32
import * as Pino from 'pino'
43

54
import { childWithFileName } from 'Helpers/Logging'
@@ -12,19 +11,31 @@ export interface ServiceConfiguration {
1211
readonly batchCreationIntervalInSeconds: number
1312
}
1413

15-
@injectable()
14+
export interface Dependencies {
15+
readonly messaging: Messaging
16+
readonly logger: Pino.Logger
17+
}
18+
19+
export interface Arguments {
20+
readonly dependencies: Dependencies
21+
readonly configuration: ServiceConfiguration
22+
readonly exchange: ExchangeConfiguration
23+
}
24+
1625
export class Service {
1726
private readonly interval: Interval
1827
private readonly messaging: Messaging
1928
private readonly logger: Pino.Logger
2029
private readonly exchange: ExchangeConfiguration
2130

22-
constructor(
23-
@inject('Logger') logger: Pino.Logger,
24-
@inject('ServiceConfiguration') configuration: ServiceConfiguration,
25-
@inject('Messaging') messaging: Messaging,
26-
@inject('ExchangeConfiguration') exchange: ExchangeConfiguration,
27-
) {
31+
constructor({
32+
dependencies: {
33+
logger,
34+
messaging,
35+
},
36+
configuration,
37+
exchange,
38+
}: Arguments) {
2839
this.logger = childWithFileName(logger, __filename)
2940
this.messaging = messaging
3041
this.exchange = exchange

0 commit comments

Comments
 (0)