From 022f4522bbc33d375afd0213c0f0c3fdb6c4fb48 Mon Sep 17 00:00:00 2001 From: Aron Adler Date: Thu, 28 Jul 2022 22:18:47 +0100 Subject: [PATCH] Add debug logs --- qew.ts | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/qew.ts b/qew.ts index 58ffd72..9601409 100644 --- a/qew.ts +++ b/qew.ts @@ -1,3 +1,6 @@ +/** + * This returns a promise along with two functions to remotely ~detonate~ trigger either a resolution of rejection of the promise from elsewhere in the code. It's magic! + */ function makeTriggerablePromise(): [ Promise, (inp: T) => void, @@ -13,19 +16,24 @@ function makeTriggerablePromise(): [ return [promToReturn, triggerResolveWith, triggerRejectWith]; } +export type Options = { debug?: boolean }; + export class Qew { private queue: (() => void)[] = []; private executing = 0; + private debug = false; /** * * @param maxConcurrent how many functions can be run simultaneously * @param delay how many ms to wait between when one function has resolved and * the next one is run + * @param options the only option currently supported is `debug`, which if enabled prints debugging logs */ constructor( private maxConcurrent = 1, - private delay: number | (() => number) = 0 + private delay: number | (() => number) = 0, + options?: Options ) { if (maxConcurrent < 1) { throw new Error("maxConcurrent has to be 1 or higher"); @@ -35,8 +43,26 @@ export class Qew { "`delay` parameter should be either a non-negative number or a function that returns one" ); } + + this.debug = options?.debug ?? this.debug; + + this.debugLog( + `Debugging for Qew enabled! This qew has a maxConcurrent of ${maxConcurrent} and a ${ + typeof delay === "number" + ? `delay of ${delay}ms` + : "custom delay generator function" + }` + ); + + this.tryMove = this.tryMove.bind(this); + this.push = this.push.bind(this); + this.pushProm = this.pushProm.bind(this); + this.debugLog = this.debugLog.bind(this); } + private debugLog = (...args: Parameters) => + this.debug ? console.log(...args) : undefined; + /** * Push another async function onto the queue * @param asyncFunc the async function to push onto this queue @@ -56,14 +82,18 @@ export class Qew { const delay = typeof this.delay === "function" ? this.delay() : this.delay; - setTimeout(this.tryMove, delay); + this.debugLog( + `Function resolved! About to tryMove again after ${delay}ms. (At ${new Date()})` + ); + + setTimeout(() => this.tryMove("promise completion"), delay); }) .catch(rejectProm); }; this.queue.push(funcToRun); - this.tryMove(); + this.tryMove("push"); return prom; } @@ -73,14 +103,27 @@ export class Qew { */ public pushProm = this.push; - private tryMove() { + private tryMove(triggeredBy: "push" | "promise completion") { + this.debugLog( + `Trying to move because of: ${triggeredBy} (at unix time ${new Date()})` + ); + if (this.executing < this.maxConcurrent) { const first = this.queue.shift(); + this.debugLog( + `Under execution limit! ${ + first ? "Grabbed a function off the queue" : "Nothing on the queue" + }` + ); if (first) { this.executing = this.executing + 1; first(); } + } else { + this.debugLog( + `Currently at execution limit of ${this.maxConcurrent} so stopped move attempt` + ); } } }