All files application.ts

97.73% Statements 43/44
100% Branches 8/8
92.86% Functions 13/14
97.56% Lines 40/41

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1001x   1x 1x         1x           1x           10x   10x 10x 10x       10x 10x   4x 20x   4x     10x   6x 12x   6x 6x 6x   6x       4x           10x 4x 4x 20x       10x 6x 6x 12x       10x       4x   4x 12x   4x 4x       10x       10x       4x       2x      
import { EventEmitter } from 'events';
import { ApplicationConfiguration } from './configuration';
import { Transaction, TRANSACTION_EVENTS } from './transaction';
import {
  TransactionTransporter,
  DefaultTransactionTransporter,
  TRANSPORTER_EVENTS
} from './transporter';
import {
  DeveloperKeyValidator,
  DefaultDeveloperKeyValidator,
  DEVELOPER_KEY_VALIDATOR_EVENTS
} from './developer';
 
export class ProviderApplication extends EventEmitter {
  private config: ApplicationConfiguration;
  private transporter: TransactionTransporter;
  private validator: DeveloperKeyValidator;
 
  constructor(config: ApplicationConfiguration) {
    super();
 
    this.config = config;
    this.transporter = new DefaultTransactionTransporter(this.config);
    this.validator = new DefaultDeveloperKeyValidator(this.config);
  }
 
  start(): Promise<this> {
    return new Promise((resolve, reject) => {
      if (this.transporter) {
        // Propagate all transporter events
        for (const event of Object.values(TRANSPORTER_EVENTS)) {
          this.transporter.on(event, data => this.emit(event, data));
        }
        this.transporter.start();
      }
 
      if (this.validator) {
        // Propagate all developer key validator events
        for (const event of Object.values(DEVELOPER_KEY_VALIDATOR_EVENTS)) {
          this.validator.on(event, data => this.emit(event, data));
        }
        this.validator.start();
        this.validator.once(DEVELOPER_KEY_VALIDATOR_EVENTS.UPDATE_SUCCESS, () => {
          resolve(this);
        });
        this.validator.once(DEVELOPER_KEY_VALIDATOR_EVENTS.UPDATE_ERROR, () => {
          reject(this);
        });
      } else {
        resolve(this);
      }
    });
  }
 
  async stop(): Promise<this> {
    if (this.transporter) {
      this.transporter.stop();
      for (const event of Object.values(TRANSPORTER_EVENTS)) {
        this.transporter.removeAllListeners(event);
      }
    }
 
    if (this.validator) {
      this.validator.stop();
      for (const event of Object.values(DEVELOPER_KEY_VALIDATOR_EVENTS)) {
        this.validator.removeAllListeners(event);
      }
    }
 
    return this;
  }
 
  startTransaction(name: string): Transaction {
    const transaction = new Transaction(name, this.config);
    // Propagate all transaction events
    for (const event of Object.values(TRANSACTION_EVENTS)) {
      transaction.on(event, () => this.emit(event, transaction));
    }
    this.transporter.push(transaction);
    return transaction.start();
  }
 
  setTransactionTransporter(transporter: TransactionTransporter): void {
    this.transporter = transporter;
  }
 
  setDeveloperKeyValidator(validator: DeveloperKeyValidator): void {
    this.validator = validator;
  }
 
  isValidDeveloperKey(developerKey: string): boolean {
    return this.validator.isValidDeveloperKey(developerKey);
  }
 
  validateDeveloperKey(developerKey: string): Error | null {
    return this.validator.validateDeveloperKey(developerKey);
  }
}