0
0
Fork 0

Use randomized setTimeout when fallback-polling and re-add since_id (#7522)

This commit is contained in:
Eugen Rochko 2018-05-18 02:32:35 +02:00 committed by GitHub
parent 1e02dc8715
commit dafd7afc5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 19 deletions

View file

@ -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();
};