Files
raven-client/ayanova/src/api/notifypoll.js
2020-08-03 23:42:48 +00:00

43 lines
1.3 KiB
JavaScript

/* xeslint-disable */
let keepChecking = false;
const DEFAULT_POLLING_INTERVAL = 60000;
//const DEFAULT_POLLING_INTERVAL = 5000;
const MAX_POLLING_INTERVAL = 10 * 60 * 1000; //10 minutes maximum wait time
export default {
async startPolling() {
if (keepChecking == true) {
return;
}
keepChecking = true;
let pollingInterval = window.$gz.dev ? 20000 : DEFAULT_POLLING_INTERVAL;
let status = null;
while (keepChecking == true) {
try {
await window.$gz.util.sleepAsync(pollingInterval);
if (keepChecking && window.$gz.store.state.authenticated) {
status = await window.$gz.api.get("notify/new-count");
if (status.error) {
throw status.error;
} else {
window.$gz.store.commit("setNewNotificationCount", status.data);
//success so go to default in case it was changed by an error
pollingInterval = window.$gz.dev ? 20000 : DEFAULT_POLLING_INTERVAL;
}
} else {
keepChecking = false;
}
} catch (error) {
pollingInterval *= 1.5;
if (pollingInterval > MAX_POLLING_INTERVAL) {
pollingInterval = MAX_POLLING_INTERVAL;
}
}
}
},
stopPolling() {
keepChecking = false;
}
};