From a7213df1356b9de2398c97ac56d1aaa51d432b20 Mon Sep 17 00:00:00 2001 From: Zephyruso <127948745+Zephyruso@users.noreply.github.com> Date: Fri, 8 Sep 2023 15:22:07 +0800 Subject: [PATCH] refactor(proxies): use connection msg instead of active connections for better performance --- src/signals/connections.ts | 4 ++-- src/signals/proxies.ts | 28 +++++++++++++++++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/signals/connections.ts b/src/signals/connections.ts index 019e3bf..8e51a24 100644 --- a/src/signals/connections.ts +++ b/src/signals/connections.ts @@ -69,7 +69,7 @@ export const useConnections = () => { } } -function restructRawMsgToConnection( +export function restructRawMsgToConnection( connections: ConnectionRawMessage[], prevActiveConnections: Connection[], ): Connection[] { @@ -95,7 +95,7 @@ function restructRawMsgToConnection( }) } -function mergeAllConnections(activeConns: Connection[]) { +export function mergeAllConnections(activeConns: Connection[]) { return unionWith(allConnections(), activeConns, (a, b) => a.id === b.id) } diff --git a/src/signals/proxies.ts b/src/signals/proxies.ts index 9214942..ce3c5cf 100644 --- a/src/signals/proxies.ts +++ b/src/signals/proxies.ts @@ -6,7 +6,11 @@ import { useRequest, } from '~/signals' import type { Proxy, ProxyNode, ProxyProvider } from '~/types' -import { useConnections } from './connections' +import { + latestConnectionMsg, + mergeAllConnections, + restructRawMsgToConnection, +} from './connections' type ProxyInfo = { name: string @@ -47,7 +51,6 @@ const setProxiesInfo = (proxies: (Proxy | ProxyNode)[]) => { export const useProxies = () => { const request = useRequest() - const { activeConnections } = useConnections() const updateProxies = async () => { const [{ providers }, { proxies }] = await Promise.all([ @@ -89,11 +92,22 @@ export const useProxies = () => { }) if (autoCloseConns()) { - activeConnections().forEach(({ id, chains }) => { - if (chains.includes(proxy.name)) { - request.delete(`connections/${id}`) - } - }) + // we dont use activeConns from useConnection here for better performance + // and we use empty array to restruct msg because they are closed and they won't have speed anyway + + const activeConns = restructRawMsgToConnection( + latestConnectionMsg()?.connections ?? [], + [], + ) + + if (activeConns.length > 0) { + activeConns.forEach(({ id, chains }) => { + if (chains.includes(proxy.name)) { + request.delete(`connections/${id}`) + } + }) + mergeAllConnections(activeConns) + } } proxyGroup.now = proxyName