about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/util/stream.js
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-05-27 19:42:45 +0200
committerThibG <thib@sitedethib.com>2018-05-29 21:25:28 +0200
commitcc589d6ec06e562c0bf43c415f816b2b922ebd0c (patch)
tree759271a9439547bbd5e12eddad1f7056164f48a0 /app/javascript/flavours/glitch/util/stream.js
parentfbc25bdd2dfbc0c2316c5bdae6b8da0c047375c2 (diff)
[Glitch] Use randomized setTimeout when fallback-polling and re-add since_id
Port dafd7afc5ef94fc9513efa341910f7e3c31b909a to glitch-soc
Diffstat (limited to 'app/javascript/flavours/glitch/util/stream.js')
-rw-r--r--app/javascript/flavours/glitch/util/stream.js15
1 files changed, 10 insertions, 5 deletions
diff --git a/app/javascript/flavours/glitch/util/stream.js b/app/javascript/flavours/glitch/util/stream.js
index 6c67ba275..9928d0dd7 100644
--- a/app/javascript/flavours/glitch/util/stream.js
+++ b/app/javascript/flavours/glitch/util/stream.js
@@ -1,21 +1,24 @@
 import WebSocketClient from 'websocket.js';
 
+const randomIntUpTo = max => Math.floor(Math.random() * Math.floor(max));
+
 export function connectStream(path, pollingRefresh = null, callbacks = () => ({ onDisconnect() {}, onReceive() {} })) {
   return (dispatch, getState) => {
     const streamingAPIBaseURL = getState().getIn(['meta', 'streaming_api_base_url']);
     const accessToken = getState().getIn(['meta', 'access_token']);
     const { onDisconnect, onReceive } = callbacks(dispatch, getState);
+
     let polling = null;
 
     const setupPolling = () => {
-      polling = setInterval(() => {
-        pollingRefresh(dispatch);
-      }, 20000);
+      pollingRefresh(dispatch, () => {
+        polling = setTimeout(() => setupPolling(), 20000 + randomIntUpTo(20000));
+      });
     };
 
     const clearPolling = () => {
       if (polling) {
-        clearInterval(polling);
+        clearTimeout(polling);
         polling = null;
       }
     };
@@ -29,8 +32,9 @@ export function connectStream(path, pollingRefresh = null, callbacks = () => ({
 
       disconnected () {
         if (pollingRefresh) {
-          setupPolling();
+          polling = setTimeout(() => setupPolling(), randomIntUpTo(40000));
         }
+
         onDisconnect();
       },
 
@@ -51,6 +55,7 @@ export function connectStream(path, pollingRefresh = null, callbacks = () => ({
       if (subscription) {
         subscription.close();
       }
+
       clearPolling();
     };