-
Notifications
You must be signed in to change notification settings - Fork 54
/
TrackedRewardsProvider.ts
42 lines (34 loc) · 1.65 KB
/
TrackedRewardsProvider.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
import { BehaviorSubject } from 'rxjs';
import { CLEAN_FN_STATS, ProviderFnStats, ProviderTracker } from './ProviderTracker';
import { RewardsProvider } from '@cardano-sdk/core';
export class RewardsProviderStats {
readonly healthCheck$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
readonly rewardsHistory$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
readonly rewardAccountBalance$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
shutdown() {
this.healthCheck$.complete();
this.rewardsHistory$.complete();
this.rewardAccountBalance$.complete();
}
reset() {
this.healthCheck$.next(CLEAN_FN_STATS);
this.rewardsHistory$.next(CLEAN_FN_STATS);
this.rewardAccountBalance$.next(CLEAN_FN_STATS);
}
}
/** Wraps a RewardsProvider, tracking # of calls of each function */
export class TrackedRewardsProvider extends ProviderTracker implements RewardsProvider {
readonly stats = new RewardsProviderStats();
readonly healthCheck: RewardsProvider['healthCheck'];
readonly rewardsHistory: RewardsProvider['rewardsHistory'];
readonly rewardAccountBalance: RewardsProvider['rewardAccountBalance'];
constructor(rewardsProvider: RewardsProvider) {
super();
rewardsProvider = rewardsProvider;
this.healthCheck = () => this.trackedCall(() => rewardsProvider.healthCheck(), this.stats.healthCheck$);
this.rewardsHistory = (fragments) =>
this.trackedCall(() => rewardsProvider.rewardsHistory(fragments), this.stats.rewardsHistory$);
this.rewardAccountBalance = (fragments) =>
this.trackedCall(() => rewardsProvider.rewardAccountBalance(fragments), this.stats.rewardAccountBalance$);
}
}