All files / src/services/ProviderTracker ProviderTracker.ts

100% Statements 11/11
100% Branches 0/0
100% Functions 2/2
100% Lines 11/11

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 43 44 45 46 47 48 49                              43x     43x   1685x 1685x 1685x 1677x 1677x           1677x   8x         8x         62x            
import { BehaviorSubject } from 'rxjs';
 
export interface ProviderFnStats {
  numCalls: number;
  numResponses: number;
  numFailures: number;
  /**
   * Either:
   * - at least 1 response received
   * - was set manually (via setStatInitialized) to indicate that it won't make a request on wallet initialization
   */
  initialized?: boolean;
  didLastRequestFail?: boolean;
}
 
export const CLEAN_FN_STATS: ProviderFnStats = { numCalls: 0, numFailures: 0, numResponses: 0 };
 
/** Wraps a Provider, tracking # of calls of each function */
export abstract class ProviderTracker {
  protected async trackedCall<T>(call: () => Promise<T>, tracker: BehaviorSubject<ProviderFnStats>) {
    tracker.next({ ...tracker.value, numCalls: tracker.value.numCalls + 1 });
    try {
      const result = await call();
      const numResponses = tracker.value.numResponses + 1;
      tracker.next({
        ...tracker.value,
        didLastRequestFail: false,
        initialized: true,
        numResponses
      });
      return result;
    } catch (error) {
      tracker.next({
        ...tracker.value,
        didLastRequestFail: true,
        numFailures: tracker.value.numFailures + 1
      });
      throw error;
    }
  }
 
  setStatInitialized(tracker: BehaviorSubject<ProviderFnStats>) {
    tracker.next({
      ...tracker.value,
      initialized: true
    });
  }
}