reduce api calls to really just check every 5 seconds

This commit is contained in:
Matthias 2021-11-30 17:34:10 +01:00
parent 1d123827bc
commit 92c3eaef8f
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
2 changed files with 17 additions and 7 deletions

View File

@ -218,11 +218,12 @@ function inventreeDocReady() {
// Display any cached alert messages
showCachedAlerts();
// Start notification background worker to check every 5 seconds if notifications are available
setInterval(notificationCheck, 5000);
// Start notification background worker to check every 1 seconds if notifications are available
setInterval(notificationCheck, 1000);
// also run when the focus returns
$(document).on('focus', function(e){
notificationCheck();
notificationCheck(force = true);
});
$('#offcanvasRight').on('show.bs.offcanvas', openNotificationPanel) // listener for opening the notification panel

View File

@ -119,14 +119,20 @@ function showMessage(message, options={}) {
});
}
var notificationUpdateTic = 0;
/**
* The notification checker is initiated when the document is loaded. It checks if there are unread notifications
* if unread messages exist the alert flag is raised by making it visible
**/
function notificationCheck() {
// only refresh state if in focus
if (document.hasFocus()) {
function notificationCheck(force = false) {
notificationUpdateTic = notificationUpdateTic + 1;
console.log(notificationUpdateTic);
// refresh if forced or
// if in focus and was not refreshed in the last 5 seconds
if (force || (document.hasFocus() && notificationUpdateTic >= 5)) {
notificationUpdateTic = 0;
inventreeGet(
'/api/notifications/',
{
@ -262,6 +268,9 @@ function closeNotificationPanel() {
* updates the notification counter
**/
function updateNotificationIndicator(count) {
// reset update Ticker -> safe some API bandwidth
notificationUpdateTic = 0;
if (count == 0) {
$("#notification-alert").addClass("d-none");
} else {