Skip to content

Commit

Permalink
Reduced polling spam while tab isn't visible
Browse files Browse the repository at this point in the history
  • Loading branch information
Selbi182 committed Oct 3, 2023
1 parent d945afa commit 0c9dcb8
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/resources/static/js/spotify-big-picture.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const INFO_URL = "/playback-info";
window.addEventListener('load', entryPoint);

function entryPoint() {
pollingLoop();
startPollingLoop();
}

function singleRequest(forceUpdate) {
Expand Down Expand Up @@ -131,16 +131,23 @@ const POLLING_INTERVAL_IDLE_MS = 60 * 1000;
let pollingRetryAttempt = 0;
const MAX_POLLING_RETRY_ATTEMPT = 5;

let pollTimeout;

function startPollingLoop() {
clearTimeout(pollTimeout);
pollingLoop();
}

function pollingLoop() {
singleRequest()
.then(success => calculateNextPollingTimeout(success))
.then(pollingMs => setTimeout(pollingLoop, parseInt(pollingMs.toString())));
.then(pollingMs => pollTimeout = setTimeout(pollingLoop, parseInt(pollingMs.toString())));
}

function calculateNextPollingTimeout(success) {
if (success) {
pollingRetryAttempt = 0;
if (!idle) {
if (!idle && isTabVisible()) {
if (!currentData.playbackContext.paused) {
let timeCurrent = currentData.currentlyPlaying.timeCurrent;
let timeTotal = currentData.currentlyPlaying.timeTotal;
Expand All @@ -158,6 +165,9 @@ function calculateNextPollingTimeout(success) {
return retryTimeoutMs;
}

function isTabVisible() {
return document.visibilityState === "visible";
}

///////////////////////////////
// MAIN DISPLAY STUFF
Expand Down Expand Up @@ -185,7 +195,7 @@ function processJson(changes) {
if (currentData.deployTime > 0 && getChange(changes, "deployTime").wasChanged) {
reloadPage();
} else {
if (document.visibilityState === "visible") {
if (isTabVisible()) {
updateExternallyToggledPreferences(changes)
.then(() => changeImage(changes))
.then(() => prerenderNextImage(changes))
Expand Down Expand Up @@ -1315,7 +1325,7 @@ function updateWebsiteTitle(changes) {
}
}
}
if (document.title !== newTitle && document.visibilityState === "visible") {
if (isTabVisible() && document.title !== newTitle) {
document.title = newTitle;
}
}
Expand Down Expand Up @@ -1939,7 +1949,7 @@ function clearLocalStoragePortraitModePresetPromptPreference() {
window.onresize = () => {
clearTimeout(refreshBackgroundEvent);
refreshBackgroundEvent = setTimeout(() => {
if (document.visibilityState === "visible") {
if (isTabVisible()) {
portraitModePresetSwitchPrompt();
}
refreshAll();
Expand Down Expand Up @@ -2280,8 +2290,8 @@ function generatePresetThumbnail() {
}

document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
singleRequest(true).then();
if (isTabVisible()) {
startPollingLoop();
} else {
markWebsiteTitleAsIdle();
}
Expand Down

0 comments on commit 0c9dcb8

Please sign in to comment.