@nu-art/ts-common 0.204.147 → 0.204.149
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/package.json +1 -1
- package/utils/queue-v2.d.ts +2 -0
- package/utils/queue-v2.js +18 -0
package/package.json
CHANGED
package/utils/queue-v2.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type QueueItem<InputType, OutputType> = {
|
|
|
7
7
|
export declare class QueueV2<ItemType, OutputType = any> extends Logger {
|
|
8
8
|
private allowedParallelOperationsCount;
|
|
9
9
|
private runningOperationsCount;
|
|
10
|
+
private cancelled;
|
|
10
11
|
private queue;
|
|
11
12
|
private onQueueEmpty?;
|
|
12
13
|
private finalResolve?;
|
|
@@ -24,4 +25,5 @@ export declare class QueueV2<ItemType, OutputType = any> extends Logger {
|
|
|
24
25
|
private ignore;
|
|
25
26
|
execute: () => void;
|
|
26
27
|
executeSync(): Promise<void>;
|
|
28
|
+
cancelAll: () => void;
|
|
27
29
|
}
|
package/utils/queue-v2.js
CHANGED
|
@@ -25,9 +25,11 @@ class QueueV2 extends Logger_1.Logger {
|
|
|
25
25
|
super(name);
|
|
26
26
|
this.allowedParallelOperationsCount = 1;
|
|
27
27
|
this.runningOperationsCount = 0;
|
|
28
|
+
this.cancelled = false;
|
|
28
29
|
this.queue = [];
|
|
29
30
|
this.getLength = () => this.queue.length;
|
|
30
31
|
this.ignore = () => {
|
|
32
|
+
this.cancelled = false;
|
|
31
33
|
};
|
|
32
34
|
this.execute = () => {
|
|
33
35
|
var _a, _b;
|
|
@@ -41,10 +43,19 @@ class QueueV2 extends Logger_1.Logger {
|
|
|
41
43
|
if (sorter)
|
|
42
44
|
this.queue = (0, array_tools_1.sortArray)(this.queue, (queueItem) => sorter(queueItem.item));
|
|
43
45
|
for (let i = 0; this.runningOperationsCount < this.allowedParallelOperationsCount && i < this.queue.length; i++) {
|
|
46
|
+
if (this.cancelled) {
|
|
47
|
+
this.cancelled = false;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
44
50
|
const toExecute = this.queue[0];
|
|
45
51
|
(0, array_tools_1.removeItemFromArray)(this.queue, toExecute);
|
|
46
52
|
this.runningOperationsCount++;
|
|
47
53
|
new Promise((resolve, reject) => {
|
|
54
|
+
if (this.cancelled) {
|
|
55
|
+
this.cancelled = false;
|
|
56
|
+
this.runningOperationsCount = 0;
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
48
59
|
this.runner(toExecute.item).then(output => {
|
|
49
60
|
var _a;
|
|
50
61
|
(_a = toExecute.onCompleted) === null || _a === void 0 ? void 0 : _a.call(toExecute, output);
|
|
@@ -69,6 +80,13 @@ class QueueV2 extends Logger_1.Logger {
|
|
|
69
80
|
}).then(this.execute)
|
|
70
81
|
.catch(this.ignore);
|
|
71
82
|
}
|
|
83
|
+
if (this.cancelled)
|
|
84
|
+
this.cancelled = false;
|
|
85
|
+
};
|
|
86
|
+
this.cancelAll = () => {
|
|
87
|
+
if (this.runningOperationsCount > 0)
|
|
88
|
+
this.cancelled = true;
|
|
89
|
+
this.queue = [];
|
|
72
90
|
};
|
|
73
91
|
this.runner = runner;
|
|
74
92
|
}
|