Files
raven-client/ayanova/src/api/notifypoll.js
2020-07-11 15:03:47 +00:00

54 lines
1.5 KiB
JavaScript

/* xeslint-disable */
let keepPolling = false;
const DEFAULT_POLLING_INTERVAL = 60000;
const MAX_POLLING_INTERVAL = 15 * 60 * 1000; //15 minutes maximum wait time
export default {
async startPolling() {
keepPolling = true;
let pollingInterval = DEFAULT_POLLING_INTERVAL;
try {
while (keepPolling == true) {
let status = await window.$gz.api.get("notify/new-count");
if (status.error) {
throw status.error;
}
window.$gz.store.commit("setNewNotificationCount", status.data);
//success so go to default in case it was changed by an error
pollingInterval = DEFAULT_POLLING_INTERVAL;
await window.$gz.util.sleepAsync(pollingInterval);
}
} catch (error) {
pollingInterval *= 1.5;
if (pollingInterval > MAX_POLLING_INTERVAL) {
pollingInterval = MAX_POLLING_INTERVAL;
}
//could be a expected issue such as server closed for maintenance
//so no need to freak out if that's the case
if (error.code) {
//is it a normal issue?
if (error.code == 2001) {
return;
}
}
//Nope, totally unexpected, alert the user
window.$gz.errorHandler.handleGeneralError(
"Error checking for notifications",
"notifypoll",
null,
null,
error
);
window.$gz.eventBus.$emit(
"notify-error",
"Error checking for notifications, see about->log for details; log out and back in again to reset"
);
}
},
stopPolling() {
keepPolling = false;
}
};