diff options
| author | Tom Harley | 2018-04-25 00:54:35 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-04-25 00:54:35 +0100 |
| commit | 203386afb89d2e557e4df610c6f5e566b4554222 (patch) | |
| tree | e6a468e393cd5653fb9b371352281f61bf35c9c7 /src/ppq | |
| parent | Add promise priority queue for conversion logic (diff) | |
| download | medicine-203386afb89d2e557e4df610c6f5e566b4554222.tar.gz medicine-203386afb89d2e557e4df610c6f5e566b4554222.zip | |
Edit promise queue to return promises itself
Diffstat (limited to 'src/ppq')
| -rw-r--r-- | src/ppq/index.d.ts | 5 | ||||
| -rw-r--r-- | src/ppq/index.js | 27 |
2 files changed, 17 insertions, 15 deletions
diff --git a/src/ppq/index.d.ts b/src/ppq/index.d.ts index 0eb2361..ee12f89 100644 --- a/src/ppq/index.d.ts +++ b/src/ppq/index.d.ts @@ -1,7 +1,8 @@ -// tslint:disable-next-line:no-any +// tslint:disable:no-any + type Promiser<T = any> = () => PromiseLike<T>; export class PromisePriorityQueue { public constructor(concurrent?: number, least?: number); - public enqueue(priority: number, promiser: Promiser): void; + public enqueue<T = any>(priority: number, promiser: Promiser<T>): Promise<T>; } diff --git a/src/ppq/index.js b/src/ppq/index.js index 5ce6115..37856d0 100644 --- a/src/ppq/index.js +++ b/src/ppq/index.js @@ -9,33 +9,34 @@ class PromisePriorityQueue { for (let i = 1; i <= this.least; ++i) { this.queues[i] = []; } - this.priority = this.least; } - enqueue(i, p) { - if (i < least) { - priority = i; - } - q[i].push(p); + configure(config) { + if (config.least) this.least = config.least; + if (config.concurrent) this.concurrent = config.concurrent; + } - this.run(); + enqueue(i, p) { + return new Promise((res, rej) => { + q[i].push([p, res, rej]); + this.run(); + }); }; dequeue() { - while(this.priority <= this.least) { - if (q[this.priority][0] !== undefined) { - return q[this.priority].shift; + for (let i = 0; i < this.least; ++i) { + if (q[i][0] !== undefined) { + return q[i].shift; } - this.priority++; } return null; } async run() { if (this.running < this.concurrent) { - let p; + let x; while (this.dequeue()) { - await p(); + await x[0]().then(x[1], x[2]); } } } |
