All files / dialer justcall-client-event-emitter.ts

100% Statements 16/16
75% Branches 6/8
100% Functions 8/8
100% Lines 14/14

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                              11x     3x   3x                 5x       2x 1x 1x       2x               13x 6x   7x         1x       1x       1x      
import {
  CallAnsweredEventData,
  CallEndedEventData,
  CallRingingEventData,
  JustCallDialerEmittableEvent,
  JustCallDialerEmittableEventWithData,
  LoggedInEventData,
  LoginCallback,
  LogoutCallback,
} from "../types";
 
import { JustcallDialerErrorCode, handleError } from "../utils/errors";
 
export class JustCallClientEventEmitter {
  private dialerEventListeners: Map<JustCallDialerEmittableEvent, Function> =
    new Map();
 
  private emit(event: JustCallDialerEmittableEventWithData) {
    const listener = this.dialerEventListeners.get(event.name);
    /* istanbul ignore if -- @preserve */
    if (listener) {
      listener(event.data);
    }
  }
 
  public addDialerEventListener(
    event: JustCallDialerEmittableEvent,
    callback: Function
  ) {
    this.dialerEventListeners.set(event, callback);
  }
 
  public unsubscribeFromDialerEvent(event: JustCallDialerEmittableEvent) {
    if (this.dialerEventListeners.has(event)) {
      this.dialerEventListeners.delete(event);
    } else throw handleError(JustcallDialerErrorCode.not_subscribed_to_event);
  }
 
  public unsubscribeAll() {
    this.dialerEventListeners.clear();
  }
 
  public handleLoggedIn(
    data: LoggedInEventData,
    onLogin: LoginCallback | null,
    onLogout: LogoutCallback | null
  ): void {
    if (data.logged_in) {
      Eif (onLogin) onLogin(data);
    } else {
      Eif (onLogout) onLogout();
    }
  }
 
  public handleCallRinging(data: CallRingingEventData): void {
    this.emit({ name: "call-ringing", data });
  }
 
  public handleCallAnswered(data: CallAnsweredEventData): void {
    this.emit({ name: "call-answered", data });
  }
 
  public handleCallEnded(data: CallEndedEventData): void {
    this.emit({ name: "call-ended", data });
  }
}