Skip to content

Commit

Permalink
Add debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow7000 committed Jul 28, 2022
1 parent 693b29d commit 022f452
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions qew.ts
Original file line number Diff line number Diff line change
@@ -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<T>(): [
Promise<T>,
(inp: T) => void,
Expand All @@ -13,19 +16,24 @@ function makeTriggerablePromise<T>(): [
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");
Expand All @@ -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<typeof console.log>) =>
this.debug ? console.log(...args) : undefined;

/**
* Push another async function onto the queue
* @param asyncFunc the async function to push onto this queue
Expand All @@ -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;
}
Expand All @@ -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`
);
}
}
}
Expand Down

0 comments on commit 022f452

Please sign in to comment.