Only automatically update when page not being viewed

This commit is contained in:
Marcus Noble 2021-05-15 11:31:55 +01:00
parent a21dd6b74f
commit 7613116bf3

View File

@ -450,27 +450,39 @@
} }
}; };
let inView = true;
window.onfocus = window.onblur = window.onpageshow = window.onpagehide = function (e) {
if ({focus:1, pageshow:1}[e.type]) {
if (inView) return;
inView = true;
} else if (inView) {
inView = false;
}
};
// Fetch updates every 5 minutes // Fetch updates every 5 minutes
setInterval(() => { setInterval(() => {
fetch(`/api/unread`) if (!inView) {
.then(res => res.json()) fetch(`/api/unread`)
.then(items => { .then(res => res.json())
if (!this.showRead) { .then(items => {
if (this.selectedItem && !items.some(i => i.ID == this.selectedItem)) { if (!this.showRead) {
items.unshift(this.items.find(i => i.ID == this.selectedItem)); if (this.selectedItem && !items.some(i => i.ID == this.selectedItem)) {
} items.unshift(this.items.find(i => i.ID == this.selectedItem));
}
this.items = items; this.items = items;
} else { } else {
for (let item of items) { for (let item of items) {
if (!this.items.some(i => i.ID == item.ID)) { if (!this.items.some(i => i.ID == item.ID)) {
this.items.unshift(item); this.items.unshift(item);
}
} }
} }
}
this.setPageTitle(); this.setPageTitle();
}) });
}
}, 5 * 60 * 1000); }, 5 * 60 * 1000);
document.addEventListener('keydown', this._keyListener.bind(this)); document.addEventListener('keydown', this._keyListener.bind(this));