Unverified Commit 5ad20109 authored by Marius van den Beek's avatar Marius van den Beek Committed by GitHub
Browse files

Merge pull request #14264 from dannon/history-watch-throttling

[22.05] History watch throttling -- relax when backgrounded
parents c0e3c232 963ff33a
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -11,8 +11,8 @@ import { getCurrentHistoryFromServer } from "./queries";
import { getGalaxyInstance } from "app";

const limit = 1000;
const throttlePeriod = 3000;

let throttlePeriod = 3000;
let watchTimeout = null;

// last time the history has changed
@@ -21,6 +21,20 @@ let lastUpdateTime = null;
// last time changed history items have been requested
let lastRequestDate = new Date();

// We only want to kick this off once we're actively watching history
let watchingVisibility = false;

function setVisibilityThrottle() {
    if (document.visibilityState === "visible") {
        // Poll every 3 seconds when visible
        throttlePeriod = 3000;
        rewatchHistory();
    } else {
        // Poll every 60 seconds when hidden/backgrounded
        throttlePeriod = 60000;
    }
}

export async function watchHistoryOnce(store) {
    // "Reset" watchTimeout so we don't queue up watchHistory calls in rewatchHistory.
    watchTimeout = null;
@@ -70,6 +84,11 @@ export async function watchHistoryOnce(store) {
}

export async function watchHistory(store = defaultStore) {
    // Only set up visibility listeners once, whenever a watch is first started
    if (watchingVisibility === false) {
        watchingVisibility = true;
        document.addEventListener("visibilitychange", setVisibilityThrottle);
    }
    try {
        await watchHistoryOnce(store);
    } catch (error) {