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:
@@ -1,25 +1,25 @@
|
||||
use crate::{
|
||||
cmd,
|
||||
config::{Config, PrfItem, PrfOption},
|
||||
core::{handle, CoreManager, *},
|
||||
config::{profiles::profiles_draft_update_item_safe, Config, PrfItem, PrfOption},
|
||||
core::{handle, tray, CoreManager},
|
||||
logging,
|
||||
process::AsyncHandler,
|
||||
utils::logging::Type,
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
/// Toggle proxy profile
|
||||
pub fn toggle_proxy_profile(profile_index: String) {
|
||||
AsyncHandler::spawn(|| async move {
|
||||
match cmd::patch_profiles_config_by_profile_index(profile_index).await {
|
||||
Ok(_) => {
|
||||
let _ = tray::Tray::global().update_menu();
|
||||
}
|
||||
Err(err) => {
|
||||
log::error!(target: "app", "{err}");
|
||||
pub async fn toggle_proxy_profile(profile_index: String) {
|
||||
match cmd::patch_profiles_config_by_profile_index(profile_index).await {
|
||||
Ok(_) => {
|
||||
let result = tray::Tray::global().update_menu().await;
|
||||
if let Err(err) = result {
|
||||
logging!(error, Type::Tray, true, "更新菜单失败: {}", err);
|
||||
}
|
||||
}
|
||||
});
|
||||
Err(err) => {
|
||||
log::error!(target: "app", "{err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Update a profile
|
||||
@@ -34,7 +34,7 @@ pub async fn update_profile(
|
||||
let auto_refresh = auto_refresh.unwrap_or(true); // 默认为true,保持兼容性
|
||||
|
||||
let url_opt = {
|
||||
let profiles = Config::profiles();
|
||||
let profiles = Config::profiles().await;
|
||||
let profiles = profiles.latest_ref();
|
||||
let item = profiles.get_item(&uid)?;
|
||||
let is_remote = item.itype.as_ref().is_some_and(|s| s == "remote");
|
||||
@@ -69,11 +69,13 @@ pub async fn update_profile(
|
||||
match PrfItem::from_url(&url, None, None, merged_opt.clone()).await {
|
||||
Ok(item) => {
|
||||
log::info!(target: "app", "[订阅更新] 更新订阅配置成功");
|
||||
let profiles = Config::profiles();
|
||||
let mut profiles = profiles.draft_mut();
|
||||
profiles.update_item(uid.clone(), item)?;
|
||||
let profiles = Config::profiles().await;
|
||||
|
||||
let is_current = Some(uid.clone()) == profiles.get_current();
|
||||
// 使用Send-safe helper函数
|
||||
let result = profiles_draft_update_item_safe(uid.clone(), item).await;
|
||||
result?;
|
||||
|
||||
let is_current = Some(uid.clone()) == profiles.latest_ref().get_current();
|
||||
log::info!(target: "app", "[订阅更新] 是否为当前使用的订阅: {is_current}");
|
||||
is_current && auto_refresh
|
||||
}
|
||||
@@ -105,9 +107,10 @@ pub async fn update_profile(
|
||||
}
|
||||
|
||||
// 更新到配置
|
||||
let profiles = Config::profiles();
|
||||
let mut profiles = profiles.draft_mut();
|
||||
profiles.update_item(uid.clone(), item.clone())?;
|
||||
let profiles = Config::profiles().await;
|
||||
|
||||
// 使用 Send-safe 方法进行数据操作
|
||||
profiles_draft_update_item_safe(uid.clone(), item.clone()).await?;
|
||||
|
||||
// 获取配置名称用于通知
|
||||
let profile_name = item.name.clone().unwrap_or_else(|| uid.clone());
|
||||
@@ -115,7 +118,7 @@ pub async fn update_profile(
|
||||
// 发送通知告知用户自动更新使用了回退机制
|
||||
handle::Handle::notice_message("update_with_clash_proxy", profile_name);
|
||||
|
||||
let is_current = Some(uid.clone()) == profiles.get_current();
|
||||
let is_current = Some(uid.clone()) == profiles.data_ref().get_current();
|
||||
log::info!(target: "app", "[订阅更新] 是否为当前使用的订阅: {is_current}");
|
||||
is_current && auto_refresh
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user