2019-08-18 16:40:15 +03:00
|
|
|
import { SECONDS } from "./utils";
|
|
|
|
|
2021-05-22 13:34:25 +03:00
|
|
|
type InternalQueueFn = () => Promise<void>;
|
|
|
|
type AnyFn = (...args: any[]) => any;
|
2018-11-24 14:58:54 +02:00
|
|
|
|
2019-08-18 16:40:15 +03:00
|
|
|
const DEFAULT_TIMEOUT = 10 * SECONDS;
|
2019-02-23 21:19:46 +02:00
|
|
|
|
2021-05-22 13:34:25 +03:00
|
|
|
export class Queue<TQueueFunction extends AnyFn = AnyFn> {
|
|
|
|
protected running = false;
|
|
|
|
protected queue: InternalQueueFn[] = [];
|
2019-02-23 21:19:46 +02:00
|
|
|
protected timeout: number;
|
|
|
|
|
|
|
|
constructor(timeout = DEFAULT_TIMEOUT) {
|
|
|
|
this.timeout = timeout;
|
|
|
|
}
|
2018-11-24 14:58:54 +02:00
|
|
|
|
2021-05-22 13:34:25 +03:00
|
|
|
public add(fn: TQueueFunction): Promise<void> {
|
|
|
|
const promise = new Promise<void>(resolve => {
|
2018-11-24 14:58:54 +02:00
|
|
|
this.queue.push(async () => {
|
|
|
|
await fn();
|
2021-05-22 13:34:25 +03:00
|
|
|
resolve();
|
2018-11-24 14:58:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!this.running) this.next();
|
|
|
|
});
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2021-05-22 13:34:25 +03:00
|
|
|
public next(): void {
|
2018-11-24 14:58:54 +02:00
|
|
|
this.running = true;
|
|
|
|
|
|
|
|
if (this.queue.length === 0) {
|
|
|
|
this.running = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const fn = this.queue.shift()!;
|
2018-11-24 14:58:54 +02:00
|
|
|
new Promise(resolve => {
|
|
|
|
// Either fn() completes or the timeout is reached
|
2021-05-22 13:34:25 +03:00
|
|
|
void fn().then(resolve);
|
2018-11-24 14:58:54 +02:00
|
|
|
setTimeout(resolve, this.timeout);
|
|
|
|
}).then(() => this.next());
|
|
|
|
}
|
2020-07-27 20:42:10 +03:00
|
|
|
|
|
|
|
public clear() {
|
|
|
|
this.queue.splice(0, this.queue.length);
|
|
|
|
}
|
2018-11-24 14:58:54 +02:00
|
|
|
}
|