diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2018-05-18 02:32:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-18 02:32:35 +0200 |
commit | dafd7afc5ef94fc9513efa341910f7e3c31b909a (patch) | |
tree | 518bc8f9b69cd7d9dd50dcdf3b81c7d561f42cb9 /app/javascript/mastodon/stream.js | |
parent | 1e02dc871533de78174b48a6a527f11b0f2bc7ec (diff) |
Use randomized setTimeout when fallback-polling and re-add since_id (#7522)
Diffstat (limited to 'app/javascript/mastodon/stream.js')
-rw-r--r-- | app/javascript/mastodon/stream.js | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/app/javascript/mastodon/stream.js b/app/javascript/mastodon/stream.js index 6c67ba275..9928d0dd7 100644 --- a/app/javascript/mastodon/stream.js +++ b/app/javascript/mastodon/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(); }; |