diff --git a/src/components/proxy/proxy-global.tsx b/src/components/proxy/proxy-global.tsx index 7529dcb..d3d36f9 100644 --- a/src/components/proxy/proxy-global.tsx +++ b/src/components/proxy/proxy-global.tsx @@ -70,12 +70,9 @@ const ProxyGlobal = (props: Props) => { const onCheckAll = useLockFn(async () => { const names = sortedProxies.map((p) => p.name); - await delayManager.checkListDelay( - { names, groupName, skipNum: 8, maxTimeout: 600 }, - () => mutate("getProxies") + await delayManager.checkListDelay({ names, groupName, skipNum: 8 }, () => + mutate("getProxies") ); - - mutate("getProxies"); }); useEffect(() => onLocation(false), [groupName]); diff --git a/src/components/proxy/proxy-group.tsx b/src/components/proxy/proxy-group.tsx index 61b8298..3f6a6db 100644 --- a/src/components/proxy/proxy-group.tsx +++ b/src/components/proxy/proxy-group.tsx @@ -92,12 +92,11 @@ const ProxyGroup = ({ group }: Props) => { const names = sortedProxies.map((p) => p.name); const groupName = group.name; - await delayManager.checkListDelay( - { names, groupName, skipNum: 8, maxTimeout: 600 }, - () => mutate("getProxies") + await delayManager.checkListDelay({ names, groupName, skipNum: 8 }, () => + mutate("getProxies") ); - mutate("getProxies"); + console.log("finish"); }); // auto scroll to current index diff --git a/src/components/proxy/proxy-item.tsx b/src/components/proxy/proxy-item.tsx index 071aea2..73bed67 100644 --- a/src/components/proxy/proxy-item.tsx +++ b/src/components/proxy/proxy-item.tsx @@ -1,4 +1,5 @@ -import { useEffect, useRef, useState } from "react"; +import { useEffect, useState } from "react"; +import { useLockFn } from "ahooks"; import { CheckCircleOutlineRounded } from "@mui/icons-material"; import { alpha, @@ -50,20 +51,15 @@ const ProxyItem = (props: Props) => { } }, [proxy]); - const delayRef = useRef(false); - const onDelay = (e: any) => { + const onDelay = useLockFn(async (e: any) => { e.preventDefault(); e.stopPropagation(); - if (delayRef.current) return; - delayRef.current = true; - - delayManager + return delayManager .checkDelay(proxy.name, groupName) .then((result) => setDelay(result)) - .catch(() => setDelay(1e6)) - .finally(() => (delayRef.current = false)); - }; + .catch(() => setDelay(1e6)); + }); return ( diff --git a/src/services/delay.ts b/src/services/delay.ts index 9cdf90c..f898a79 100644 --- a/src/services/delay.ts +++ b/src/services/delay.ts @@ -48,32 +48,36 @@ class DelayManager { names: readonly string[]; groupName: string; skipNum: number; - maxTimeout: number; }, callback: Function ) { - let names = [...options.names]; - const { groupName, skipNum, maxTimeout } = options; + const { groupName, skipNum } = options; - while (names.length) { - const list = names.slice(0, skipNum); - names = names.slice(skipNum); + const names = [...options.names]; + const total = names.length; - let called = false; - setTimeout(() => { - if (!called) { - called = true; - callback(); - } - }, maxTimeout); + let count = 0; + let current = 0; - await Promise.all(list.map((n) => this.checkDelay(n, groupName))); + return new Promise((resolve) => { + const help = async (): Promise => { + if (current >= skipNum) return; - if (!called) { - called = true; - callback(); - } - } + const task = names.shift(); + if (!task) return; + + current += 1; + await this.checkDelay(task, groupName); + current -= 1; + + if (count++ % skipNum === 0 || count === total) callback(); + if (count === total) resolve(null); + + return help(); + }; + + for (let i = 0; i < skipNum; ++i) help(); + }); } }