diff --git a/ts/services/networkObserver.ts b/ts/services/networkObserver.ts index 34f39c80af4d..ef1c383b01d6 100644 --- a/ts/services/networkObserver.ts +++ b/ts/services/networkObserver.ts @@ -27,6 +27,8 @@ export function initializeNetworkObserver( }); }; + window.Whisper.events.on('socketStatusChange', refresh); + window.addEventListener('online', refresh); window.addEventListener('offline', refresh); window.setInterval(refresh, REFRESH_INTERVAL); diff --git a/ts/textsecure/SocketManager.ts b/ts/textsecure/SocketManager.ts index 6fe4d93a801c..1782e3d367c3 100644 --- a/ts/textsecure/SocketManager.ts +++ b/ts/textsecure/SocketManager.ts @@ -128,7 +128,7 @@ export class SocketManager extends EventListener { window.log.info('SocketManager: connecting authenticated socket'); - this.status = SocketStatus.CONNECTING; + this.setStatus(SocketStatus.CONNECTING); const process = this.connectResource({ path: '/v1/websocket/', @@ -185,7 +185,7 @@ export class SocketManager extends EventListener { let authenticated: WebSocketResource; try { authenticated = await process.getResult(); - this.status = SocketStatus.OPEN; + this.setStatus(SocketStatus.OPEN); } catch (error) { window.log.warn( 'SocketManager: authenticated socket connection failed with ' + @@ -407,6 +407,15 @@ export class SocketManager extends EventListener { // Private // + private setStatus(status: SocketStatus): void { + if (this.status === status) { + return; + } + + this.status = status; + this.emit('statusChange'); + } + private async getUnauthenticatedResource(): Promise { if (this.isOffline) { throw new HTTPError('SocketManager offline', { @@ -592,7 +601,7 @@ export class SocketManager extends EventListener { this.incomingRequestQueue = []; this.authenticated = undefined; - this.status = SocketStatus.CLOSED; + this.setStatus(SocketStatus.CLOSED); } private dropUnauthenticated( @@ -707,6 +716,7 @@ export class SocketManager extends EventListener { // EventEmitter types public on(type: 'authError', callback: (error: HTTPError) => void): this; + public on(type: 'statusChange', callback: () => void): this; public on( type: string | symbol, @@ -717,6 +727,7 @@ export class SocketManager extends EventListener { } public emit(type: 'authError', error: HTTPError): boolean; + public emit(type: 'statusChange'): boolean; // eslint-disable-next-line @typescript-eslint/no-explicit-any public emit(type: string | symbol, ...args: Array): boolean { diff --git a/ts/textsecure/WebAPI.ts b/ts/textsecure/WebAPI.ts index da8d1f1708d8..9da2e114c4d1 100644 --- a/ts/textsecure/WebAPI.ts +++ b/ts/textsecure/WebAPI.ts @@ -1093,6 +1093,10 @@ export function initialize({ proxyUrl, }); + socketManager.on('statusChange', () => { + window.Whisper.events.trigger('socketStatusChange'); + }); + socketManager.on('authError', () => { window.Whisper.events.trigger('unlinkAndDisconnect'); });