UNPKG

detritus-client-socket

Version:

A TypeScript NodeJS library to interact with Discord's Gateway

73 lines (72 loc) 1.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Bucket = void 0; const detritus_utils_1 = require("detritus-utils"); class Bucket { constructor(limit = 0, delay = 0) { this.timeout = new detritus_utils_1.Timers.Timeout(); this.delay = delay; this.limit = limit; this.locked = false; this.queue = []; this.sent = { amount: 0, reset: 0, }; Object.defineProperties(this, { timeout: { enumerable: false }, }); } add(throttled, unshift = false) { if (unshift) { this.queue.unshift(throttled); } else { this.queue.push(throttled); } this.shift(); } clear() { this.queue.length = 0; } lock(unlockIn = 0) { this.timeout.stop(); this.locked = true; if (unlockIn) { this.timeout.start(unlockIn, () => { this.unlock(); }); } } shift() { if (this.locked) { return; } if (!this.queue.length) { return; } if (this.limit) { const now = Date.now(); if (this.sent.reset + this.delay <= now) { this.sent.reset = now; this.sent.amount = 0; } if (this.limit <= ++this.sent.amount) { const diff = Math.max(this.delay - (now - this.sent.reset), 0); if (diff) { this.lock(diff); } } } const throttled = this.queue.shift(); if (throttled) { throttled(); this.shift(); } } unlock() { this.locked = false; this.shift(); } } exports.Bucket = Bucket;