Cancel previous request to avoid stale result (#7)

Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
This commit is contained in:
秀吉
2026-08-20 14:23:24 +01:00
committed by GitHub
parent 3817a3dbc0
commit 2501731b52
+8
View File
@@ -7,6 +7,9 @@ const status = document.querySelector("#status");
const rawResultsWrapper = document.querySelector("#rawResultsWrapper"); const rawResultsWrapper = document.querySelector("#rawResultsWrapper");
const rawResults = document.querySelector("#rawResults"); const rawResults = document.querySelector("#rawResults");
// Keep the current request so a newer search can cancel the older one
let inflight = null;
// Parse URL query parameters // Parse URL query parameters
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
const user = urlParams.get('user'); const user = urlParams.get('user');
@@ -20,6 +23,9 @@ if (user) {
function find() { function find() {
let user = gh.value.trim().toLowerCase(); let user = gh.value.trim().toLowerCase();
if (user != "") { if (user != "") {
// Cancel the previous request so an older slower response can't overwrite a newer one
if (inflight) inflight.abort();
inflight = new AbortController();
statusUpdate(`Fetching devstats score for '${user}'`, "info"); statusUpdate(`Fetching devstats score for '${user}'`, "info");
rawResultsWrapper.classList.add('hidden'); rawResultsWrapper.classList.add('hidden');
fetch("https://devstats.cncf.io/api/v1", { fetch("https://devstats.cncf.io/api/v1", {
@@ -30,6 +36,7 @@ function find() {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
redirect: "follow", redirect: "follow",
signal: inflight.signal,
body: "{\"api\":\"GithubIDContributions\",\"payload\":{\"github_id\":\"" + user + "\"}}", body: "{\"api\":\"GithubIDContributions\",\"payload\":{\"github_id\":\"" + user + "\"}}",
}) })
.then(res => res.json()) .then(res => res.json())
@@ -45,6 +52,7 @@ function find() {
rawResultsWrapper.classList.remove('hidden'); rawResultsWrapper.classList.remove('hidden');
}) })
.catch(err => { .catch(err => {
if (err.name === "AbortError") return; // request was replaced by a newer one
statusUpdate(`Failed to get devstats score for '${user}'`, "error"); statusUpdate(`Failed to get devstats score for '${user}'`, "error");
console.log(err); console.log(err);
}); });