Make CooldownTimer accept an aync action

This commit is contained in:
Abdo 2025-10-19 06:10:23 +03:00
parent 6cab377083
commit 224ea3abb1
2 changed files with 4 additions and 5 deletions

View file

@ -47,7 +47,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const debouncer = new CooldownTimer(500); const debouncer = new CooldownTimer(500);
$: debouncer.schedule(() => { $: debouncer.schedule(async () => {
const cache = getCache($pageTheme.isDark, fontSize); const cache = getCache($pageTheme.isDark, fontSize);
const entry = cache.get(mathjax); const entry = cache.get(mathjax);
if (entry) { if (entry) {

View file

@ -3,20 +3,19 @@
export class CooldownTimer { export class CooldownTimer {
private executing = false; private executing = false;
private queuedAction: (() => void) | null = null; private queuedAction: (() => Promise<void>) | null = null;
private delay: number; private delay: number;
constructor(delayMs: number) { constructor(delayMs: number) {
this.delay = delayMs; this.delay = delayMs;
} }
schedule(action: () => void): void { schedule(action: () => Promise<void>): void {
if (this.executing) { if (this.executing) {
this.queuedAction = action; this.queuedAction = action;
} else { } else {
this.executing = true; this.executing = true;
action(); action().then(() => setTimeout(this.#pop.bind(this), this.delay));
setTimeout(this.#pop.bind(this), this.delay);
} }
} }