5180 fichiers 38.5 Mo 0 téléchargements 199 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / api / utils / TrailingThrottle.ts
TrailingThrottle.ts 1.3 Ko Télécharger le fichier
/**
 * Runs at most one task per interval and never loses the last one: a task asked for too soon waits for the end of
 * the interval, and a newer one replaces it. For requests the server rate-limits and silently drops - answering
 * none of a burst and then the first one, while the screen waits for the last.
 */
export class TrailingThrottle {
    private lastRun = Number.NEGATIVE_INFINITY;
    private timer: ReturnType<typeof setTimeout> = null;
    private pending: (() => void) | null = null;

    constructor(
        private readonly intervalMs: number,
        private readonly now: () => number = () => Date.now()
    ) {}

    public run(task: () => void): void {
        const wait = this.lastRun + this.intervalMs - this.now();

        if (wait <= 0 && !this.timer) {
            this.lastRun = this.now();
            task();
            return;
        }

        this.pending = task;

        if (this.timer) return;

        this.timer = setTimeout(
            () => {
                const next = this.pending;

                this.timer = null;
                this.pending = null;
                this.lastRun = this.now();
                next?.();
            },
            Math.max(0, wait)
        );
    }

    public cancel(): void {
        if (this.timer) clearTimeout(this.timer);

        this.timer = null;
        this.pending = null;
    }
}
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser