Spaces:
Running
Running
File size: 1,256 Bytes
22cfde9 8cd21d0 22cfde9 8cd21d0 22cfde9 8cd21d0 22cfde9 8cd21d0 22cfde9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | // Track visible work by page so unrelated preloads cannot hold the bar open.
const DeltaLoading = (() => {
const bar = document.getElementById("site-loading");
const slow = document.getElementById("site-slow");
const SLOW_MS = 4000;
const pending = new Set();
let slowTimer = null;
// The Space can take tens of seconds on a first visit, so say so when the bar stays up.
function refresh() {
const page = document.body.dataset.page;
bar.hidden = !Array.from(pending).some(pages => !pages.length || pages.includes(page));
if (bar.hidden) {
clearTimeout(slowTimer);
slowTimer = null;
slow.hidden = true;
} else if (slowTimer === null && slow.hidden) {
slowTimer = setTimeout(() => {
slowTimer = null;
slow.hidden = bar.hidden;
}, SLOW_MS);
}
}
function start(...pages) {
pending.add(pages);
refresh();
return () => {
pending.delete(pages);
refresh();
};
}
function track(promise, ...pages) {
const finish = start(...pages);
return Promise.resolve(promise).finally(finish);
}
if (document.readyState !== "complete") {
window.addEventListener("load", start(), { once: true });
}
return { refresh, start, track };
})();
|