From 22e2e751a21d0c5a8eb864219c40f8bdc615f611 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Wed, 26 Nov 2025 04:06:57 +0800 Subject: [PATCH] refactor(profile): update get_profiles to return SharedBox and optimize clone usage --- src-tauri/src/cmd/profile.rs | 20 +++++++++++++------- src-tauri/src/core/notification.rs | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index 37db0f3de..861040066 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -16,6 +16,7 @@ use crate::{ ret_err, utils::{dirs, help}, }; +use clash_verge_draft::SharedBox; use clash_verge_logging::{Type, logging}; use scopeguard::defer; use smartstring::alias::String; @@ -25,10 +26,10 @@ use std::time::Duration; static CURRENT_SWITCHING_PROFILE: AtomicBool = AtomicBool::new(false); #[tauri::command] -pub async fn get_profiles() -> CmdResult { +pub async fn get_profiles() -> CmdResult> { logging!(debug, Type::Cmd, "获取配置文件列表"); let draft = Config::profiles().await; - let data = (**draft.data_arc()).clone(); + let data = draft.data_arc(); Ok(data) } @@ -92,7 +93,7 @@ pub async fn import_profile(url: std::string::String, option: Option) return Err(format!("导入订阅失败: {}", e).into()); } } - // 立即发送配置变更通知 + if let Some(uid) = &item.uid { logging!(info, Type::Cmd, "[导入订阅] 发送配置变更通知: {}", uid); handle::Handle::notify_profile_changed(uid.clone()); @@ -103,6 +104,7 @@ pub async fn import_profile(url: std::string::String, option: Option) if let Some(uid) = uid_clone { // 延迟发送,确保文件已完全写入 tokio::time::sleep(Duration::from_millis(100)).await; + logging!(info, Type::Cmd, "[导入订阅] 发送配置变更通知: {}", uid); handle::Handle::notify_profile_changed(uid); } @@ -135,9 +137,9 @@ pub async fn create_profile(item: PrfItem, file_data: Option) -> CmdResu match profiles_append_item_with_filedata_safe(&item, file_data).await { Ok(_) => { // 发送配置变更通知 - if let Some(uid) = &item.uid { + if let Some(uid) = item.uid.clone() { logging!(info, Type::Cmd, "[创建订阅] 发送配置变更通知: {}", uid); - handle::Handle::notify_profile_changed(uid.clone()); + handle::Handle::notify_profile_changed(uid); } Config::profiles().await.apply(); AutoBackupManager::trigger_backup(AutoBackupTrigger::ProfileChange); @@ -493,7 +495,7 @@ pub async fn view_profile(index: String) -> CmdResult { .get_item(&index) .stringify_err()? .file - .clone() + .as_ref() .ok_or("the file field is null")?; let path = dirs::app_profiles_dir() @@ -513,7 +515,11 @@ pub async fn read_profile_file(index: String) -> CmdResult { let profiles = Config::profiles().await; let profiles_ref = profiles.latest_arc(); PrfItem { - file: profiles_ref.get_item(&index).stringify_err()?.file.clone(), + file: profiles_ref + .get_item(&index) + .stringify_err()? + .file + .to_owned(), ..Default::default() } }; diff --git a/src-tauri/src/core/notification.rs b/src-tauri/src/core/notification.rs index 52cac30eb..472854a4c 100644 --- a/src-tauri/src/core/notification.rs +++ b/src-tauri/src/core/notification.rs @@ -13,6 +13,7 @@ use std::{ }; use tauri::{Emitter as _, WebviewWindow}; +// TODO 重构或优化,避免 Clone 过多 #[derive(Debug, Clone)] pub enum FrontendEvent { RefreshClash,