From 45be87fe8232b531f949b4c2415b71fed763ed0c Mon Sep 17 00:00:00 2001 From: David Teller Date: Tue, 24 May 2022 17:19:05 +0200 Subject: [PATCH] WIP: Clarifying error when a ThrottlingQueue has been disposed of --- src/queues/ThrottlingQueue.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/queues/ThrottlingQueue.ts b/src/queues/ThrottlingQueue.ts index b9052c6..6c2fb4d 100644 --- a/src/queues/ThrottlingQueue.ts +++ b/src/queues/ThrottlingQueue.ts @@ -26,7 +26,7 @@ export class ThrottlingQueue { /** * The pending tasks. */ - private tasks: (() => Promise)[] | null; + private _tasks: (() => Promise)[] | null; /** * A timeout for the next task to execute. @@ -50,7 +50,7 @@ export class ThrottlingQueue { constructor(private mjolnir: Mjolnir, delayMS: number) { this.timeout = null; this.delayMS = delayMS; - this.tasks = []; + this._tasks = []; } /** @@ -58,14 +58,14 @@ export class ThrottlingQueue { */ public dispose() { this.stop(); - this.tasks = null; + this._tasks = null; } /** * The number of tasks waiting to be executed. */ get length(): number { - return this.tasks!.length; + return this.tasks.length; } /** @@ -86,7 +86,7 @@ export class ThrottlingQueue { reject(ex); }; }; - this.tasks!.push(wrapper); + this.tasks.push(wrapper); this.start(); }); } @@ -118,7 +118,7 @@ export class ThrottlingQueue { // Already started. return; } - if (!this.tasks!.length) { + if (!this.tasks.length) { // Nothing to do. return; } @@ -168,7 +168,7 @@ export class ThrottlingQueue { */ private async step() { // Pull task. - const task = this.tasks!.shift(); + const task = this.tasks.shift(); if (!task) { // Nothing to do. // Stop the loop until we have something to do. @@ -188,4 +188,14 @@ export class ThrottlingQueue { this.start(); } } + + /** + * Return `tasks`, unless the queue has been disposed of. + */ + private get tasks(): (() => Promise)[] { + if (this._tasks == null) { + throw new TypeError("This Throttling Queue has been disposed of and shouldn't be used anymore"); + } + return this._tasks; + } }