seyfert
Version:
The most advanced framework for discord bots
72 lines (71 loc) • 2.06 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectQueue = exports.ConnectTimeout = void 0;
class ConnectTimeout {
intervalTime;
promises = [];
interval = undefined;
constructor(intervalTime = 5000) {
this.intervalTime = intervalTime;
}
wait() {
return new Promise(res => {
if (!this.promises.length) {
this.interval = setInterval(() => {
this.shift();
}, this.intervalTime);
res(true);
}
this.promises.push(res);
});
}
shift() {
this.promises.shift()?.(true);
if (!this.promises.length) {
clearInterval(this.interval);
this.interval = undefined;
}
}
}
exports.ConnectTimeout = ConnectTimeout;
class ConnectQueue {
intervalTime;
concurrency;
queue = [];
remaining = 0;
interval = undefined;
constructor(intervalTime = 5000, concurrency = 1) {
this.intervalTime = intervalTime;
this.concurrency = concurrency;
this.remaining = concurrency;
}
push(callback) {
if (this.remaining === 0)
return this.queue.push(callback);
this.remaining--;
if (!this.interval) {
this.startInterval();
}
if (this.queue.length < this.concurrency) {
return callback();
}
return this.queue.push(callback);
}
startInterval() {
this.interval = setInterval(() => {
let cb;
while (this.queue.length && !(cb = this.queue.shift())) {
//
}
if (cb)
return cb?.();
if (this.remaining < this.concurrency)
return this.remaining++;
if (!this.queue.length) {
clearInterval(this.interval);
this.interval = undefined;
}
}, this.intervalTime / this.concurrency);
}
}
exports.ConnectQueue = ConnectQueue;