discord-hybrid-sharding
Version:
The first package which combines sharding manager & internal sharding to save a lot of resources, which allows clustering!
77 lines (76 loc) • 1.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Queue = void 0;
const Util_1 = require("../Util/Util");
class Queue {
queue;
options;
paused;
constructor(options) {
this.options = options;
this.queue = [];
this.paused = false;
}
/**
* Starts the queue and run's the item functions
*/
async start() {
if (!this.options.auto) {
return new Promise(resolve => {
const interval = setInterval(() => {
if (this.queue.length === 0) {
clearInterval(interval);
resolve('Queue finished');
}
}, 200);
});
}
const length = this.queue.length;
for (let i = 0; i < length; i++) {
if (!this.queue[0])
continue;
const timeout = this.queue[0].timeout;
await this.next();
await (0, Util_1.delayFor)(timeout);
}
return this;
}
/**
* Goes to the next item in the queue
*/
async next() {
if (this.paused)
return;
const item = this.queue.shift();
if (!item)
return true;
return item.run(...item.args);
}
/**
* Stop's the queue and blocks the next item from running
*/
stop() {
this.paused = true;
return this;
}
/**
* Resume's the queue
*/
resume() {
this.paused = false;
return this;
}
/**
* Adds an item to the queue
*/
add(item) {
this.queue.push({
run: item.run,
args: item.args,
time: Date.now(),
timeout: item.timeout ?? this.options.timeout,
});
return this;
}
}
exports.Queue = Queue;