From b254022db2d65adc8fd88d0535adb311295f298f Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Sat, 22 May 2021 13:34:25 +0300 Subject: [PATCH] Update types for Queue --- backend/src/Queue.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) 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()); }