status-sharding
Version:
Welcome to Status Sharding! This package is designed to provide an efficient and flexible solution for sharding Discord bots, allowing you to scale your bot across multiple processes or workers.
69 lines (68 loc) • 1.95 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Queue = void 0;
const shardingUtils_1 = require("../other/shardingUtils");
/** Queue class. */
class Queue {
options;
/** Whether the queue is paused. */
paused = false;
/** List of items in the queue. */
queue = [];
/** Creates an instance of Queue. */
constructor(options) {
this.options = options;
}
/** Starts the queue and run's the item functions. */
async start() {
if (this.options.mode !== 'auto') {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (this.queue.length === 0) {
clearInterval(interval);
resolve(this); // Queue successfully 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 shardingUtils_1.ShardingUtils.delayFor(timeout);
}
return this;
}
/** Runs 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);
}
/** Stops the queue. */
stop() {
this.paused = true;
return this;
}
/** Resumes 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;