Skip to content

Commit

Permalink
revert: 订阅流量信息回退到一组三个
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Nov 3, 2024
1 parent 1977c6e commit 24ee0b0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store-front-end",
"version": "2.14.282",
"version": "2.14.283",
"private": true,
"scripts": {
"dev": "vite --host",
Expand Down
53 changes: 20 additions & 33 deletions src/store/subs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,46 +74,33 @@ export const useSubsStore = defineStore('subsStore', {
}
},
async fetchFlows(sub?: Sub[]) {
type FlowUrlItem = [string, string, boolean, boolean, boolean];
const asyncGetFlow = async (item: FlowUrlItem) => {
const [url, name, noFlow, hideExpire, showRemaining] = item;
const asyncGetFlow = async ([url, name, noFlow, hideExpire, showRemaining]) => {
if (noFlow) {
this.flows[url] = { status:'noFlow' };
return false;
}
try {
const { data } = await subsApi.getFlow(name);
this.flows[url] = {...data, hideExpire, showRemaining };
} catch (e) {
this.flows[url] = { status: 'error' };
} else {
try {
const { data } = await subsApi.getFlow(name);
this.flows[url] = {...data, hideExpire, showRemaining };
} catch (e) {
}
}
};
// 并发执行所有请求,每4个请求错开150ms发起
const flowsUrlList = getFlowsUrlList(sub || this.subs) as FlowUrlItem[];
const requests: Promise<void>[] = flowsUrlList.map((item, index) => {
const delay = Math.floor(index / 4) * 150;
return new Promise<void>((resolve) => {
setTimeout(() => {
void asyncGetFlow(item).finally(() => resolve());
}, delay);
});
});

await Promise.all(requests);
// const subs = sub || this.subs;
// getFlowsUrlList(subs).forEach(asyncGetFlow);
// 多次反复开启 容易爆内存 尝试分批请求 3/100ms
// const flowsUrlList = getFlowsUrlList(sub || this.subs);
// const batches = [];
const flowsUrlList = getFlowsUrlList(sub || this.subs);
const batches = [];

// for (let i = 0; i < flowsUrlList.length; i += 4) {
// const batch = flowsUrlList.slice(i, i + 4);
// batches.push(batch);
// }
for (let i = 0; i < flowsUrlList.length; i += 4) {
const batch = flowsUrlList.slice(i, i + 4);
batches.push(batch);
}

// for (const batch of batches) {
// const promises = batch.map(asyncGetFlow);
// await Promise.all(promises);
// // await new Promise((resolve) => setTimeout(resolve, 150));
// }
for (const batch of batches) {
const promises = batch.map(asyncGetFlow);
await Promise.all(promises);
// await new Promise((resolve) => setTimeout(resolve, 150));
}
},
async deleteSub(type: SubsType, name: string) {
const { showNotify } = useAppNotifyStore();
Expand Down

0 comments on commit 24ee0b0

Please sign in to comment.