From 4b863d5d2ee47818c3e508ee64e4af1f33250124 Mon Sep 17 00:00:00 2001 From: Sulthan Zaki Date: Mon, 27 Jul 2026 17:11:26 +0700 Subject: [PATCH] feat(userscript): drain the retry queue on navigate and on reconnect --- userscript/manga-bookmark.user.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/userscript/manga-bookmark.user.js b/userscript/manga-bookmark.user.js index 8a5a2fb..fb0c8dd 100644 --- a/userscript/manga-bookmark.user.js +++ b/userscript/manga-bookmark.user.js @@ -440,6 +440,31 @@ saveQueue(queue); } + let draining = false; + + // Replays everything owed. Cheap in the normal case — it returns on the first + // line when the queue is empty, which is why it can hang off navigation. A + // 401 stops the whole pass: the token is wrong, so the next entry would fail + // the same way, and the queue is left intact so fixing the token fixes it. + async function drain() { + if (draining || queue.length === 0) return; + draining = true; + authFailed = false; + try { + for (const e of queue.slice()) { + if (e.op === "delete") await pushDelete(e.key); + else await pushBookmark(e.key, e.sendStatus); + if (authFailed) { + toast("Sync auth failed — check the token", true); + break; + } + } + } finally { + draining = false; + } + render(); + } + // ============================================================ // Mutations (optimistic: update UI/cache first, then sync) // ============================================================ @@ -1018,6 +1043,9 @@ // Asura is client-routed, so full loads are rare — checking here too is // what keeps its bookmarks current. The throttle still caps the rate. backgroundRefreshLatest(); + // Drain only, not a full refresh: Asura may go a long time without a + // reload, and an extra GET per client-side route change is not wanted. + drain(); } // Framework-agnostic URL-change watcher: patch history + poll as a fallback, @@ -1053,6 +1081,7 @@ state.page = detect(); render(); installNavWatcher(); + window.addEventListener("online", drain); // signal returned while the page stayed open // Sync first: both auto-record and the latest-chapter checks below need to // know which series are bookmarked and how fresh they are. refresh().then(() => {