-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
66 lines (55 loc) · 1.74 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { ServiceBroker } from 'moleculer'
import { Blockchain } from '../../types.js'
import {
MsIds,
getRegistryNodesWithService,
waitForAllNodesWithService,
} from '../common.js'
import { FetcherClientI } from './interface.js'
import { FetcherStateRequestArgs, FetcherState } from './src/types.js'
/**
* Client to access the main fetcher service through the broker.
*/
export class FetcherMsClient {
/**
* @param broker The broker instance to retrieve the actual service from.
* @param msId The moleculer service id of the fetcher service.
*/
constructor(
protected broker: ServiceBroker,
protected blockchains: Record<Blockchain, FetcherClientI>,
protected msId: MsIds = MsIds.Fetcher,
) {}
useBlockchain(blockchainId: Blockchain): FetcherClientI {
return this.getBlockchainClientInstance(blockchainId)
}
waitForAll(indexers: string[]): Promise<void> {
return waitForAllNodesWithService(this.broker, indexers, this.msId)
}
waitForService(): Promise<void> {
return this.broker.waitForServices(this.msId)
}
getAllFetchers(): string[] {
return getRegistryNodesWithService(this.broker, this.msId)
}
getAllBlockchains(): Blockchain[] {
return Object.keys(this.blockchains) as Blockchain[]
}
getNodeId(): string {
return this.broker.nodeID
}
getFetcherState(args: FetcherStateRequestArgs): Promise<FetcherState[]> {
return this.broker.call(`${this.msId}.getFetcherState`, args, {
nodeID: args.fetcher,
})
}
protected getBlockchainClientInstance(
blockchainId: Blockchain,
): FetcherClientI {
const instance = this.blockchains[blockchainId]
if (!instance) {
throw new Error(`${blockchainId} blockchain not supported`)
}
return instance
}
}