Skip to content

Commit 53ded3e

Browse files
committed
fix(boxjs): 订阅并发控制
1 parent baa2893 commit 53ded3e

File tree

4 files changed

+108
-22
lines changed

4 files changed

+108
-22
lines changed

box/chavy.boxjs.js

+42-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const $ = new Env('BoxJs')
33
// 为 eval 准备的上下文环境
44
const $eval_env = {}
55

6-
$.version = '0.19.20'
6+
$.version = '0.19.21'
77
$.versionType = 'beta'
88

99
// 发出的请求需要需要 Surge、QuanX 的 rewrite
@@ -1002,18 +1002,49 @@ function update(obj, path, value) {
10021002
current[keys[keys.length - 1]] = value
10031003
}
10041004

1005+
// 自定义并发控制函数
1006+
async function limitConcurrency(tasks, limit) {
1007+
const results = [];
1008+
const executing = [];
1009+
1010+
for (const task of tasks) {
1011+
const promise = task(); // 执行任务
1012+
results.push(promise);
1013+
1014+
if (executing.length >= limit) {
1015+
await Promise.race(executing);
1016+
}
1017+
1018+
executing.push(promise);
1019+
promise.then(() => {
1020+
const index = executing.indexOf(promise);
1021+
if (index !== -1) executing.splice(index, 1);
1022+
}).catch(() => {
1023+
const index = executing.indexOf(promise);
1024+
if (index !== -1) executing.splice(index, 1);
1025+
});
1026+
}
1027+
1028+
return Promise.all(results);
1029+
}
1030+
10051031
async function reloadAppSubCaches() {
1006-
$.msg($.name, '更新订阅: 开始!')
1007-
const reloadActs = []
1008-
const usercfgs = getUserCfgs()
1032+
$.msg($.name, '更新订阅: 开始!');
1033+
const reloadActs = [];
1034+
const usercfgs = getUserCfgs();
1035+
1036+
// 收集所有任务(函数形式)
10091037
usercfgs.appsubs.forEach((sub) => {
1010-
reloadActs.push(reloadAppSubCache(sub.url))
1011-
})
1012-
await Promise.all(reloadActs)
1013-
$.log(`全部订阅, 完成!`)
1014-
const endTime = new Date().getTime()
1015-
const costTime = (endTime - $.startTime) / 1000
1016-
$.msg($.name, `更新订阅: 完成! 🕛 ${costTime} 秒`)
1038+
reloadActs.push(() => reloadAppSubCache(sub.url)); // 存储函数而不是立即执行的 Promise
1039+
});
1040+
1041+
// 使用并发限制执行任务
1042+
await limitConcurrency(reloadActs, 20); // 限制并发数为 20
1043+
1044+
$.log(`全部订阅, 完成!`);
1045+
const endTime = new Date().getTime();
1046+
const costTime = (endTime - $.startTime) / 1000;
1047+
$.msg($.name, `更新订阅: 完成! 🕛 ${costTime} 秒`);
10171048
}
10181049

10191050
function upgradeUserData() {

box/release/box.release.json

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
{
22
"releases": [
3+
{
4+
"version": "0.19.21",
5+
"tags": ["beta"],
6+
"author": "@GideonSenku",
7+
"msg": "fix(boxjs): 订阅更新问题",
8+
"notes": [
9+
{
10+
"name": "修复",
11+
"descs": ["订阅更新问题"]
12+
}
13+
]
14+
},
315
{
416
"version": "0.19.20",
517
"tags": ["beta"],

box/release/box.release.tf.json

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
{
22
"releases": [
3+
{
4+
"version": "0.19.21",
5+
"tags": ["beta"],
6+
"author": "@GideonSenku",
7+
"msg": "fix(boxjs): 订阅更新问题",
8+
"notes": [
9+
{
10+
"name": "修复",
11+
"descs": ["订阅更新问题"]
12+
}
13+
]
14+
},
315
{
416
"version": "0.19.20",
517
"tags": ["beta"],

chavy.box.js

+42-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const $ = new Env('BoxJs')
33
// 为 eval 准备的上下文环境
44
const $eval_env = {}
55

6-
$.version = '0.19.20'
6+
$.version = '0.19.21'
77
$.versionType = 'beta'
88

99
// 发出的请求需要需要 Surge、QuanX 的 rewrite
@@ -1002,18 +1002,49 @@ function update(obj, path, value) {
10021002
current[keys[keys.length - 1]] = value
10031003
}
10041004

1005+
// 自定义并发控制函数
1006+
async function limitConcurrency(tasks, limit) {
1007+
const results = [];
1008+
const executing = [];
1009+
1010+
for (const task of tasks) {
1011+
const promise = task(); // 执行任务
1012+
results.push(promise);
1013+
1014+
if (executing.length >= limit) {
1015+
await Promise.race(executing);
1016+
}
1017+
1018+
executing.push(promise);
1019+
promise.then(() => {
1020+
const index = executing.indexOf(promise);
1021+
if (index !== -1) executing.splice(index, 1);
1022+
}).catch(() => {
1023+
const index = executing.indexOf(promise);
1024+
if (index !== -1) executing.splice(index, 1);
1025+
});
1026+
}
1027+
1028+
return Promise.all(results);
1029+
}
1030+
10051031
async function reloadAppSubCaches() {
1006-
$.msg($.name, '更新订阅: 开始!')
1007-
const reloadActs = []
1008-
const usercfgs = getUserCfgs()
1032+
$.msg($.name, '更新订阅: 开始!');
1033+
const reloadActs = [];
1034+
const usercfgs = getUserCfgs();
1035+
1036+
// 收集所有任务(函数形式)
10091037
usercfgs.appsubs.forEach((sub) => {
1010-
reloadActs.push(reloadAppSubCache(sub.url))
1011-
})
1012-
await Promise.all(reloadActs)
1013-
$.log(`全部订阅, 完成!`)
1014-
const endTime = new Date().getTime()
1015-
const costTime = (endTime - $.startTime) / 1000
1016-
$.msg($.name, `更新订阅: 完成! 🕛 ${costTime} 秒`)
1038+
reloadActs.push(() => reloadAppSubCache(sub.url)); // 存储函数而不是立即执行的 Promise
1039+
});
1040+
1041+
// 使用并发限制执行任务
1042+
await limitConcurrency(reloadActs, 20); // 限制并发数为 20
1043+
1044+
$.log(`全部订阅, 完成!`);
1045+
const endTime = new Date().getTime();
1046+
const costTime = (endTime - $.startTime) / 1000;
1047+
$.msg($.name, `更新订阅: 完成! 🕛 ${costTime} 秒`);
10171048
}
10181049

10191050
function upgradeUserData() {

0 commit comments

Comments
 (0)