|
| 1 | +import { Subject } from 'rxjs'; |
| 2 | +import type { Socket } from 'socket.io-client'; |
| 3 | + |
| 4 | +import { ErrorCallback } from '../common/types/callbacks.types'; |
| 5 | + |
| 6 | +import { ClientState, ConnectionState, SocketErrorEvent, SocketEvent } from './types'; |
| 7 | +import { Logger } from '../../../common/utils'; |
| 8 | + |
| 9 | +export class ClientConnection { |
| 10 | + private logger: Logger; |
| 11 | + private stateObserver: Subject<ConnectionState>; |
| 12 | + |
| 13 | + public state: ClientState; |
| 14 | + |
| 15 | + constructor(private socket: Socket) { |
| 16 | + this.logger = new Logger('@superviz/socket-client/connection'); |
| 17 | + this.subscribeToManagerEvents(); |
| 18 | + this.stateObserver = new Subject(); |
| 19 | + } |
| 20 | + |
| 21 | + public on(next: (state: ConnectionState) => void, error?: ErrorCallback) { |
| 22 | + if (this.stateObserver.closed) { |
| 23 | + this.stateObserver = new Subject(); |
| 24 | + } |
| 25 | + |
| 26 | + this.stateObserver.subscribe({ |
| 27 | + next, |
| 28 | + error, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + public off() { |
| 33 | + if (this.stateObserver.closed) return; |
| 34 | + |
| 35 | + this.stateObserver.unsubscribe(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @function subscribeToManagerEvents |
| 40 | + * @description Subscribe to the manager events |
| 41 | + * @returns {void} |
| 42 | + */ |
| 43 | + private subscribeToManagerEvents(): void { |
| 44 | + this.socket.on('connect', this.onConnect); |
| 45 | + this.socket.on('disconnect', this.onDisconnect); |
| 46 | + this.socket.on('connect_error', this.onConnectError); |
| 47 | + this.socket.io.on('error', this.onConnectionError); |
| 48 | + this.socket.io.on('reconnect', this.onReconnect); |
| 49 | + this.socket.io.on('reconnect_attempt', this.onReconnecAttempt); |
| 50 | + this.socket.io.on('reconnect_error', this.onReconnectError); |
| 51 | + this.socket.io.on('reconnect_failed', this.onReconnectFailed); |
| 52 | + |
| 53 | + // custom validations listener |
| 54 | + this.socket.on(SocketEvent.ERROR, this.onCustomError); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @function changeState |
| 59 | + * @description Change the state of the connection |
| 60 | + * @returns {void} |
| 61 | + */ |
| 62 | + private changeState(state: ClientState, reason?: string): void { |
| 63 | + this.state = state; |
| 64 | + |
| 65 | + if (this.stateObserver.closed) return; |
| 66 | + |
| 67 | + this.stateObserver.next({ |
| 68 | + state, |
| 69 | + reason, |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + /** Manager events handlers */ |
| 74 | + |
| 75 | + private onConnect = () => { |
| 76 | + this.logger.log('connection @ on connect', 'Connected to the socket'); |
| 77 | + this.changeState(ClientState.CONNECTED); |
| 78 | + }; |
| 79 | + |
| 80 | + private onDisconnect = (reason: Socket.DisconnectReason) => { |
| 81 | + this.logger.log('connection @ on disconnect', 'Disconnected from the socket'); |
| 82 | + this.changeState(ClientState.DISCONNECTED, reason); |
| 83 | + }; |
| 84 | + |
| 85 | + private onConnectError = (error: Error) => { |
| 86 | + this.logger.log('connection @ on connect error', 'Connection error', error); |
| 87 | + this.changeState(ClientState.CONNECTION_ERROR, error.message); |
| 88 | + }; |
| 89 | + |
| 90 | + private onConnectionError = (error: Error) => { |
| 91 | + this.logger.log('connection @ on connection error', 'Connection error', error); |
| 92 | + this.changeState(ClientState.CONNECTION_ERROR, error.message); |
| 93 | + }; |
| 94 | + |
| 95 | + private onReconnect = () => { |
| 96 | + this.logger.log('connection @ on reconnect', 'Reconnected to the socket'); |
| 97 | + this.changeState(ClientState.CONNECTED); |
| 98 | + }; |
| 99 | + |
| 100 | + private onReconnectError = (error: Error) => { |
| 101 | + this.logger.log('connection @ on reconnect error', 'Reconnect error', error); |
| 102 | + this.changeState(ClientState.RECONNECT_ERROR, error.message); |
| 103 | + }; |
| 104 | + |
| 105 | + private onReconnectFailed = () => { |
| 106 | + this.logger.log('connection @ on reconnect failed', 'Failed to reconnect to the socket'); |
| 107 | + this.changeState(ClientState.RECONNECT_ERROR); |
| 108 | + }; |
| 109 | + |
| 110 | + private onReconnecAttempt = (attempt: number) => { |
| 111 | + this.logger.log('connection @ on reconnect attempt', `Reconnect attempt #${attempt}`); |
| 112 | + this.changeState(ClientState.RECONNECTING, `Reconnect attempt #${attempt}`); |
| 113 | + }; |
| 114 | + |
| 115 | + private onCustomError = (error: SocketErrorEvent) => { |
| 116 | + if (error.needsToDisconnect) { |
| 117 | + this.socket.disconnect(); |
| 118 | + } |
| 119 | + |
| 120 | + if (error.level === 'error') { |
| 121 | + console.error('[SuperViz - Error]', 'Type: ', error.errorType, 'Message :', error.message); |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + console.warn('[SuperViz - Warning]', 'Type: ', error.errorType, 'Message :', error.message); |
| 126 | + }; |
| 127 | +} |
0 commit comments