3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 14:11:50 +00:00
zeppelin/backend/src/Queue.ts
2021-05-22 13:57:54 +03:00

63 lines
1.4 KiB
TypeScript

import { SECONDS } from "./utils";
type InternalQueueFn = () => Promise<void>;
type AnyFn = (...args: any[]) => any;
const DEFAULT_TIMEOUT = 10 * SECONDS;
export class Queue<TQueueFunction extends AnyFn = AnyFn> {
protected running = false;
protected queue: InternalQueueFn[] = [];
protected _timeout: number;
constructor(timeout = DEFAULT_TIMEOUT) {
this._timeout = timeout;
}
get timeout(): number {
return this._timeout;
}
/**
* The number of operations that are currently queued up or running.
* I.e. backlog (queue) + current running process, if any.
*
* If this is 0, queueing a function will run it as soon as possible.
*/
get length(): number {
return this.queue.length + (this.running ? 1 : 0);
}
public add(fn: TQueueFunction): Promise<void> {
const promise = new Promise<void>(resolve => {
this.queue.push(async () => {
await fn();
resolve();
});
if (!this.running) this.next();
});
return promise;
}
public next(): void {
this.running = true;
if (this.queue.length === 0) {
this.running = false;
return;
}
const fn = this.queue.shift()!;
new Promise(resolve => {
// Either fn() completes or the timeout is reached
void fn().then(resolve);
setTimeout(resolve, this._timeout);
}).then(() => this.next());
}
public clear() {
this.queue.splice(0, this.queue.length);
}
}