All files / dialer index.ts

100% Statements 62/62
76.19% Branches 16/21
90.9% Functions 10/11
100% Lines 62/62

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216                                        12x 12x 12x   12x         12x 12x 12x   12x     12x 12x 1x     11x 11x 11x 11x 11x   11x 11x 11x     11x 11x                   11x 1x     10x   10x 2x     8x       8x 8x   8x   8x           8x       8x                 8x                         7x 7x 1x   6x 1x   5x                   2x 2x                   2x 2x                   1x 1x 1x                         1x 1x 1x                                         1x 1x   1x 1x 1x     1x 1x 1x 1x   1x 1x 1x                  
import type {
  JustCallDialerInitProps,
  LoginCallback,
  LogoutCallback,
  JustCallDialerEmittableEvent,
  OnDialerReadyCallback,
} from "../types";
import { JustCallDialerEventListeners } from "./justcall-dialer-event-listener";
import { JustCallClientEventEmitter } from "./justcall-client-event-emitter";
import { JustCallDialerEventEmitter } from "./justcall-dialer-event-emitter";
import getJustcallDialerIframeTempl from "../template/dialer-iframe";
import {
  handleError,
  JustcallDialerErrorCode,
  JustcallDialerError,
} from "../utils/errors";
import { validEmittableEvents } from "../utils/contants";
 
export class JustCallDialer {
  private dialerId: string;
  private dialerDiv: HTMLElement | null = null;
  private dialerIframe: HTMLIFrameElement | null = null;
  private dialerEventListeners: JustCallDialerEventListeners | null = null;
  private clientEventEmitter: JustCallClientEventEmitter;
  private dialerEventEmitter: JustCallDialerEventEmitter | null = null;
  private dialerReadyPromise: Promise<void>;
  private resolveDialerReadyPromise!: () => void;
  private rejectDialerReadyPromise!: () => void;
 
  public onLogin: LoginCallback | null = null;
  public onLogout: LogoutCallback | null = null;
  public onReady: OnDialerReadyCallback | null = null;
 
  public isDialerReady = false;
 
  public constructor(props: JustCallDialerInitProps) {
    try {
      if (typeof window === "undefined") {
        throw handleError(JustcallDialerErrorCode.browser_environment_required);
      }
 
      const { onLogin = null, onLogout = null, dialerId, onReady = null } = props;
      this.onLogin = onLogin;
      this.onLogout = onLogout;
      this.dialerId = dialerId;
      this.onReady = onReady;
 
      this.dialerReadyPromise = new Promise((resolve, reject) => {
        this.resolveDialerReadyPromise = resolve;
        this.rejectDialerReadyPromise = reject;
      });
 
      this.clientEventEmitter = new JustCallClientEventEmitter();
      this.init();
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  private init() {
    if (!this.dialerId) {
      throw handleError(JustcallDialerErrorCode.no_dialer_id);
    }
 
    this.dialerDiv = document.getElementById(this.dialerId);
 
    if (!this.dialerDiv) {
      throw handleError(JustcallDialerErrorCode.dialer_id_not_found);
    }
 
    this.load();
  }
 
  private load() {
    try {
      this.dialerIframe = getJustcallDialerIframeTempl(document);
 
      this.dialerDiv?.appendChild(this.dialerIframe!);
 
      this.dialerEventListeners = new JustCallDialerEventListeners({
        onLogin: this.onLogin,
        onLogout: this.onLogout,
        clientEventEmitter: this.clientEventEmitter,
      });
 
      this.dialerEventEmitter = new JustCallDialerEventEmitter(
        this.dialerIframe
      );
 
      this.dialerEventListeners.startListening();
 
      /* istanbul ignore next -- @preserve */
      this.dialerIframe.onload = () => {
        this.isDialerReady = true;
        if (this.onReady) this?.onReady();
        this.resolveDialerReadyPromise();
      };
 
      this.dialerIframe.onerror = () => {
        /* istanbul ignore next -- @preserve */
        this.rejectDialerReadyPromise();
      };
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  public on(event: JustCallDialerEmittableEvent, callback: Function) {
    try {
      if (!event) {
        throw handleError(JustcallDialerErrorCode.no_event_name);
      }
      if (!validEmittableEvents.includes(event)) {
        throw handleError(JustcallDialerErrorCode.invalid_event_name);
      }
      this.clientEventEmitter.addDialerEventListener(event, callback);
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  public unsubscribe(event: JustCallDialerEmittableEvent) {
    try {
      this.clientEventEmitter.unsubscribeFromDialerEvent(event);
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  public unsubscribeAll() {
    try {
      this.clientEventEmitter.unsubscribeAll();
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  public dialNumber(number: string) {
    try {
      Eif (!this.isDialerReady) {
        throw handleError(JustcallDialerErrorCode.dialer_not_ready);
      }
      /* istanbul ignore next -- @preserve */
      this.dialerEventEmitter!.handleExternalDial(number);
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  public async isLoggedIn() {
    try {
      Eif (!this.isDialerReady) {
        throw handleError(JustcallDialerErrorCode.dialer_not_ready);
      }
      /* istanbul ignore next -- @preserve */
      {
        this.dialerEventEmitter?.handleIsLoggedIn();
        return await this.dialerEventListeners!.awaitForEvent("is-logged-in");
      }
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
 
  /* istanbul ignore next -- @preserve */
  public async ready() {
    return this.dialerReadyPromise;
  }
 
  public destroy() {
    try {
      this.unsubscribeAll();
 
      Eif (this.dialerIframe && this.dialerIframe.parentNode) {
        this.dialerIframe.parentNode.removeChild(this.dialerIframe);
        this.dialerIframe = null;
      }
 
      this.dialerDiv = null;
      this.dialerEventListeners = null;
      this.dialerEventEmitter = null;
      this.isDialerReady = false;
 
      this.onLogin = null;
      this.onLogout = null;
      this.onReady = null;
    } catch (error) {
      /* istanbul ignore next -- @preserve */ {
        if (error instanceof JustcallDialerError) throw error;
        throw handleError(JustcallDialerErrorCode.unknown_error);
      }
    }
  }
}