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);
$: debouncer.schedule(() => {
$: debouncer.schedule(async () => {
const cache = getCache($pageTheme.isDark, fontSize);
const entry = cache.get(mathjax);
if (entry) {

View file

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