From a23e7349be8ba8ccc0a4858662e435a2e35b906f Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Fri, 8 Nov 2019 18:41:08 +0100 Subject: [PATCH] fix(core): update --- ts/interfaces/connection.ts | 2 +- ts/smartsocket.classes.smartsocketclient.ts | 13 +++++++++++-- ts/smartsocket.classes.socketconnection.ts | 12 ++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ts/interfaces/connection.ts b/ts/interfaces/connection.ts index 8e79ecb..0a805a8 100644 --- a/ts/interfaces/connection.ts +++ b/ts/interfaces/connection.ts @@ -2,4 +2,4 @@ export interface IRequestAuthPayload { serverShortId: string; } -export type TConnectionEvent = 'terminated' | 'error'; \ No newline at end of file +export type TConnectionStatus = 'new' | 'connecting' | 'connected' | 'disconnecting' | 'disconnected'; diff --git a/ts/smartsocket.classes.smartsocketclient.ts b/ts/smartsocket.classes.smartsocketclient.ts index 722ecb1..3bdbe41 100644 --- a/ts/smartsocket.classes.smartsocketclient.ts +++ b/ts/smartsocket.classes.smartsocketclient.ts @@ -33,7 +33,9 @@ export class SmartsocketClient { public serverPort: number; public autoReconnect: boolean; - public eventSubject = new plugins.smartrx.rxjs.Subject(); + // status handling + public eventSubject = new plugins.smartrx.rxjs.Subject(); + public eventStatus: interfaces.TConnectionStatus = 'new'; public socketFunctions = new plugins.lik.Objectmap>(); public socketRequests = new plugins.lik.Objectmap>(); @@ -135,7 +137,7 @@ export class SmartsocketClient { } defaultLogger.log('warn', `disconnected from server ${this.remoteShortId}`); this.remoteShortId = null; - this.eventSubject.next('terminated'); + this.updateStatus('disconnected'); if (this.autoReconnect) { this.tryDebouncedReconnect(); @@ -170,4 +172,11 @@ export class SmartsocketClient { const result = response.funcDataArg; return result; } + + private updateStatus (statusArg: interfaces.TConnectionStatus) { + if (this.eventStatus !== statusArg) { + this.eventSubject.next(statusArg); + } + this.eventStatus = statusArg; + } } diff --git a/ts/smartsocket.classes.socketconnection.ts b/ts/smartsocket.classes.socketconnection.ts index 6228c46..6e7f4cd 100644 --- a/ts/smartsocket.classes.socketconnection.ts +++ b/ts/smartsocket.classes.socketconnection.ts @@ -55,7 +55,8 @@ export class SocketConnection { public smartsocketRef: Smartsocket | SmartsocketClient; public socket: SocketIO.Socket | SocketIOClient.Socket; - public eventSubject = new plugins.smartrx.rxjs.Subject(); + public eventSubject = new plugins.smartrx.rxjs.Subject(); + public eventStatus: interfaces.TConnectionStatus = 'new'; constructor(optionsArg: ISocketConnectionConstructorOptions) { this.alias = optionsArg.alias; @@ -173,6 +174,13 @@ export class SocketConnection { // disconnecting ---------------------- public async disconnect() { this.socket.disconnect(true); - this.eventSubject.next('terminated'); + this.updateStatus('disconnected'); + } + + private updateStatus (statusArg: interfaces.TConnectionStatus) { + if (this.eventStatus !== statusArg) { + this.eventSubject.next(statusArg); + } + this.eventStatus = statusArg; } }