From 89562b11b09e96cb275502a92169e8b8dc3f7256 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Mon, 22 Jul 2019 22:43:25 -0600 Subject: [PATCH] Re-auth on visibility change --- client/sprinklersRpc/WebSocketRpcClient.ts | 4 ++++ client/state/AppState.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/client/sprinklersRpc/WebSocketRpcClient.ts b/client/sprinklersRpc/WebSocketRpcClient.ts index 3c8ddc8..08bfe7f 100644 --- a/client/sprinklersRpc/WebSocketRpcClient.ts +++ b/client/sprinklersRpc/WebSocketRpcClient.ts @@ -92,6 +92,10 @@ export class WebSocketRpcClient extends s.SprinklersRPC { this._connect(); } + reconnect() { + this._connect(); + } + stop() { if (this.reconnectTimer != null) { clearTimeout(this.reconnectTimer); diff --git a/client/state/AppState.ts b/client/state/AppState.ts index 089f3c7..723796e 100644 --- a/client/state/AppState.ts +++ b/client/state/AppState.ts @@ -38,8 +38,16 @@ export default class AppState extends TypedEventEmitter { when(() => !this.tokenStore.accessToken.isValid, this.checkToken); this.sprinklersRpc.start(); }); + + document.addEventListener("visibilitychange", this.onPageFocus); } + onPageFocus = () => { + if (document.visibilityState === "visible") { + this.sprinklersRpc.reconnect(); + } + }; + @computed get isLoggedIn() { return this.tokenStore.accessToken.isValid;