optimized delay logic

This commit is contained in:
pompurin404 2024-09-04 14:28:54 +08:00
parent 035d573d88
commit 1cbae3a510
No known key found for this signature in database
4 changed files with 19 additions and 13 deletions

View File

@ -1,6 +1,5 @@
import { Button, Card, CardBody } from '@nextui-org/react'
import React, { useEffect, useMemo, useState } from 'react'
import PubSub from 'pubsub-js'
import React, { useMemo, useState } from 'react'
interface Props {
mutateProxies: () => void
@ -45,13 +44,6 @@ const ProxyItem: React.FC<Props> = (props) => {
})
}
useEffect(() => {
const token = PubSub.subscribe(`${group.name}-delay`, onDelay)
return (): void => {
PubSub.unsubscribe(token)
}
}, [])
const fixed = group.fixed && group.fixed === proxy.name
return (

View File

@ -6,6 +6,7 @@ import { IoMdRefresh } from 'react-icons/io'
import { useSortable } from '@dnd-kit/sortable'
import { CSS } from '@dnd-kit/utilities'
import { useLocation, useNavigate } from 'react-router-dom'
import PubSub from 'pubsub-js'
import useSWR from 'swr'
const MihomoCoreCard: React.FC = () => {

View File

@ -7,6 +7,7 @@ import { useControledMihomoConfig } from '@renderer/hooks/use-controled-mihomo-c
import { platform } from '@renderer/utils/init'
import { FaNetworkWired } from 'react-icons/fa'
import { IoMdCloudDownload } from 'react-icons/io'
import PubSub from 'pubsub-js'
import { mihomoUpgrade, restartCore, triggerSysProxy } from '@renderer/utils/ipc'
import React, { useState } from 'react'
import InterfaceModal from '@renderer/components/mihomo/interface-modal'

View File

@ -30,6 +30,7 @@ const Proxies: React.FC = () => {
} = appConfig || {}
const [cols, setCols] = useState(1)
const [isOpen, setIsOpen] = useState(Array(groups.length).fill(false))
const [delaying, setDelaying] = useState(Array(groups.length).fill(false))
const [searchValue, setSearchValue] = useState(Array(groups.length).fill(''))
const virtuosoRef = useRef<GroupedVirtuosoHandle>(null)
const { groupCounts, allProxies } = useMemo(() => {
@ -76,9 +77,19 @@ const Proxies: React.FC = () => {
return await mihomoProxyDelay(proxy, url)
}
const onGroupDelay = async (group: string, url?: string): Promise<void> => {
PubSub.publish(`${group}-delay`)
await mihomoGroupDelay(group, url)
const onGroupDelay = async (index: number): Promise<void> => {
setDelaying((prev) => {
const newDelaying = [...prev]
newDelaying[index] = true
return newDelaying
})
await mihomoGroupDelay(groups[index].name, groups[index].testUrl)
setDelaying((prev) => {
const newDelaying = [...prev]
newDelaying[index] = false
return newDelaying
})
mutate()
}
const calcCols = (): number => {
@ -267,10 +278,11 @@ const Proxies: React.FC = () => {
<Button
title="延迟测试"
variant="light"
isLoading={delaying[index]}
size="sm"
isIconOnly
onPress={() => {
onGroupDelay(groups[index].name, groups[index].testUrl)
onGroupDelay(index)
}}
>
<MdOutlineSpeed className="text-lg text-default-500" />