aboutsummaryrefslogtreecommitdiff
path: root/src/ppq/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/ppq/index.js')
-rw-r--r--src/ppq/index.js31
1 files changed, 23 insertions, 8 deletions
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]);
}
}