feat: lock some async functions

This commit is contained in:
GyDi 2022-02-17 01:42:25 +08:00
parent 74bbd3c3a2
commit 0ff8bb8090
No known key found for this signature in database
GPG Key ID: 1C95E0D3467B3084

View File

@ -29,27 +29,33 @@ interface Props {
const ProxyGroup = ({ group }: Props) => { const ProxyGroup = ({ group }: Props) => {
const { mutate } = useSWRConfig(); const { mutate } = useSWRConfig();
const listRef = useRef<any>();
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const [now, setNow] = useState(group.now); const [now, setNow] = useState(group.now);
const virtuosoRef = useRef<any>();
const proxies = group.all ?? []; const proxies = group.all ?? [];
const selectLockRef = useRef(false);
const onSelect = async (name: string) => { const onSelect = async (name: string) => {
// can not call update // Todo: support another proxy group type
if (group.type !== "Selector") { if (group.type !== "Selector") return;
// Todo
// error Tips if (selectLockRef.current) return;
return; selectLockRef.current = true;
}
const oldValue = now; const oldValue = now;
try { try {
setNow(name); setNow(name);
await updateProxy(group.name, name); await updateProxy(group.name, name);
} catch {
setNow(oldValue);
return; // do not update profile
} finally {
selectLockRef.current = false;
}
const profiles = await getProfiles().catch(console.error); try {
if (!profiles) return; const profiles = await getProfiles();
const profile = profiles.items![profiles.current!]!; const profile = profiles.items![profiles.current!]!;
if (!profile) return; if (!profile) return;
if (!profile.selected) profile.selected = []; if (!profile.selected) profile.selected = [];
@ -63,12 +69,9 @@ const ProxyGroup = ({ group }: Props) => {
} else { } else {
profile.selected[index] = { name: group.name, now: name }; profile.selected[index] = { name: group.name, now: name };
} }
await patchProfile(profiles.current!, profile);
patchProfile(profiles.current!, profile).catch(console.error); } catch (err) {
} catch { console.error(err);
setNow(oldValue);
// Todo
// error tips
} }
}; };
@ -76,7 +79,7 @@ const ProxyGroup = ({ group }: Props) => {
const index = proxies.findIndex((p) => p.name === now); const index = proxies.findIndex((p) => p.name === now);
if (index >= 0) { if (index >= 0) {
listRef.current?.scrollToIndex?.({ virtuosoRef.current?.scrollToIndex?.({
index, index,
align: "center", align: "center",
behavior: "smooth", behavior: "smooth",
@ -84,12 +87,18 @@ const ProxyGroup = ({ group }: Props) => {
} }
}; };
const checkLockRef = useRef(false);
const onCheckAll = async () => { const onCheckAll = async () => {
let names = proxies.map((p) => p.name); if (checkLockRef.current) return;
checkLockRef.current = true;
// rerender quickly
if (proxies.length) setTimeout(() => mutate("getProxies"), 500);
let names = proxies.map((p) => p.name);
while (names.length) { while (names.length) {
const list = names.slice(0, 10); const list = names.slice(0, 8);
names = names.slice(10); names = names.slice(8);
await Promise.all( await Promise.all(
list.map((n) => delayManager.checkDelay(n, group.name)) list.map((n) => delayManager.checkDelay(n, group.name))
@ -97,6 +106,8 @@ const ProxyGroup = ({ group }: Props) => {
mutate("getProxies"); mutate("getProxies");
} }
checkLockRef.current = false;
}; };
return ( return (
@ -130,7 +141,7 @@ const ProxyGroup = ({ group }: Props) => {
{proxies.length >= 10 ? ( {proxies.length >= 10 ? (
<Virtuoso <Virtuoso
ref={listRef} ref={virtuosoRef}
style={{ height: "320px", marginBottom: "4px" }} style={{ height: "320px", marginBottom: "4px" }}
totalCount={proxies.length} totalCount={proxies.length}
itemContent={(index) => ( itemContent={(index) => (