@edgenets/utils 0.1.12 → 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,18 +1,17 @@
|
|
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) {
|
15
|
-
|
13
|
+
console.log("[Queue] Automatically starting processQueue...");
|
14
|
+
this.processQueue();
|
16
15
|
}
|
17
16
|
}
|
18
17
|
// 手动触发队列处理
|
@@ -35,43 +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
|
-
// 定义处理下一个任务的逻辑
|
54
|
-
this.processNextTask = () => {
|
55
|
-
if (this.queue.length === 0) {
|
56
|
-
if (this.activeTasks === 0) {
|
57
|
-
this.isProcessing = false;
|
58
|
-
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();
|
59
45
|
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
if (task) {
|
66
|
-
this.activeTasks++;
|
67
|
-
processTask(task); // 处理任务
|
46
|
+
catch (err) {
|
47
|
+
console.error(`[Queue][TaskFailed]: ${err}`);
|
48
|
+
}
|
49
|
+
finally {
|
50
|
+
this.activeTasks--;
|
68
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);
|
69
56
|
}
|
70
|
-
};
|
71
|
-
// 启动并发任务
|
72
|
-
for (let i = 0; i < (this.options.concurrency || 1) && this.queue.length > 0; i++) {
|
73
|
-
this.processNextTask();
|
74
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));
|
75
64
|
}
|
76
65
|
}
|
77
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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