Skip to content

Commit

Permalink
Restore pushProm as alias + add JSDoc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrow7000 committed Jan 29, 2020
1 parent acf4a8d commit 00ccd23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 18 additions & 3 deletions qew.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type PromFunc<T> = () => Promise<T>;
type AsyncFunc<T> = () => Promise<T>;

function makeTriggerablePromise<T>(): [Promise<T>, (inp: T) => void] {
let triggerResolveWith!: (inp: T) => void;
Expand All @@ -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<T>(promFunc: PromFunc<T>) {
/**
* Push another async function onto the queue
* @param asyncFunc the async function to push onto this queue
*/
public push<T>(asyncFunc: AsyncFunc<T>) {
const [prom, resolveProm] = makeTriggerablePromise<T>();

const funcToRun = () => {
promFunc().then(result => {
asyncFunc().then(result => {
resolveProm(result);
this.executing = this.executing - 1;

Expand All @@ -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
Expand Down

0 comments on commit 00ccd23

Please sign in to comment.