Refactor async runtime usage with AsyncHandler wrapper

This commit is contained in:
Tunglies 2025-04-11 17:47:08 +08:00
parent b6a6f5f434
commit 51ef1329be
7 changed files with 55 additions and 38 deletions

View File

@ -49,7 +49,7 @@ pub fn open_devtools(app_handle: tauri::AppHandle) {
/// 退出应用 /// 退出应用
#[tauri::command] #[tauri::command]
pub fn exit_app() { pub fn exit_app() {
feat::quit(Some(0)); feat::quit();
} }
/// 重启应用 /// 重启应用

View File

@ -1,5 +1,7 @@
use super::CmdResult; use super::CmdResult;
use crate::{config::*, core::*, feat, module::mihomo::MihomoManager, wrap_err}; use crate::{
config::*, core::*, feat, module::mihomo::MihomoManager, process::AsyncHandler, wrap_err,
};
use serde_yaml::Mapping; use serde_yaml::Mapping;
/// 复制Clash环境变量 /// 复制Clash环境变量
@ -104,10 +106,9 @@ pub fn apply_dns_config(apply: bool) -> CmdResult {
core::{handle, CoreManager}, core::{handle, CoreManager},
utils::dirs, utils::dirs,
}; };
use tauri::async_runtime;
// 使用spawn来处理异步操作 // 使用spawn来处理异步操作
async_runtime::spawn(async move { AsyncHandler::spawn(move || async move {
if apply { if apply {
// 读取DNS配置文件 // 读取DNS配置文件
let dns_path = match dirs::app_home_dir() { let dns_path = match dirs::app_home_dir() {

View File

@ -3,13 +3,14 @@ use crate::{
core::handle, core::handle,
feat, logging, logging_error, feat, logging, logging_error,
module::lightweight::entry_lightweight_mode, module::lightweight::entry_lightweight_mode,
process::AsyncHandler,
utils::{logging::Type, resolve}, utils::{logging::Type, resolve},
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{collections::HashMap, sync::Arc}; use std::{collections::HashMap, sync::Arc};
use tauri::{async_runtime, Manager}; use tauri::Manager;
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState}; use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState};
pub struct Hotkey { pub struct Hotkey {
@ -153,7 +154,7 @@ impl Hotkey {
); );
// 使用 spawn_blocking 来确保在正确的线程上执行 // 使用 spawn_blocking 来确保在正确的线程上执行
async_runtime::spawn_blocking(|| { AsyncHandler::spawn_blocking(|| {
logging!(debug, Type::Hotkey, "Toggle dashboard window visibility"); logging!(debug, Type::Hotkey, "Toggle dashboard window visibility");
// 检查窗口是否存在 // 检查窗口是否存在
@ -195,7 +196,7 @@ impl Hotkey {
"toggle_system_proxy" => || feat::toggle_system_proxy(), "toggle_system_proxy" => || feat::toggle_system_proxy(),
"toggle_tun_mode" => || feat::toggle_tun_mode(None), "toggle_tun_mode" => || feat::toggle_tun_mode(None),
"entry_lightweight_mode" => || entry_lightweight_mode(), "entry_lightweight_mode" => || entry_lightweight_mode(),
"quit" => || feat::quit(Some(0)), "quit" => || feat::quit(),
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
"hide" => || feat::hide(), "hide" => || feat::hide(),

View File

@ -691,7 +691,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
"restart_app" => feat::restart_app(), "restart_app" => feat::restart_app(),
"entry_lightweight_mode" => entry_lightweight_mode(), "entry_lightweight_mode" => entry_lightweight_mode(),
"quit" => { "quit" => {
feat::quit(Some(0)); feat::quit();
} }
id if id.starts_with("profiles_") => { id if id.starts_with("profiles_") => {
let profile_index = &id["profiles_".len()..]; let profile_index = &id["profiles_".len()..];

View File

@ -50,7 +50,7 @@ pub fn open_or_close_dashboard() {
} }
/// 优化的应用退出函数 /// 优化的应用退出函数
pub fn quit(code: Option<i32>) { pub fn quit() {
log::debug!(target: "app", "启动退出流程"); log::debug!(target: "app", "启动退出流程");
// 获取应用句柄并设置退出标志 // 获取应用句柄并设置退出标志
@ -64,47 +64,46 @@ pub fn quit(code: Option<i32>) {
// 在单独线程中处理资源清理,避免阻塞主线程 // 在单独线程中处理资源清理,避免阻塞主线程
std::thread::spawn(move || { std::thread::spawn(move || {
// 使用tokio运行时执行异步清理任务 use tokio::time::{timeout, Duration};
tauri::async_runtime::block_on(async { let rt = tokio::runtime::Runtime::new().unwrap();
// 使用超时机制处理清理操作 let cleanup_result = rt.block_on(async {
use tokio::time::{timeout, Duration}; // 1. 处理TUN模式
let tun_success = if Config::verge().data().enable_tun_mode.unwrap_or(false) {
// 1. 直接关闭TUN模式 (优先处理,通常最容易卡住) let disable_tun = serde_json::json!({
if Config::verge().data().enable_tun_mode.unwrap_or(false) {
let disable = serde_json::json!({
"tun": { "tun": {
"enable": false "enable": false
} }
}); });
timeout(
// 设置1秒超时
let _ = timeout(
Duration::from_secs(1), Duration::from_secs(1),
MihomoManager::global().patch_configs(disable), MihomoManager::global().patch_configs(disable_tun),
) )
.await; .await
} .is_ok()
} else {
true
};
// 2. 并行处理系统代理和核心进程清理 // 2. 顺序执行关键清理
let proxy_future = timeout( let proxy_res = timeout(
Duration::from_secs(1), Duration::from_secs(1),
sysopt::Sysopt::global().reset_sysproxy(), sysopt::Sysopt::global().reset_sysproxy(),
); )
.await;
let core_future = timeout(Duration::from_secs(1), CoreManager::global().stop_core()); let core_res = timeout(Duration::from_secs(1), CoreManager::global().stop_core()).await;
// 同时等待两个任务完成 // 3. 平台特定清理
let _ = futures::join!(proxy_future, core_future);
// 3. 处理macOS特定清理
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ let _dns_res = timeout(Duration::from_millis(500), resolve::restore_public_dns()).await;
let _ = timeout(Duration::from_millis(500), resolve::restore_public_dns()).await;
} tun_success && proxy_res.is_ok() && core_res.is_ok()
}); });
// 无论清理结果如何,确保应用退出 app_handle.exit(match cleanup_result {
app_handle.exit(code.unwrap_or(0)); true => 0,
false => 1,
});
}); });
} }

View File

@ -86,7 +86,7 @@ impl AppHandleManager {
#[allow(clippy::panic)] #[allow(clippy::panic)]
pub fn run() { pub fn run() {
// 单例检测 // 单例检测
let app_exists: bool = tauri::async_runtime::block_on(async move { let app_exists: bool = AsyncHandler::block_on(move || async move {
if server::check_singleton().await.is_err() { if server::check_singleton().await.is_err() {
println!("app exists"); println!("app exists");
true true
@ -134,7 +134,7 @@ pub fn run() {
}); });
}); });
tauri::async_runtime::block_on(async move { AsyncHandler::block_on(move || async move {
resolve::resolve_setup(app).await; resolve::resolve_setup(app).await;
}); });

View File

@ -11,4 +11,20 @@ impl AsyncHandler {
{ {
async_runtime::spawn(f()) async_runtime::spawn(f())
} }
pub fn spawn_blocking<F, R>(f: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
async_runtime::spawn_blocking(f)
}
pub fn block_on<F, Fut, R>(f: F) -> R
where
F: FnOnce() -> Fut,
Fut: Future<Output = R>,
{
async_runtime::block_on(f())
}
} }