perf(core): batch close connections on mode change

This commit is contained in:
Tunglies
2026-09-04 18:20:39 +08:00
parent 836199cfe2
commit d8b94d12dd
3 changed files with 7 additions and 25 deletions
+6 -12
View File
@@ -68,18 +68,12 @@ pub async fn restart_app() {
fn after_change_clash_mode() {
AsyncHandler::spawn(move || async {
let mihomo = handle::Handle::mihomo();
match mihomo.get_connections().await {
Ok(connections) => {
if let Some(connections_array) = connections.connections {
for connection in connections_array {
let _ = mihomo.close_connection(&connection.id).await;
}
}
}
Err(err) => {
logging!(error, Type::Core, "Failed to get connections: {err}");
}
if let Err(err) = handle::Handle::mihomo().close_all_connections().await {
logging!(
error,
Type::Core,
"Failed to close all connections after changing clash mode: {err}"
);
}
});
}
+1 -6
View File
@@ -7,10 +7,9 @@ import { Box, Paper, Stack, Typography } from '@mui/material'
import { useLockFn } from 'ahooks'
import { type ReactNode, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { type BaseConfig, closeAllConnections } from 'tauri-plugin-mihomo-api'
import type { BaseConfig } from 'tauri-plugin-mihomo-api'
import { useClashMode, useRuntimeConfig } from '@/hooks/use-clash'
import { useVerge } from '@/hooks/use-verge'
import {
useAppRefreshers,
useClashConfigData,
@@ -58,7 +57,6 @@ const MODE_ICONS: Record<ClashMode, ReactNode> = {
export const ClashModeCard = () => {
const { t } = useTranslation()
const { verge } = useVerge()
const { clashConfig } = useClashConfigData()
const { isCoreDataPending } = useCoreDataStatus()
const { refreshClashConfig } = useAppRefreshers()
@@ -89,9 +87,6 @@ export const ClashModeCard = () => {
const onChangeMode = useLockFn(async (mode: ClashMode) => {
if (mode === currentMode) return
if (verge?.auto_close_connection) {
closeAllConnections()
}
setOptimisticMode(mode)
try {
-7
View File
@@ -3,12 +3,10 @@ import { Box, Button, ButtonGroup } from '@mui/material'
import { useLockFn } from 'ahooks'
import { useCallback, useEffect, useReducer, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { closeAllConnections } from 'tauri-plugin-mihomo-api'
import { BasePage, TooltipIcon } from '@/components/base'
import { ProviderButton } from '@/components/proxy/provider-button'
import { ProxyGroups } from '@/components/proxy/proxy-groups'
import { useVerge } from '@/hooks/use-verge'
import {
useAppRefreshers,
useClashConfigData,
@@ -51,17 +49,12 @@ const ProxyPage = () => {
const updateChainConfigData = useCallback((value: string | null) => {
dispatchChainConfigData(value)
}, [])
const { verge } = useVerge()
const normalizedMode = clashConfig?.mode?.toLowerCase()
const curMode = isMode(normalizedMode) ? normalizedMode : undefined
const chainWarning = t('proxies.page.chain.warning')
const onChangeMode = useLockFn(async (mode: Mode) => {
// 断开连接
if (mode !== curMode && verge?.auto_close_connection) {
closeAllConnections()
}
try {
// patchClashMode 在后端 PATCH 失败时会 reject,需提示用户而非静默失败
await patchClashMode(mode)