From 23d7dc86d5b87a3a34df2ae69c2caacef803ef81 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Fri, 29 Aug 2025 06:16:42 +0800 Subject: [PATCH] refactor: streamline asynchronous handling in config and resolve setup --- src-tauri/src/config/config.rs | 6 ++--- src-tauri/src/lib.rs | 36 +++++++++++++------------- src-tauri/src/process/async_handler.rs | 4 +++ src-tauri/src/utils/resolve/mod.rs | 8 +----- 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src-tauri/src/config/config.rs b/src-tauri/src/config/config.rs index ad0be9e8..aad066dc 100644 --- a/src-tauri/src/config/config.rs +++ b/src-tauri/src/config/config.rs @@ -123,10 +123,8 @@ impl Config { // 在单独的任务中发送通知 if let Some((msg_type, msg_content)) = validation_result { - AsyncHandler::spawn(move || async move { - sleep(Duration::from_secs(2)).await; - handle::Handle::notice_message(msg_type, &msg_content); - }); + sleep(Duration::from_secs(2)).await; + handle::Handle::notice_message(msg_type, &msg_content); } Ok(()) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8eb844ea..150d8146 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -133,23 +133,21 @@ mod app_init { } /// Initialize core components asynchronously - pub fn init_core_async() { - AsyncHandler::spawn(move || async move { - logging!(info, Type::Setup, true, "异步执行应用设置..."); - match timeout(Duration::from_secs(30), resolve::resolve_setup_async()).await { - Ok(_) => { - logging!(info, Type::Setup, true, "应用设置成功完成"); - } - Err(_) => { - logging!( - error, - Type::Setup, - true, - "应用设置超时(30秒),继续执行后续流程" - ); - } + pub async fn init_core_async() { + logging!(info, Type::Setup, true, "异步执行应用设置..."); + match timeout(Duration::from_secs(30), resolve::resolve_setup_async()).await { + Ok(_) => { + logging!(info, Type::Setup, true, "应用设置成功完成"); } - }); + Err(_) => { + logging!( + error, + Type::Setup, + true, + "应用设置超时(30秒),继续执行后续流程" + ); + } + } } /// Initialize core components synchronously @@ -365,12 +363,14 @@ pub fn run() { let app_handle = app.handle().clone(); // Initialize core components asynchronously - app_init::init_core_async(); + AsyncHandler::spawn_blocking(move || async move { + app_init::init_core_async().await; + }); logging!(info, Type::Setup, true, "执行主要设置操作..."); // Initialize core components synchronously - AsyncHandler::spawn(move || async move { + AsyncHandler::spawn_blocking(move || async move { if let Err(e) = app_init::init_core_sync(&app_handle).await { logging!( error, diff --git a/src-tauri/src/process/async_handler.rs b/src-tauri/src/process/async_handler.rs index a06e7067..6cf8212c 100644 --- a/src-tauri/src/process/async_handler.rs +++ b/src-tauri/src/process/async_handler.rs @@ -51,7 +51,11 @@ impl AsyncHandler { F: ?Sized, { const TRACE_MINI_SIZE: usize = 4; + const TRACE_SPECIAL_SIZE: usize = 24; let size = std::mem::size_of_val(f); + if size == TRACE_SPECIAL_SIZE { + return; + } if size <= TRACE_MINI_SIZE { return; } diff --git a/src-tauri/src/utils/resolve/mod.rs b/src-tauri/src/utils/resolve/mod.rs index 0ebd4fea..0c7d2be7 100644 --- a/src-tauri/src/utils/resolve/mod.rs +++ b/src-tauri/src/utils/resolve/mod.rs @@ -29,23 +29,17 @@ pub async fn resolve_setup_async() { init_embed_server(); }); - AsyncHandler::spawn_blocking(|| async { + AsyncHandler::spawn(|| async { init_config().await; init_core_manager().await; init_tray().await; - }); - AsyncHandler::spawn_blocking(|| async { init_system_proxy().await; init_system_proxy_guard().await; - }); - AsyncHandler::spawn_blocking(|| async { init_timer().await; init_auto_lightweight_mode().await; - }); - AsyncHandler::spawn_blocking(|| async { init_hotkey().await; init_window().await; refresh_tray_menu().await;