@edgenets/utils 0.1.11 → 0.1.13
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.
@@ -8,7 +8,6 @@ export declare class Queue<T> {
|
|
8
8
|
private queue;
|
9
9
|
private activeTasks;
|
10
10
|
private isProcessing;
|
11
|
-
private processNextTask;
|
12
11
|
constructor(options?: QueueOptions);
|
13
12
|
enqueue(task: QueueTask<T>): void;
|
14
13
|
trigger(): Promise<void>;
|
@@ -18,5 +17,6 @@ export declare class Queue<T> {
|
|
18
17
|
isProcessing: boolean;
|
19
18
|
};
|
20
19
|
private processQueue;
|
20
|
+
private delay;
|
21
21
|
}
|
22
22
|
//# sourceMappingURL=queue.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../../src/utils/tasks/queue.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAE5C,MAAM,WAAW,YAAY;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,KAAK,CAAC,CAAC;
|
1
|
+
{"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../../../src/utils/tasks/queue.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAE5C,MAAM,WAAW,YAAY;IAC3B,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,qBAAa,KAAK,CAAC,CAAC;IAMhB,OAAO,CAAC,OAAO;IALjB,OAAO,CAAC,KAAK,CAAsB;IACnC,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,YAAY,CAAS;gBAGnB,OAAO,GAAE,YAAiD;IAI7D,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI;IAS3B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAU9B,SAAS,IAAI;QAClB,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,OAAO,CAAC;KACvB;YASa,YAAY;IAgC1B,OAAO,CAAC,KAAK;CAGd"}
|
@@ -1,17 +1,16 @@
|
|
1
1
|
export class Queue {
|
2
2
|
options;
|
3
|
-
queue = [];
|
4
|
-
activeTasks = 0;
|
5
|
-
isProcessing = false;
|
6
|
-
|
7
|
-
constructor(options = { concurrency: 1, interval: 0 }) {
|
3
|
+
queue = [];
|
4
|
+
activeTasks = 0;
|
5
|
+
isProcessing = false;
|
6
|
+
constructor(options = { concurrency: 1, interval: 1000 }) {
|
8
7
|
this.options = options;
|
9
|
-
this.processNextTask = () => { };
|
10
8
|
}
|
11
9
|
// 添加任务到队列
|
12
10
|
enqueue(task) {
|
13
11
|
this.queue.push(task);
|
14
12
|
if (!this.isProcessing) {
|
13
|
+
console.log("[Queue] Automatically starting processQueue...");
|
15
14
|
this.processQueue();
|
16
15
|
}
|
17
16
|
}
|
@@ -35,41 +34,33 @@ export class Queue {
|
|
35
34
|
}
|
36
35
|
// 队列处理逻辑
|
37
36
|
async processQueue() {
|
38
|
-
if (this.isProcessing)
|
39
|
-
return; // 防止重复调用
|
40
37
|
this.isProcessing = true;
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
}
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
finally {
|
49
|
-
this.activeTasks--;
|
50
|
-
this.processNextTask();
|
51
|
-
}
|
52
|
-
};
|
53
|
-
this.processNextTask = () => {
|
54
|
-
if (this.queue.length === 0) {
|
55
|
-
if (this.activeTasks === 0) {
|
56
|
-
this.isProcessing = false;
|
57
|
-
console.log("[Queue] All tasks processed.");
|
38
|
+
while (this.queue.length > 0) {
|
39
|
+
const batch = this.queue.splice(0, this.options.concurrency || 1);
|
40
|
+
this.activeTasks += batch.length;
|
41
|
+
console.log(`[Queue] Processing batch of ${batch.length} tasks...`);
|
42
|
+
await Promise.all(batch.map(async (task) => {
|
43
|
+
try {
|
44
|
+
await task();
|
58
45
|
}
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
this.activeTasks++;
|
65
|
-
processTask(task);
|
46
|
+
catch (err) {
|
47
|
+
console.error(`[Queue][TaskFailed]: ${err}`);
|
48
|
+
}
|
49
|
+
finally {
|
50
|
+
this.activeTasks--;
|
66
51
|
}
|
52
|
+
}));
|
53
|
+
if (this.queue.length > 0) {
|
54
|
+
console.log(`[Queue] Delaying for ${this.options.interval || 0}ms...`);
|
55
|
+
await this.delay(this.options.interval || 0);
|
67
56
|
}
|
68
|
-
};
|
69
|
-
while (this.activeTasks < (this.options.concurrency || 1) &&
|
70
|
-
this.queue.length > 0) {
|
71
|
-
this.processNextTask();
|
72
57
|
}
|
58
|
+
this.isProcessing = false;
|
59
|
+
console.log("[Queue] All tasks processed.");
|
60
|
+
}
|
61
|
+
// 延迟执行
|
62
|
+
delay(ms) {
|
63
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
73
64
|
}
|
74
65
|
}
|
75
66
|
//# sourceMappingURL=queue.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../src/utils/tasks/queue.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,KAAK;
|
1
|
+
{"version":3,"file":"queue.js","sourceRoot":"","sources":["../../../src/utils/tasks/queue.ts"],"names":[],"mappings":"AAOA,MAAM,OAAO,KAAK;IAMN;IALF,KAAK,GAAmB,EAAE,CAAC;IAC3B,WAAW,GAAG,CAAC,CAAC;IAChB,YAAY,GAAG,KAAK,CAAC;IAE7B,YACU,UAAwB,EAAE,WAAW,EAAE,CAAC,EAAE,QAAQ,EAAE,IAAI,EAAE;QAA1D,YAAO,GAAP,OA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