From 00ccd23dc94d4849c176636105515a9450fd6b28 Mon Sep 17 00:00:00 2001 From: Aron Adler Date: Wed, 29 Jan 2020 11:06:08 +1100 Subject: [PATCH] Restore pushProm as alias + add JSDoc comments --- package.json | 2 +- qew.ts | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index a89287d..7c5ad85 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "qew", - "version": "0.10.0", + "version": "0.10.1", "description": "Queue asynchronous functions", "source": "qew.ts", "main": "dist/qew.js", diff --git a/qew.ts b/qew.ts index d268531..660521c 100644 --- a/qew.ts +++ b/qew.ts @@ -1,4 +1,4 @@ -type PromFunc = () => Promise; +type AsyncFunc = () => Promise; function makeTriggerablePromise(): [Promise, (inp: T) => void] { let triggerResolveWith!: (inp: T) => void; @@ -13,13 +13,23 @@ export class Qew { private queue: (() => void)[] = []; private executing = 0; + /** + * + * @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 + */ constructor(private maxConcurrent = 1, private delay = 0) {} - public push(promFunc: PromFunc) { + /** + * Push another async function onto the queue + * @param asyncFunc the async function to push onto this queue + */ + public push(asyncFunc: AsyncFunc) { const [prom, resolveProm] = makeTriggerablePromise(); const funcToRun = () => { - promFunc().then(result => { + asyncFunc().then(result => { resolveProm(result); this.executing = this.executing - 1; @@ -36,6 +46,11 @@ export class Qew { return prom; } + /** + * @deprecated this is now only an alias for `Qew#push` + */ + public pushProm = this.push; + private tryMove() { if (this.executing >= this.maxConcurrent) { // do nothing