性能优化
This commit is contained in:
@@ -10,7 +10,7 @@ import { invoke } from '@tauri-apps/api/core'
|
||||
import { appDataDir } from '@tauri-apps/api/path'
|
||||
import { revealItemInDir } from '@tauri-apps/plugin-opener'
|
||||
import { useProxyStore, type ProxyNode } from '@/stores/proxyStore'
|
||||
import { useModuleTabs } from '@/lib/useModuleTabs'
|
||||
import { useModuleTabs } from '@/lib/use-module-tabs'
|
||||
import { useModuleTabsStore } from '@/stores/moduleTabsStore'
|
||||
import { createLogger } from '@/lib/logger'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
@@ -98,6 +98,8 @@ const autoSwitchInterval = ref(5) // 分钟
|
||||
const autoSwitchTargetGroup = ref('') // 目标代理组
|
||||
const autoSwitchRegion = ref('') // 空=全部,否则按地区过滤
|
||||
let autoSwitchTimer: ReturnType<typeof setInterval> | null = null
|
||||
/** 自动切换执行中标志(防重入:测速超时时上一轮未结束,间隔触发会重叠) */
|
||||
let autoSwitchRunning = false
|
||||
|
||||
// 从 store.settings 同步自动切换设置
|
||||
const syncAutoSwitchSettings = () => {
|
||||
@@ -307,30 +309,41 @@ const loadProxiesWithError = async () => {
|
||||
}
|
||||
|
||||
const init = async () => {
|
||||
// 获取 appData 路径,用于将内核路径替换为 %APPDATA% 形式
|
||||
try {
|
||||
appDataPath.value = await appDataDir()
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
await Promise.all([store.loadSettings(), store.refreshKernel(), store.refreshStatus()])
|
||||
// 同步持久化的自动切换设置
|
||||
syncAutoSwitchSettings()
|
||||
if (running.value) {
|
||||
await store.waitForApi()
|
||||
store.refreshVersion()
|
||||
loadProxiesWithError()
|
||||
// 若自动切换已开启,恢复定时器
|
||||
if (autoSwitchEnabled.value) {
|
||||
startAutoSwitch()
|
||||
// 获取 appData 路径,用于将内核路径替换为 %APPDATA% 形式
|
||||
try {
|
||||
appDataPath.value = await appDataDir()
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
try {
|
||||
await Promise.all([store.loadSettings(), store.refreshKernel(), store.refreshStatus()])
|
||||
} catch (e) {
|
||||
logger.error('代理初始化失败: ' + e)
|
||||
toast.error('代理模块初始化失败', { description: String(e) })
|
||||
}
|
||||
// 同步持久化的自动切换设置
|
||||
syncAutoSwitchSettings()
|
||||
if (running.value) {
|
||||
await store.waitForApi()
|
||||
store.refreshVersion()
|
||||
loadProxiesWithError()
|
||||
// 若自动切换已开启,恢复定时器
|
||||
if (autoSwitchEnabled.value) {
|
||||
startAutoSwitch()
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// 无论初始化链路是否出错,都结束"加载中"占位,避免模块永久卡死
|
||||
store.initialized = true
|
||||
}
|
||||
store.initialized = true
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
init()
|
||||
statusTimer = setInterval(async () => {
|
||||
// 窗口/标签页不可见时暂停状态轮询,恢复可见后下个 tick 自动继续
|
||||
if (document.hidden) return
|
||||
await store.refreshStatus()
|
||||
}, 3000)
|
||||
})
|
||||
@@ -345,17 +358,27 @@ watch(running, async (val, old) => {
|
||||
await store.waitForApi()
|
||||
await store.refreshVersion()
|
||||
await loadProxiesWithError()
|
||||
// 自动切换若已开启,mihomo 启动/重启后恢复定时器
|
||||
// (handleStop 会停掉旧定时器,此处统一接管启动路径,避免开关显示开但功能静默失效)
|
||||
if (autoSwitchEnabled.value) {
|
||||
startAutoSwitch()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// 切换到节点 Tab 时,加载节点列表并自动测速
|
||||
// 切换到节点 Tab 时,加载节点列表并自动测速(10s 节流:快速切换 Tab 时避免重复 IPC 洪峰)
|
||||
let lastAutoTestAt = 0
|
||||
watch(activeTab, async (tab) => {
|
||||
if (tab === 'proxies' && running.value) {
|
||||
if (!Object.keys(store.proxies).length) {
|
||||
await loadProxiesWithError()
|
||||
}
|
||||
// 自动对所有组测速一次
|
||||
autoTestAllGroups()
|
||||
const now = Date.now()
|
||||
if (now - lastAutoTestAt > 10000) {
|
||||
lastAutoTestAt = now
|
||||
// 自动对所有组测速一次
|
||||
autoTestAllGroups()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -499,13 +522,14 @@ const onAutoSwitchIntervalChange = (val: unknown) => {
|
||||
}
|
||||
|
||||
const runAutoSwitch = async () => {
|
||||
const groupName = autoSwitchTargetGroup.value || mainGroupName.value
|
||||
if (!groupName || !running.value) return
|
||||
const nodes = filteredNodes.value
|
||||
if (!nodes.length) return
|
||||
|
||||
toast.info('正在测试节点延迟...')
|
||||
if (autoSwitchRunning) return
|
||||
autoSwitchRunning = true
|
||||
try {
|
||||
const groupName = autoSwitchTargetGroup.value || mainGroupName.value
|
||||
if (!groupName || !running.value) return
|
||||
const nodes = filteredNodes.value
|
||||
if (!nodes.length) return
|
||||
|
||||
// 使用 testDelayBatch 测速,它会更新 store.proxies[name].history,
|
||||
// 确保 UI 显示的延迟与选优结果一致
|
||||
await store.testDelayBatch(nodes)
|
||||
@@ -532,13 +556,11 @@ const runAutoSwitch = async () => {
|
||||
toast.success('已自动切换到最优节点', {
|
||||
description: `${best.name} (${best.delay}ms)`
|
||||
})
|
||||
} else {
|
||||
toast.success('当前节点已是最优', {
|
||||
description: `${best.name} (${best.delay}ms)`
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error('自动切换失败: ' + e)
|
||||
} finally {
|
||||
autoSwitchRunning = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1465,7 +1487,7 @@ tabsStore.registerSave(saveSettingsForm)
|
||||
</div>
|
||||
<p class="text-xs text-muted-foreground truncate">{{ p.url }}</p>
|
||||
<p class="text-xs text-muted-foreground">
|
||||
{{ formatSize(p.size) }} · 更新于 {{ p.updatedAt }}
|
||||
{{ formatSize(p.size ?? 0) }} · 更新于 {{ p.updatedAt }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex gap-1 shrink-0">
|
||||
|
||||
Reference in New Issue
Block a user