Skip to content

Commit f0f594c

Browse files
refactor: Drop InversifyJS: BatchReader (#856)
1 parent bfedb26 commit f0f594c

File tree

6 files changed

+111
-52
lines changed

6 files changed

+111
-52
lines changed

src/BatchReader/BatchReader.ts

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Container } from 'inversify'
21
import { Db, MongoClient, Collection } from 'mongodb'
32
import * as Pino from 'pino'
43
import { pick } from 'ramda'
@@ -23,7 +22,6 @@ export interface BatchReaderConfiguration extends LoggingConfiguration, ServiceC
2322
export class BatchReader {
2423
private readonly logger: Pino.Logger
2524
private readonly configuration: BatchReaderConfiguration
26-
private readonly container = new Container()
2725
private mongoClient: MongoClient
2826
private dbConnection: Db
2927
private router: Router
@@ -44,15 +42,48 @@ export class BatchReader {
4442
this.messaging = new Messaging(this.configuration.rabbitmqUrl, exchangesMessaging)
4543
await this.messaging.start()
4644

47-
this.initializeContainer()
45+
const ipfs = new IPFS({
46+
configuration: {
47+
ipfsUrl: this.configuration.ipfsUrl,
48+
},
49+
})
50+
51+
const directoryCollection = this.dbConnection.collection('batchReader')
52+
53+
const directoryDAO = new DirectoryDAO({
54+
dependencies: {
55+
directoryCollection,
56+
},
57+
})
4858

49-
this.router = this.container.get('Router')
59+
const claimController = new ClaimController({
60+
dependencies: {
61+
directoryDAO,
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+
})
5074
await this.router.start()
5175

52-
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+
readNextDirectoryIntervalInSeconds: this.configuration.readNextDirectoryIntervalInSeconds,
83+
},
84+
exchange: this.configuration.exchanges,
85+
})
5386
await this.service.start()
54-
const directoryDAO: DirectoryDAO = this.container.get('DirectoryDAO')
55-
await directoryDAO.start()
5687

5788
this.logger.info('BatchReader Started')
5889
}
@@ -64,24 +95,4 @@ export class BatchReader {
6495
await this.router.stop()
6596
await this.service.stop()
6697
}
67-
68-
initializeContainer() {
69-
this.container.bind<Pino.Logger>('Logger').toConstantValue(this.logger)
70-
this.container.bind<ClaimController>('ClaimController').to(ClaimController)
71-
this.container.bind<Collection>('directoryCollection').toConstantValue(this.dbConnection.collection('batchReader'))
72-
this.container.bind<Db>('DB').toConstantValue(this.dbConnection)
73-
this.container.bind<DirectoryDAO>('DirectoryDAO').to(DirectoryDAO)
74-
this.container.bind<IPFS>('IPFS').to(IPFS)
75-
this.container.bind<IPFSConfiguration>('IPFSConfiguration').toConstantValue({
76-
ipfsUrl: this.configuration.ipfsUrl,
77-
})
78-
this.container.bind<Router>('Router').to(Router)
79-
this.container.bind<Messaging>('Messaging').toConstantValue(this.messaging)
80-
this.container.bind<Service>('Service').to(Service)
81-
this.container.bind<ServiceConfiguration>('ServiceConfiguration').toConstantValue({
82-
readNextDirectoryIntervalInSeconds: this.configuration.readNextDirectoryIntervalInSeconds,
83-
})
84-
85-
this.container.bind<ExchangeConfiguration>('ExchangeConfiguration').toConstantValue(this.configuration.exchanges)
86-
}
8798
}

src/BatchReader/ClaimController.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1-
import { inject, injectable } from 'inversify'
21
import { InsertWriteOpResult } from 'mongodb'
32

43
import { DirectoryDAO } from './DirectoryDAO'
54
import { IPFS } from './IPFS'
65

7-
@injectable()
6+
export interface Dependencies {
7+
readonly directoryDAO: DirectoryDAO
8+
readonly ipfs: IPFS
9+
}
10+
11+
export interface Arguments {
12+
readonly dependencies: Dependencies
13+
}
14+
815
export class ClaimController {
916
private readonly directoryDAO: DirectoryDAO
1017
private readonly ipfs: IPFS
1118

12-
constructor(@inject('DirectoryDAO') directoryDAO: DirectoryDAO, @inject('IPFS') ipfs: IPFS) {
19+
constructor({
20+
dependencies: {
21+
directoryDAO,
22+
ipfs,
23+
},
24+
}: Arguments) {
1325
this.directoryDAO = directoryDAO
1426
this.ipfs = ipfs
1527
}

src/BatchReader/DirectoryDAO.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, InsertWriteOpResult, UpdateWriteOpResult } from 'mongodb'
32

43
import { ErrorCodes } from 'Helpers/MongoDB'
@@ -31,11 +30,22 @@ type incEntryAttempts = (x: Entry) => Promise<UpdateWriteOpResult>
3130

3231
type updateFileHashes = (x: Entry) => Promise<UpdateWriteOpResult>
3332

34-
@injectable()
33+
export interface Dependencies {
34+
readonly directoryCollection: Collection
35+
}
36+
37+
export interface Arguments {
38+
readonly dependencies: Dependencies
39+
}
40+
3541
export class DirectoryDAO {
3642
private readonly directoryCollection: Collection
3743

38-
constructor(@inject('directoryCollection') directoryCollection: Collection) {
44+
constructor({
45+
dependencies: {
46+
directoryCollection,
47+
},
48+
}: Arguments) {
3949
this.directoryCollection = directoryCollection
4050
}
4151

src/BatchReader/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
import { isNotNil } from 'Helpers/isNotNil'
@@ -40,11 +39,16 @@ export interface IPFSConfiguration {
4039
readonly ipfsUrl: string
4140
}
4241

43-
@injectable()
42+
export interface Arguments {
43+
readonly configuration: IPFSConfiguration
44+
}
45+
4446
export class IPFS {
4547
private readonly url: string
4648

47-
constructor(@inject('IPFSConfiguration') configuration: IPFSConfiguration) {
49+
constructor({
50+
configuration,
51+
}: Arguments) {
4852
this.url = configuration.ipfsUrl
4953
}
5054

src/BatchReader/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
import { isNil } from 'ramda'
43

@@ -9,19 +8,31 @@ import { Messaging } from 'Messaging/Messaging'
98
import { ClaimController } from './ClaimController'
109
import { ExchangeConfiguration } from './ExchangeConfiguration'
1110

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

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

src/BatchReader/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 readNextDirectoryIntervalInSeconds: 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)