diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index 44d6a37ae..64e787892 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -96,12 +96,11 @@ pub async fn import_profile(url: std::string::String, option: Option) if let Some(uid) = &item.uid { logging!(info, Type::Cmd, "[导入订阅] 发送配置变更通知: {}", uid); - handle::Handle::notify_profile_changed(uid.clone()); + handle::Handle::notify_profile_changed(uid); } // 异步保存配置文件并发送全局通知 - let uid_clone = item.uid.clone(); - if let Some(uid) = uid_clone { + if let Some(uid) = &item.uid { // 延迟发送,确保文件已完全写入 tokio::time::sleep(Duration::from_millis(100)).await; logging!(info, Type::Cmd, "[导入订阅] 发送配置变更通知: {}", uid); @@ -137,7 +136,7 @@ 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.clone() { + if let Some(uid) = &item.uid { logging!(info, Type::Cmd, "[创建订阅] 发送配置变更通知: {}", uid); handle::Handle::notify_profile_changed(uid); } @@ -184,7 +183,7 @@ pub async fn delete_profile(index: String) -> CmdResult { handle::Handle::refresh_clash(); // 发送配置变更通知 logging!(info, Type::Cmd, "[删除订阅] 发送配置变更通知: {}", index); - handle::Handle::notify_profile_changed(index); + handle::Handle::notify_profile_changed(&index); AutoBackupManager::trigger_backup(AutoBackupTrigger::ProfileChange); } Err(e) => { @@ -315,7 +314,7 @@ async fn handle_success(current_value: Option<&String>) -> CmdResult { && WindowManager::get_main_window().is_some() { logging!(info, Type::Cmd, "向前端发送配置变更事件: {}", current); - handle::Handle::notify_profile_changed(current.to_owned()); + handle::Handle::notify_profile_changed(current); } Ok(true) @@ -434,7 +433,7 @@ pub async fn patch_profile(index: String, profile: PrfItem) -> CmdResult { logging!(error, Type::Timer, "刷新定时器失败: {}", e); } else { // 刷新成功后发送自定义事件,不触发配置重载 - crate::core::handle::Handle::notify_timer_updated(index); + crate::core::handle::Handle::notify_timer_updated(&index); } }); } diff --git a/src-tauri/src/core/handle.rs b/src-tauri/src/core/handle.rs index d58ec706e..b94e711d4 100644 --- a/src-tauri/src/core/handle.rs +++ b/src-tauri/src/core/handle.rs @@ -44,27 +44,26 @@ impl Handle { Self::send_event(FrontendEvent::RefreshVerge); } - pub fn notify_profile_changed(profile_id: String) { + pub fn notify_profile_changed(profile_id: &String) { Self::send_event(FrontendEvent::ProfileChanged { current_profile_id: profile_id, }); } - pub fn notify_timer_updated(profile_index: String) { + pub fn notify_timer_updated(profile_index: &String) { Self::send_event(FrontendEvent::TimerUpdated { profile_index }); } - pub fn notify_profile_update_started(uid: String) { + pub fn notify_profile_update_started(uid: &String) { Self::send_event(FrontendEvent::ProfileUpdateStarted { uid }); } - pub fn notify_profile_update_completed(uid: String) { + pub fn notify_profile_update_completed(uid: &String) { Self::send_event(FrontendEvent::ProfileUpdateCompleted { uid }); } - // TODO 利用 &str 等缩短 Clone - pub fn notice_message, M: Into>(status: S, msg: M) { - let status_str = status.into(); + pub fn notice_message, M: Into>(status: S, msg: M) { + let status_str = status.as_ref(); let msg_str = msg.into(); Self::send_event(FrontendEvent::NoticeMessage { diff --git a/src-tauri/src/core/notification.rs b/src-tauri/src/core/notification.rs index b231ba703..782835277 100644 --- a/src-tauri/src/core/notification.rs +++ b/src-tauri/src/core/notification.rs @@ -5,16 +5,15 @@ use smartstring::alias::String; use tauri::{Emitter as _, WebviewWindow}; -// TODO 重构或优化,避免 Clone 过多 -#[derive(Debug, Clone)] -pub enum FrontendEvent { +#[derive(Debug)] +pub enum FrontendEvent<'a> { RefreshClash, RefreshVerge, - NoticeMessage { status: String, message: String }, - ProfileChanged { current_profile_id: String }, - TimerUpdated { profile_index: String }, - ProfileUpdateStarted { uid: String }, - ProfileUpdateCompleted { uid: String }, + NoticeMessage { status: &'a str, message: String }, + ProfileChanged { current_profile_id: &'a String }, + TimerUpdated { profile_index: &'a String }, + ProfileUpdateStarted { uid: &'a String }, + ProfileUpdateCompleted { uid: &'a String }, } #[derive(Debug)] diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index 38afb096e..844824734 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -409,12 +409,12 @@ impl Timer { } /// Emit update events for frontend notification - fn emit_update_event(_uid: &str, _is_start: bool) { + fn emit_update_event(uid: &String, is_start: bool) { { - if _is_start { - super::handle::Handle::notify_profile_update_started(_uid.into()); + if is_start { + super::handle::Handle::notify_profile_update_started(uid); } else { - super::handle::Handle::notify_profile_update_completed(_uid.into()); + super::handle::Handle::notify_profile_update_completed(uid); } } } diff --git a/src-tauri/src/utils/resolve/scheme.rs b/src-tauri/src/utils/resolve/scheme.rs index 35c1ae507..f5ba6fe26 100644 --- a/src-tauri/src/utils/resolve/scheme.rs +++ b/src-tauri/src/utils/resolve/scheme.rs @@ -116,7 +116,7 @@ async fn fetch_profile_item(url: &str, name: Option<&String>) -> Option async fn post_import_updates(uid: &String, had_current_profile: bool) { handle::Handle::refresh_verge(); - handle::Handle::notify_profile_changed(uid.clone()); + handle::Handle::notify_profile_changed(uid); tokio::time::sleep(Duration::from_millis(100)).await; let should_update_core = if uid.is_empty() || had_current_profile { @@ -125,7 +125,7 @@ async fn post_import_updates(uid: &String, had_current_profile: bool) { let profiles = Config::profiles().await; profiles.latest_arc().is_current_profile_index(uid) }; - handle::Handle::notify_profile_changed(uid.clone()); + handle::Handle::notify_profile_changed(uid); if should_update_core { refresh_core_config().await;