diff options
author | tdro <tdro@noreply.example.com> | 2024-03-17 18:59:31 -0400 |
---|---|---|
committer | tdro <tdro@noreply.example.com> | 2024-03-17 18:59:31 -0400 |
commit | 8f3c9d30798b71e90a1d34e49e333560b836652f (patch) | |
tree | 13849a206c8a8ee1111670699a2b8d7e75ddc653 /static | |
parent | 2449925e0df0ab9c884295e7b55b8828c39c4204 (diff) | |
download | canory-8f3c9d30798b71e90a1d34e49e333560b836652f.tar.gz canory-8f3c9d30798b71e90a1d34e49e333560b836652f.tar.bz2 canory-8f3c9d30798b71e90a1d34e49e333560b836652f.zip |
static/js/update: Handle more drift
Make a bit smarter
Diffstat (limited to 'static')
-rw-r--r-- | static/js/update.ts | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/static/js/update.ts b/static/js/update.ts index 27b13b5..38d5925 100644 --- a/static/js/update.ts +++ b/static/js/update.ts @@ -15,15 +15,18 @@ function update() { const url = self.location.href.split("#")[0].split("?")[0]; + + const indicator = document.querySelector("a[data-update]"); + if (indicator.dataset.update === "refresh" || indicator === null) return; + const anchor = indicator.cloneNode(); + fetch(url, "HEAD", function (request) { - const local = document.querySelector('meta[name="last-modified"]').content; - const remote = request.getResponseHeader("last-modified") || ''; - const modified = Date.parse(document.lastModified) !== Date.parse(remote); - const drift = Date.parse(remote || local) - Date.parse(local); + const local = document.querySelector('meta[name="last-modified"]').content || document.lastModified; + const remote = request.getResponseHeader("last-modified") || document.lastModified; + const modified = Date.parse(remote) > Date.parse(local); + const drift = Date.parse(remote) - Date.parse(local); - let indicator = document.querySelector("a[data-update]"); - if (indicator === null) return; - const anchor = indicator.cloneNode(); + if (drift < 3000) return; function reset() { indicator.href = anchor.href; @@ -31,14 +34,11 @@ indicator.dataset.update = anchor.dataset.update; } - if (drift === 0) return; - if (remote && modified) { fetch(url, "GET"); indicator.href = url; indicator.removeAttribute("id"); indicator.dataset.update = "refresh"; - self.removeEventListener("blur", update); console.log("INFO: R: " + remote); console.log("INFO: L: " + local); console.log("INFO: D: " + drift); @@ -47,5 +47,16 @@ }); } - self.addEventListener("blur", update); + let scrolled; + let delay = 1000; + let delayed = 0; + + self.addEventListener("scroll", function () { + if (scrolled) clearTimeout(scrolled); + scrolled = setTimeout(function () { update(); delay = delay + delayed; delayed = delay - delayed; }, delay); + }); + + ["focus", "load", "URLChangedCustomEvent"].forEach(function (event) { + self.addEventListener(event, function () { update(); }); + }); })(); |