From 13fe439d9f22f37288730cded6084fb0c380baec Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Fri, 28 Apr 2023 14:02:30 +0530 Subject: [PATCH] Move the reconnect logic from the update presence (#6992) --- .../helpers/BaseActionCableConnector.js | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/app/javascript/shared/helpers/BaseActionCableConnector.js b/app/javascript/shared/helpers/BaseActionCableConnector.js index 44ef0d1bd..eda4316c9 100644 --- a/app/javascript/shared/helpers/BaseActionCableConnector.js +++ b/app/javascript/shared/helpers/BaseActionCableConnector.js @@ -2,6 +2,7 @@ import { createConsumer } from '@rails/actioncable'; import { BUS_EVENTS } from 'shared/constants/busEvents'; const PRESENCE_INTERVAL = 20000; +const RECONNECT_INTERVAL = 1000; class BaseActionCableConnector { static isDisconnected = false; @@ -25,6 +26,7 @@ class BaseActionCableConnector { disconnected: () => { BaseActionCableConnector.isDisconnected = true; this.onDisconnected(); + this.initReconnectTimer(); // TODO: Remove this after completing the conversation list refetching window.bus.$emit(BUS_EVENTS.WEBSOCKET_DISCONNECT); }, @@ -32,11 +34,11 @@ class BaseActionCableConnector { ); this.app = app; this.events = {}; + this.reconnectTimer = null; this.isAValidEvent = () => true; this.triggerPresenceInterval = () => { setTimeout(() => { this.subscription.updatePresence(); - this.checkConnection(); this.triggerPresenceInterval(); }, PRESENCE_INTERVAL); }; @@ -48,11 +50,28 @@ class BaseActionCableConnector { const isReconnected = BaseActionCableConnector.isDisconnected && isConnectionActive; if (isReconnected) { + this.clearReconnectTimer(); this.onReconnect(); BaseActionCableConnector.isDisconnected = false; + } else { + this.initReconnectTimer(); } } + clearReconnectTimer = () => { + if (this.reconnectTimer) { + clearTimeout(this.reconnectTimer); + this.reconnectTimer = null; + } + }; + + initReconnectTimer = () => { + this.clearReconnectTimer(); + this.reconnectTimer = setTimeout(() => { + this.checkConnection(); + }, RECONNECT_INTERVAL); + }; + onReconnect = () => {}; onDisconnected = () => {};