aboutsummaryrefslogtreecommitdiff
path: root/src/ppq
diff options
context:
space:
mode:
authorcjd242018-04-25 06:41:56 +0100
committercjd242018-04-25 06:41:56 +0100
commitf36deb0245881d8442c31e16572c67e58bb7c451 (patch)
tree8e3cc52fe8b8a896be28ff1453ebe5dd82e39b24 /src/ppq
parentEdit promise queue to return promises itself (diff)
downloadmedicine-f36deb0245881d8442c31e16572c67e58bb7c451.tar.gz
medicine-f36deb0245881d8442c31e16572c67e58bb7c451.zip

Fixing lots of CZI integration things, to prep for server deployment for JH demo

Diffstat (limited to 'src/ppq')
-rw-r--r--src/ppq/index.d.ts7
-rw-r--r--src/ppq/index.js31
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]);
}
}