@wilcosp/rex
Version:
Rex is an automated command manager for discord js
67 lines (66 loc) • 1.85 kB
JavaScript
"use strict";
/*!
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RexAsyncDefferedQueue = void 0;
const promises_1 = require("node:timers/promises");
class RexAsyncDefferedQueue {
get ended() {
return this._ended;
}
constructor(delay = 1) {
this.delay = delay;
this.waiting = [];
this._ended = false;
this.interval = setInterval(() => this.check(), this.delay * 1000);
}
check() {
if (this.waiting?.length > 0) {
clearInterval(this.interval);
this.next();
}
}
next() {
if (this.waiting?.length > 0) {
const first = this.waiting.shift();
first.resolve();
return;
}
if (!this._ended) {
this.interval = setInterval(() => this.check(), this.delay * 1000);
}
}
get size() {
return this.waiting.length;
}
wait() {
let res;
const prom = new Promise(resolve => {
res = resolve;
});
this.waiting.push({
promise: prom,
resolve: res,
});
return prom;
}
async end() {
clearInterval(this.interval);
this._ended = true;
let prom;
while ((prom = this.waiting.shift())) {
prom.resolve();
await (0, promises_1.setTimeout)(1000);
}
}
restart() {
if (this._ended) {
this._ended = false;
this.interval = setInterval(() => this.check(), this.delay * 1000);
}
}
}
exports.RexAsyncDefferedQueue = RexAsyncDefferedQueue;