5180 fichiers 38.5 Mo 0 téléchargements 262 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / hooks / inventory / badgeWearSender.ts
badgeWearSender.ts 1.2 Ko Télécharger le fichier
/** How long a change to the worn badges waits for the next one before the set is sent. */
export const BADGE_WEAR_SEND_DELAY_MS = 300;

export interface TrailingSender<T> {
    /** Keeps the newest value and sends it once no other arrives within the delay. */
    push(value: T): void;
    /** Sends the waiting value now, if there is one. */
    flush(): void;
    isPending(): boolean;
}

/**
 * Sends only the last of a quick run of values. Wearing badges one click after another sent a packet per click, and
 * the server's flood guard dropped the later ones, so the final set was lost.
 */
export const createTrailingSender = <T>(send: (value: T) => void, delayMs = BADGE_WEAR_SEND_DELAY_MS): TrailingSender<T> => {
    let timer: ReturnType<typeof setTimeout> | null = null;
    let waiting: { value: T } | null = null;

    const flush = () => {
        if (timer) clearTimeout(timer);
        timer = null;

        if (!waiting) return;

        const { value } = waiting;
        waiting = null;
        send(value);
    };

    return {
        push(value) {
            waiting = { value };

            if (timer) clearTimeout(timer);
            timer = setTimeout(flush, delayMs);
        },
        flush,
        isPending: () => waiting !== 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