Skip to content

Commit 3d03b8a

Browse files
committed
Reload page on zapper done and flush pending saves
1 parent eca08df commit 3d03b8a

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

wBlock Advanced/Resources/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24220,6 +24220,7 @@ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e =
2422024220
doneButton.addEventListener("click", (event) => {
2422124221
interceptEvent(event);
2422224222
deactivate({ removeUi: true });
24223+
window.location.reload();
2422324224
});
2422424225

2422524226
const defaultGroup = document.createElement("span");

wBlock Scripts (iOS)/Resources/zapper-content.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,43 @@
4444

4545
let ruleSyncIntervalId = null;
4646
let ruleSyncInFlight = false;
47+
let isFinalizingSession = false;
48+
const pendingSaveOperations = new Set();
49+
50+
function trackPendingSave(operationPromise) {
51+
let trackedPromise;
52+
trackedPromise = Promise.resolve(operationPromise)
53+
.catch(() => {})
54+
.finally(() => {
55+
pendingSaveOperations.delete(trackedPromise);
56+
});
57+
pendingSaveOperations.add(trackedPromise);
58+
return trackedPromise;
59+
}
60+
61+
async function waitForPendingSaves(timeoutMs = 1500) {
62+
if (pendingSaveOperations.size === 0) return;
63+
const pending = Array.from(pendingSaveOperations);
64+
await Promise.race([
65+
Promise.allSettled(pending),
66+
new Promise((resolve) => setTimeout(resolve, timeoutMs))
67+
]);
68+
}
69+
70+
async function finalizeSessionAndReload() {
71+
if (isFinalizingSession) return;
72+
isFinalizingSession = true;
73+
74+
try {
75+
await waitForPendingSaves();
76+
deactivateZapper({ removeUi: true });
77+
window.location.reload();
78+
} catch {
79+
deactivateZapper({ removeUi: true });
80+
} finally {
81+
isFinalizingSession = false;
82+
}
83+
}
4784

4885
function safeHostname() {
4986
try {
@@ -423,9 +460,8 @@
423460
e.stopPropagation();
424461
if (typeof e.stopImmediatePropagation === 'function') e.stopImmediatePropagation();
425462
}
426-
// Delay teardown until after the current input/click sequence completes
427-
// so we don't accidentally retarget the synthetic click to the page.
428-
setTimeout(() => deactivateZapper({ removeUi: true }), 0);
463+
doneButton.disabled = true;
464+
finalizeSessionAndReload().catch(() => {});
429465
};
430466
doneButton.addEventListener('click', onDone);
431467
doneButton.addEventListener('pointerup', onDone, true);
@@ -690,7 +726,7 @@
690726
}
691727
state.rules = state.rules.concat([normalized]).slice(0, MAX_RULES_PER_SITE);
692728
state.undoStack.push(normalized);
693-
await saveRulesForHost(state.host, state.rules);
729+
await trackPendingSave(saveRulesForHost(state.host, state.rules));
694730
applyRulesToPage(state.rules);
695731
if (state.ui.undoButton) state.ui.undoButton.disabled = false;
696732
showToast(options.manual ? 'Rule saved for this site.' : 'Hidden. Rule saved for this site.');
@@ -713,7 +749,7 @@
713749
if (state.undoStack.length === 0) return;
714750
const toRemove = state.undoStack.pop();
715751
state.rules = state.rules.filter((r) => r !== toRemove);
716-
await saveRulesForHost(state.host, state.rules);
752+
await trackPendingSave(saveRulesForHost(state.host, state.rules));
717753
applyRulesToPage(state.rules);
718754
if (state.ui.undoButton) state.ui.undoButton.disabled = state.undoStack.length === 0;
719755
showToast('Undone.');

0 commit comments

Comments
 (0)