All files / src/sockets inner-socket.ts

100% Statements 46/46
100% Branches 8/8
100% Functions 14/14
100% Lines 46/46

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 11819x         19x         6x   6x       6x   6x     6x 6x 6x 6x       7x 4x 4x           7x 2x 2x           7x 3x 3x 1x   3x       3x 3x       3x     7x 1x 1x 1x       1x 1x 1x       7x       1x       1x 1x 1x       3x       2x       2x 2x 2x       4x 4x         1x       14x 4x        
import { Logger } from '../logger';
import { IDictionary, INewable } from '../utils';
import { ISocketClient } from './interfaces';
import { SocketClient } from './socket-client';
 
export class InnerSocket {
	private connection: WebSocket;
 
	private client: ISocketClient;
 
	private isConnectionOpen = false;
 
	private runningRetry = false;
 
	private hostValue: string;
 
	private listeners: IDictionary<(data: any) => void> = {};
 
	private shouldReconnect = true;
 
	constructor(socketClient: INewable<SocketClient>, host: string) {
		this.connection = new WebSocket(host);
		this.client = new socketClient(); // eslint-disable-line new-cap
		this.hostValue = host;
		this.setup();
	}
 
	private setup() {
		this.connection.onopen = () => {
			this.isConnectionOpen = true;
			this.notify({
				event: 'onOpen',
				data: true,
			});
		};
 
		this.connection.onerror = () => {
			Logger.error('WebSocket error');
			this.notify({
				event: 'onError',
				data: true,
			});
		};
 
		this.connection.onmessage = (event: MessageEvent) => {
			const message = JSON.parse(event.data);
			if (this.client[message.event] && typeof this.client[message.event] === 'function') {
				this.client[message.event](message.data);
			}
			let parse = {
				event: 'onMessage',
				data: message.data,
			};
			this.notify(parse);
			parse = {
				event: message.event,
				data: message.data,
			};
			this.notify(parse);
		};
 
		this.connection.onclose = () => {
			this.isConnectionOpen = false;
			this.runningRetry = false;
			this.notify({
				event: 'connectionLost',
				data: true,
			});
			if (this.shouldReconnect && !this.runningRetry) {
				this.runningRetry = true;
				setTimeout(this.retryConnection.bind(this), 500);
			}
		};
 
		this.shouldReconnect = true;
	}
 
	private retryConnection() {
		this.notify({
			event: 'retryConnection',
			data: true,
		});
		this.connection = new WebSocket(this.hostValue);
		this.setup();
		this.runningRetry = false;
	}
 
	public send(message: any) {
		this.connection.send(message);
	}
 
	public isOpen() {
		return this.isConnectionOpen;
	}
 
	public close(reconnect = false) {
		this.shouldReconnect = reconnect;
		this.connection.close();
		this.isConnectionOpen = false;
	}
 
	public addListener(listener: IDictionary<(data: any) => void>) {
		Object.keys(listener).forEach((key) => {
			this.listeners[key] = listener[key];
		});
	}
 
	public removeListener(listenerKey: string) {
		delete this.listeners[listenerKey];
	}
 
	private notify(message: any) {
		if (this.listeners[message.event]) {
			this.listeners[message.event](message.data);
		}
	}
}