diff --git a/backend/src/Queue.ts b/backend/src/Queue.ts index 41192e0d..3158346b 100644 --- a/backend/src/Queue.ts +++ b/backend/src/Queue.ts @@ -1,23 +1,24 @@ import { SECONDS } from "./utils"; -type QueueFn = (...args: any[]) => Promise; +type InternalQueueFn = () => Promise; +type AnyFn = (...args: any[]) => any; const DEFAULT_TIMEOUT = 10 * SECONDS; -export class Queue { - protected running: boolean = false; - protected queue: QueueFn[] = []; +export class Queue { + protected running = false; + protected queue: InternalQueueFn[] = []; protected timeout: number; constructor(timeout = DEFAULT_TIMEOUT) { this.timeout = timeout; } - public add(fn) { - const promise = new Promise(resolve => { + public add(fn: TQueueFunction): Promise { + const promise = new Promise(resolve => { this.queue.push(async () => { await fn(); - resolve(undefined); + resolve(); }); if (!this.running) this.next(); @@ -26,7 +27,7 @@ export class Queue { return promise; } - public next() { + public next(): void { this.running = true; if (this.queue.length === 0) { @@ -37,7 +38,7 @@ export class Queue { const fn = this.queue.shift()!; new Promise(resolve => { // Either fn() completes or the timeout is reached - fn().then(resolve); + void fn().then(resolve); setTimeout(resolve, this.timeout); }).then(() => this.next()); }