mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor(async): migrate from sync-blocking async execution to true async with unified AsyncHandler::spawn (#4502)
* feat: replace all tokio::spawn with unified AsyncHandler::spawn - 🚀 Core Improvements: * Replace all tokio::spawn calls with AsyncHandler::spawn for unified Tauri async task management * Prioritize converting sync functions to async functions to reduce spawn usage * Use .await directly in async contexts instead of spawn - 🔧 Major Changes: * core/hotkey.rs: Use AsyncHandler::spawn for hotkey callback functions * module/lightweight.rs: Async lightweight mode switching * feat/window.rs: Convert window operation functions to async, use .await internally * feat/proxy.rs, feat/clash.rs: Async proxy and mode switching functions * lib.rs: Window focus handling with AsyncHandler::spawn * core/tray/mod.rs: Complete async tray event handling - ✨ Technical Advantages: * Unified task tracking and debugging capabilities (via tokio-trace feature) * Better error handling and task management * Consistency with Tauri runtime * Reduced async boundaries for better performance - 🧪 Verification: * Compilation successful with 0 errors, 0 warnings * Maintains complete original functionality * Optimized async execution flow * feat: complete tokio fs migration and replace tokio::spawn with AsyncHandler 🚀 Major achievements: - Migrate 8 core modules from std::fs to tokio::fs - Create 6 Send-safe wrapper functions using spawn_blocking pattern - Replace all tokio::spawn calls with AsyncHandler::spawn for unified async task management - Solve all 19 Send trait compilation errors through innovative spawn_blocking architecture 🔧 Core changes: - config/profiles.rs: Add profiles_*_safe functions to handle Send trait constraints - cmd/profile.rs: Update all Tauri commands to use Send-safe operations - config/prfitem.rs: Replace append_item calls with profiles_append_item_safe - utils/help.rs: Convert YAML operations to async (read_yaml, save_yaml) - Multiple modules: Replace tokio::task::spawn_blocking with AsyncHandler::spawn_blocking ✅ Technical innovations: - spawn_blocking wrapper pattern resolves parking_lot RwLock Send trait conflicts - Maintain parking_lot performance while achieving Tauri async command compatibility - Preserve backwards compatibility with gradual migration strategy 🎯 Results: - Zero compilation errors - Zero warnings - All async file operations working correctly - Complete Send trait compliance for Tauri commands * feat: refactor app handle and command functions to use async/await for improved performance * feat: update async handling in profiles and logging functions for improved error handling and performance * fix: update TRACE_MINI_SIZE constant to improve task logging threshold * fix(windows): convert service management functions to async for improved performance * fix: convert service management functions to async for improved responsiveness * fix(ubuntu): convert install and reinstall service functions to async for improved performance * fix(linux): convert uninstall_service function to async for improved performance * fix: convert uninstall_service call to async for improved performance * fix: convert file and directory creation calls to async for improved performance * fix: convert hotkey functions to async for improved responsiveness * chore: update UPDATELOG.md for v2.4.1 with major improvements and performance optimizations
This commit is contained in:
@@ -14,10 +14,7 @@ use once_cell::sync::OnceCell;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use percent_encoding::percent_decode_str;
|
||||
use scopeguard;
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::time::{Duration, Instant};
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
use tauri::Url;
|
||||
@@ -109,7 +106,7 @@ pub fn reset_ui_ready() {
|
||||
}
|
||||
|
||||
/// 异步方式处理启动后的额外任务
|
||||
pub async fn resolve_setup_async(app_handle: Arc<AppHandle>) {
|
||||
pub async fn resolve_setup_async(app_handle: &AppHandle) {
|
||||
let start_time = std::time::Instant::now();
|
||||
logging!(
|
||||
info,
|
||||
@@ -144,7 +141,7 @@ pub async fn resolve_setup_async(app_handle: Arc<AppHandle>) {
|
||||
// 启动时清理冗余的 Profile 文件
|
||||
logging!(info, Type::Setup, true, "开始清理冗余的Profile文件...");
|
||||
|
||||
match Config::profiles().latest_ref().auto_cleanup() {
|
||||
match Config::profiles().await.latest_ref().auto_cleanup() {
|
||||
Ok(_) => {
|
||||
logging!(info, Type::Setup, true, "启动时Profile文件清理完成");
|
||||
}
|
||||
@@ -165,7 +162,9 @@ pub async fn resolve_setup_async(app_handle: Arc<AppHandle>) {
|
||||
|
||||
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
||||
logging!(info, Type::Tray, true, "创建系统托盘...");
|
||||
let result = tray::Tray::global().create_tray_from_handle(app_handle);
|
||||
let result = tray::Tray::global()
|
||||
.create_tray_from_handle(&app_handle)
|
||||
.await;
|
||||
if result.is_ok() {
|
||||
logging!(info, Type::Tray, true, "系统托盘创建成功");
|
||||
} else if let Err(e) = result {
|
||||
@@ -193,7 +192,8 @@ pub async fn resolve_setup_async(app_handle: Arc<AppHandle>) {
|
||||
);
|
||||
|
||||
// 创建窗口
|
||||
let is_silent_start = { Config::verge().latest_ref().enable_silent_start }.unwrap_or(false);
|
||||
let is_silent_start =
|
||||
{ Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false);
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
if is_silent_start {
|
||||
@@ -202,18 +202,18 @@ pub async fn resolve_setup_async(app_handle: Arc<AppHandle>) {
|
||||
AppHandleManager::global().set_activation_policy_accessory();
|
||||
}
|
||||
}
|
||||
create_window(!is_silent_start);
|
||||
create_window(!is_silent_start).await;
|
||||
|
||||
// 初始化定时器
|
||||
logging_error!(Type::System, true, timer::Timer::global().init());
|
||||
logging_error!(Type::System, true, timer::Timer::global().init().await);
|
||||
|
||||
// 自动进入轻量模式
|
||||
auto_lightweight_mode_init();
|
||||
auto_lightweight_mode_init().await;
|
||||
|
||||
logging_error!(Type::Tray, true, tray::Tray::global().update_part());
|
||||
logging_error!(Type::Tray, true, tray::Tray::global().update_part().await);
|
||||
|
||||
logging!(trace, Type::System, true, "初始化热键...");
|
||||
logging_error!(Type::System, true, hotkey::Hotkey::global().init());
|
||||
logging_error!(Type::System, true, hotkey::Hotkey::global().init().await);
|
||||
|
||||
let elapsed = start_time.elapsed();
|
||||
logging!(
|
||||
@@ -257,7 +257,7 @@ pub async fn resolve_reset_async() {
|
||||
}
|
||||
|
||||
/// Create the main window
|
||||
pub fn create_window(is_show: bool) -> bool {
|
||||
pub async fn create_window(is_show: bool) -> bool {
|
||||
logging!(
|
||||
info,
|
||||
Type::Window,
|
||||
@@ -268,7 +268,7 @@ pub fn create_window(is_show: bool) -> bool {
|
||||
|
||||
if !is_show {
|
||||
logging!(info, Type::Window, true, "静默模式启动时不创建窗口");
|
||||
lightweight::set_lightweight_mode(true);
|
||||
lightweight::set_lightweight_mode(true).await;
|
||||
handle::Handle::notify_startup_completed();
|
||||
return false;
|
||||
}
|
||||
@@ -332,7 +332,7 @@ pub fn create_window(is_show: bool) -> bool {
|
||||
};
|
||||
|
||||
match tauri::WebviewWindowBuilder::new(
|
||||
&*app_handle,
|
||||
&app_handle,
|
||||
"main", /* the unique window label */
|
||||
tauri::WebviewUrl::App("index.html".into()),
|
||||
)
|
||||
@@ -425,7 +425,7 @@ pub fn create_window(is_show: bool) -> bool {
|
||||
);
|
||||
|
||||
// 先运行轻量模式检测
|
||||
lightweight::run_once_auto_lightweight();
|
||||
lightweight::run_once_auto_lightweight().await;
|
||||
|
||||
// 发送启动完成事件,触发前端开始加载
|
||||
logging!(
|
||||
@@ -631,7 +631,7 @@ pub async fn resolve_scheme(param: String) -> Result<()> {
|
||||
Some(url) => {
|
||||
log::info!(target:"app", "decoded subscription url: {url}");
|
||||
|
||||
create_window(false);
|
||||
create_window(false).await;
|
||||
match PrfItem::from_url(url.as_ref(), name, None, None).await {
|
||||
Ok(item) => {
|
||||
let uid = match item.uid.clone() {
|
||||
@@ -645,7 +645,8 @@ pub async fn resolve_scheme(param: String) -> Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
let _ = wrap_err!(Config::profiles().data_mut().append_item(item));
|
||||
let result = crate::config::profiles::profiles_append_item_safe(item).await;
|
||||
let _ = wrap_err!(result);
|
||||
handle::Handle::notice_message("import_sub_url::ok", uid);
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user