@wikicasa-dev/node-common 2.0.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/Pool.d.ts +2 -1
- package/dist/src/Pool.js +17 -12
- package/package.json +1 -1
package/dist/src/Pool.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export declare class Pool {
|
|
2
2
|
private name;
|
|
3
3
|
private readonly concurrency;
|
|
4
|
+
private readonly limiter;
|
|
4
5
|
private queue;
|
|
5
6
|
private activeCount;
|
|
6
|
-
constructor(name: string, concurrency: number);
|
|
7
|
+
constructor(name: string, concurrency: number, limitCall?: number, limitInterval?: number);
|
|
7
8
|
static run: (calls: Array<() => Promise<any>>, concurrency: number) => Promise<any[]>;
|
|
8
9
|
enqueue(...calls: Array<() => Promise<any>>): Promise<any[]>;
|
|
9
10
|
private schedule;
|
package/dist/src/Pool.js
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { Console, COLORS } from "./Console.js";
|
|
2
|
+
import throttledQueue from "throttled-queue";
|
|
2
3
|
const BATCH_SIZE = 1000;
|
|
3
4
|
export class Pool {
|
|
4
5
|
name;
|
|
5
6
|
concurrency;
|
|
7
|
+
limiter;
|
|
6
8
|
queue = [];
|
|
7
9
|
activeCount;
|
|
8
|
-
constructor(name = "default", concurrency) {
|
|
10
|
+
constructor(name = "default", concurrency, limitCall = 1000, limitInterval = 1000) {
|
|
9
11
|
this.name = name;
|
|
10
12
|
this.concurrency = concurrency;
|
|
11
13
|
this.queue = [];
|
|
12
14
|
this.activeCount = 0;
|
|
15
|
+
this.limiter = throttledQueue(limitCall, limitInterval, true);
|
|
13
16
|
this.updatePrint();
|
|
14
17
|
}
|
|
15
18
|
static run = async (calls, concurrency) => {
|
|
@@ -37,17 +40,19 @@ export class Pool {
|
|
|
37
40
|
while (this.activeCount < this.concurrency && this.queue.length > 0) {
|
|
38
41
|
const { call, resolve, reject } = this.queue.shift();
|
|
39
42
|
this.activeCount++;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
this.limiter(() => {
|
|
44
|
+
call()
|
|
45
|
+
.then((result) => {
|
|
46
|
+
resolve(result);
|
|
47
|
+
})
|
|
48
|
+
.catch((error) => {
|
|
49
|
+
reject(error);
|
|
50
|
+
})
|
|
51
|
+
.finally(() => {
|
|
52
|
+
this.activeCount--;
|
|
53
|
+
this.updatePrint();
|
|
54
|
+
this.schedule();
|
|
55
|
+
});
|
|
51
56
|
});
|
|
52
57
|
}
|
|
53
58
|
}
|