diff options
Diffstat (limited to 'src/ppq')
| -rw-r--r-- | src/ppq/index.d.ts | 7 | ||||
| -rw-r--r-- | src/ppq/index.js | 31 |
2 files changed, 28 insertions, 10 deletions
diff --git a/src/ppq/index.d.ts b/src/ppq/index.d.ts index ee12f89..21cdeaf 100644 --- a/src/ppq/index.d.ts +++ b/src/ppq/index.d.ts @@ -1,8 +1,11 @@ // tslint:disable:no-any -type Promiser<T = any> = () => PromiseLike<T>; +export type Promiser<T = any> = (args: any) => PromiseLike<T>; export class PromisePriorityQueue { public constructor(concurrent?: number, least?: number); - public enqueue<T = any>(priority: number, promiser: Promiser<T>): Promise<T>; + public enqueue<T = any>(priority: number, promiser: Promiser<T>, binding: any, ...argv: any[]): Promise<T>; + public readonly size: number; } + +export const queue: PromisePriorityQueue; diff --git a/src/ppq/index.js b/src/ppq/index.js index 37856d0..ec2f188 100644 --- a/src/ppq/index.js +++ b/src/ppq/index.js @@ -1,14 +1,17 @@ class PromisePriorityQueue { - construtor(concurrent, least) { + + constructor(concurrent, least) { this.least = least || 10; - this.concurrent = concurrent || 50; + this.concurrent = concurrent || 2; this.running = 0; this.queues = []; - for (let i = 1; i <= this.least; ++i) { + for (let i = 0; i < this.least; ++i) { this.queues[i] = []; } + + this.size = 0; } configure(config) { @@ -16,17 +19,29 @@ class PromisePriorityQueue { if (config.concurrent) this.concurrent = config.concurrent; } - enqueue(i, p) { + // enqueue(i, p, t, ...args) { + // return p.bind(t)(...args); + // } + + enqueue(i, p, t, ...argv) { + this.size++; return new Promise((res, rej) => { - q[i].push([p, res, rej]); + this.queues[i-1].push( + [ + p.bind(t, ...argv), + res, + rej + ] + ); this.run(); }); }; dequeue() { for (let i = 0; i < this.least; ++i) { - if (q[i][0] !== undefined) { - return q[i].shift; + if (this.queues[i][0] !== undefined) { + this.size--; + return this.queues[i].shift(); } } return null; @@ -35,7 +50,7 @@ class PromisePriorityQueue { async run() { if (this.running < this.concurrent) { let x; - while (this.dequeue()) { + while (x = this.dequeue()) { await x[0]().then(x[1], x[2]); } } |
