fix: app freezed when change group proxy

This commit is contained in:
oomeow
2025-10-02 11:40:58 +08:00
Unverified
parent 89d5281b81
commit d6d625bc3a
10 changed files with 74 additions and 78 deletions

View File

@@ -1,3 +1,6 @@
[env]
TS_RS_EXPORT_DIR = { value = "crates/tauri-plugin-mihomo/guest-js/bindings/", relative = true }
[target.aarch64-unknown-linux-gnu]
linker = "aarch64-linux-gnu-gcc"

View File

@@ -6,3 +6,10 @@ pnpm-lock.yaml
src-tauri/target/
src-tauri/gen/
target
crates/tauri-plugin-mihomo/dist-js
crates/tauri-plugin-mihomo/examples
crates/tauri-plugin-mihomo/guest-js/bindings
crates/tauri-plugin-mihomo/permissions

View File

@@ -1,14 +1,5 @@
use tauri::Emitter;
use super::CmdResult;
use crate::{
core::{
handle::{self, Handle},
tray::Tray,
},
logging,
utils::logging::Type,
};
use crate::{logging, utils::logging::Type};
// use std::time::Duration;
// const PROXIES_REFRESH_INTERVAL: Duration = Duration::from_secs(60);
@@ -73,50 +64,50 @@ pub async fn sync_tray_proxy_selection() -> CmdResult<()> {
}
}
/// 更新代理选择并同步托盘和GUI状态
#[tauri::command]
pub async fn update_proxy_and_sync(group: String, proxy: String) -> CmdResult<()> {
match handle::Handle::mihomo()
.await
.select_node_for_group(&group, &proxy)
.await
{
Ok(_) => {
// println!("Proxy updated successfully: {} -> {}", group,proxy);
logging!(
info,
Type::Cmd,
"Proxy updated successfully: {} -> {}",
group,
proxy
);
// 更新代理选择并同步托盘和GUI状态
// #[tauri::command]
// pub async fn update_proxy_and_sync(group: String, proxy: String) -> CmdResult<()> {
// match handle::Handle::mihomo()
// .await
// .select_node_for_group(&group, &proxy)
// .await
// {
// Ok(_) => {
// // println!("Proxy updated successfully: {} -> {}", group,proxy);
// logging!(
// info,
// Type::Cmd,
// "Proxy updated successfully: {} -> {}",
// group,
// proxy
// );
if let Err(e) = Tray::global().update_menu().await {
logging!(error, Type::Cmd, "Failed to sync tray menu: {}", e);
}
// if let Err(e) = Tray::global().update_menu().await {
// logging!(error, Type::Cmd, "Failed to sync tray menu: {}", e);
// }
let app_handle = Handle::app_handle();
let _ = app_handle.emit("verge://refresh-proxy-config", ());
// let app_handle = Handle::app_handle();
// let _ = app_handle.emit("verge://refresh-proxy-config", ());
logging!(
info,
Type::Cmd,
"Proxy and sync completed successfully: {} -> {}",
group,
proxy
);
Ok(())
}
Err(e) => {
logging!(
error,
Type::Cmd,
"Failed to update proxy: {} -> {}, error: {}",
group,
proxy,
e
);
Err(e.to_string())
}
}
}
// logging!(
// info,
// Type::Cmd,
// "Proxy and sync completed successfully: {} -> {}",
// group,
// proxy
// );
// Ok(())
// }
// Err(e) => {
// logging!(
// error,
// Type::Cmd,
// "Failed to update proxy: {} -> {}, error: {}",
// group,
// proxy,
// e
// );
// Err(e.to_string())
// }
// }
// }

View File

@@ -308,10 +308,10 @@ impl Handle {
}
pub fn refresh_websocket() {
if let Some(window) = Self::get_window() {
if let Err(e) = window.emit("verge://refresh-websocket", "yes") {
log::error!("{e}");
}
if let Some(window) = Self::get_window()
&& let Err(e) = window.emit("verge://refresh-websocket", "yes")
{
log::error!("{e}");
}
}

View File

@@ -1,5 +1,5 @@
use once_cell::sync::OnceCell;
use tauri::{Emitter};
use tauri::Emitter;
use tauri::tray::TrayIconBuilder;
#[cfg(target_os = "macos")]
pub mod speed_rate;
@@ -1018,14 +1018,15 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
let group_name = parts[1];
let proxy_name = parts[2];
match cmd::proxy::update_proxy_and_sync(
group_name.to_string(),
proxy_name.to_string(),
)
.await
match handle::Handle::mihomo()
.await
.select_node_for_group(group_name, proxy_name)
.await
{
Ok(_) => {
log::info!(target: "app", "切换代理成功: {} -> {}", group_name, proxy_name);
let _ = handle::Handle::app_handle()
.emit("verge://refresh-proxy-config", ());
}
Err(e) => {
log::error!(target: "app", "切换代理失败: {} -> {}, 错误: {:?}", group_name, proxy_name, e);

View File

@@ -194,7 +194,6 @@ mod app_init {
// cmd::force_refresh_proxies,
// cmd::get_providers_proxies,
cmd::sync_tray_proxy_selection,
cmd::update_proxy_and_sync,
cmd::save_dns_config,
cmd::apply_dns_config,
cmd::check_dns_config_exists,

View File

@@ -36,12 +36,11 @@ import { useTranslation } from "react-i18next";
import useSWR from "swr";
import { useAppData } from "@/providers/app-data-provider";
import { calcuProxies, updateProxyChainConfigInRuntime } from "@/services/cmds";
import {
calcuProxies,
updateProxyAndSync,
updateProxyChainConfigInRuntime,
} from "@/services/cmds";
import { closeAllConnections } from "tauri-plugin-mihomo-api";
closeAllConnections,
selectNodeForGroup,
} from "tauri-plugin-mihomo-api";
interface ProxyChainItem {
id: string;
@@ -367,7 +366,7 @@ export const ProxyChain = ({
const targetGroup = mode === "global" ? "GLOBAL" : selectedGroup;
await updateProxyAndSync(targetGroup || "GLOBAL", lastNode.name);
await selectNodeForGroup(targetGroup || "GLOBAL", lastNode.name);
localStorage.setItem("proxy-chain-group", targetGroup || "GLOBAL");
localStorage.setItem("proxy-chain-exit-node", lastNode.name);

View File

@@ -8,7 +8,7 @@ import {
import { useProfiles } from "@/hooks/use-profiles";
import { useVerge } from "@/hooks/use-verge";
import { syncTrayProxySelection, updateProxyAndSync } from "@/services/cmds";
import { syncTrayProxySelection } from "@/services/cmds";
// 缓存连接清理
const cleanupConnections = async (previousProxy: string) => {
@@ -75,7 +75,8 @@ export const useProxySelection = (options: ProxySelectionOptions = {}) => {
await patchCurrent({ selected: current.selected });
}
await updateProxyAndSync(groupName, proxyName);
await selectNodeForGroup(groupName, proxyName);
await syncTrayProxySelection();
console.log(
`[ProxySelection] 代理和状态同步完成: ${groupName} -> ${proxyName}`,
);

View File

@@ -113,10 +113,6 @@ export async function syncTrayProxySelection() {
return invoke<void>("sync_tray_proxy_selection");
}
export async function updateProxyAndSync(group: string, proxy: string) {
return invoke<void>("update_proxy_and_sync", { group, proxy });
}
export async function calcuProxies(): Promise<{
global: IProxyGroupItem;
direct: IProxyItem;

View File

@@ -1,5 +1,4 @@
// IPC-based log service using Tauri commands with streaming support
import dayjs from "dayjs";
import { clearLogs as clearLogsCmd } from "@/services/cmds";