All files / src/services/ProviderTracker TrackedUtxoProvider.ts

100% Statements 19/19
100% Branches 0/0
100% Functions 8/8
100% Lines 16/16

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 3643x 43x     43x 132x 132x     119x 119x       2x 2x         43x 132x         132x 132x   132x   132x 129x      
import { BehaviorSubject } from 'rxjs';
import { CLEAN_FN_STATS, ProviderFnStats, ProviderTracker } from './ProviderTracker';
import { UtxoProvider } from '@cardano-sdk/core';
 
export class UtxoProviderStats {
  readonly healthCheck$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
  readonly utxoByAddresses$ = new BehaviorSubject<ProviderFnStats>(CLEAN_FN_STATS);
 
  shutdown() {
    this.utxoByAddresses$.complete();
    this.healthCheck$.complete();
  }
 
  reset() {
    this.healthCheck$.next(CLEAN_FN_STATS);
    this.utxoByAddresses$.next(CLEAN_FN_STATS);
  }
}
 
/** Wraps a UtxoProvider, tracking # of calls of each function */
export class TrackedUtxoProvider extends ProviderTracker implements UtxoProvider {
  readonly stats = new UtxoProviderStats();
  readonly healthCheck: UtxoProvider['healthCheck'];
  readonly utxoByAddresses: UtxoProvider['utxoByAddresses'];
 
  constructor(utxoProvider: UtxoProvider) {
    super();
    utxoProvider = utxoProvider;
 
    this.healthCheck = () => this.trackedCall(() => utxoProvider.healthCheck(), this.stats.healthCheck$);
 
    this.utxoByAddresses = (addresses) =>
      this.trackedCall(() => utxoProvider.utxoByAddresses(addresses), this.stats.utxoByAddresses$);
  }
}