All files / src/services/ProviderTracker TrackedRewardsProvider.ts

72% Statements 18/25
100% Branches 0/0
50% Functions 5/10
80.95% Lines 17/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 4342x 42x     42x 135x 135x 135x     120x 120x 120x                     42x 135x           135x 135x   135x   135x 127x   135x        
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$);
  }
}