45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
let keepChecking = false;
|
|
const DEFAULT_POLLING_INTERVAL = 60000;
|
|
const MAX_POLLING_INTERVAL = 30 * 60 * 1000; //30 minutes maximum wait time
|
|
export default {
|
|
async startPolling() {
|
|
if (keepChecking == true) {
|
|
return;
|
|
}
|
|
keepChecking = true;
|
|
//initial delay so it fetches "immediately"
|
|
let pollingInterval = 3000;
|
|
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 new Error(window.$gz.errorHandler.errorToString(status));
|
|
// throw new Error(status.error);
|
|
} else {
|
|
window.$gz.store.commit("setNewNotificationCount", status.data);
|
|
//success so go to default in case it was changed by an error
|
|
pollingInterval = DEFAULT_POLLING_INTERVAL;
|
|
}
|
|
} else {
|
|
keepChecking = false;
|
|
}
|
|
} catch (error) {
|
|
//fixup if fails on very first iteration with initial short polling interval
|
|
if (pollingInterval < DEFAULT_POLLING_INTERVAL) {
|
|
pollingInterval = DEFAULT_POLLING_INTERVAL;
|
|
}
|
|
pollingInterval *= 1.5;
|
|
if (pollingInterval > MAX_POLLING_INTERVAL) {
|
|
pollingInterval = MAX_POLLING_INTERVAL;
|
|
}
|
|
}
|
|
}
|
|
},
|
|
stopPolling() {
|
|
keepChecking = false;
|
|
}
|
|
};
|