From b70e2022012a874fa71d58539180012ba321ff63 Mon Sep 17 00:00:00 2001 From: wonfen Date: Sun, 18 May 2025 20:58:59 +0800 Subject: [PATCH] refactor: simplify frontend event dispatch by restructuring notification system --- UPDATELOG.md | 1 + src-tauri/src/cmd/profile.rs | 13 ++--- src-tauri/src/core/handle.rs | 90 ++++++++++++++++++++++++++++++++++ src-tauri/src/core/timer.rs | 13 ++--- src-tauri/src/utils/resolve.rs | 36 ++------------ 5 files changed, 101 insertions(+), 52 deletions(-) diff --git a/UPDATELOG.md b/UPDATELOG.md index 38a39da4..3d62b584 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -72,6 +72,7 @@ - 对获取系统信息的流程进行了优化,并添加了去重检测机制,确保剔除重复的信息 - 优化窗口状态初始化逻辑和添加缺失的权限设置 - 异步化配置:优化端口查找和配置保存逻辑 + - 重构事件通知机制到独立线程,避免前端卡死 ## v2.2.3 diff --git a/src-tauri/src/cmd/profile.rs b/src-tauri/src/cmd/profile.rs index ec1197f3..165e25c0 100644 --- a/src-tauri/src/cmd/profile.rs +++ b/src-tauri/src/cmd/profile.rs @@ -6,7 +6,6 @@ use crate::{ utils::{dirs, help, logging::Type}, wrap_err, }; -use tauri::Emitter; /// 获取配置文件列表 #[tauri::command] @@ -160,11 +159,9 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult { Config::profiles().apply(); wrap_err!(Config::profiles().data().save_file())?; - if let Some(window) = handle::Handle::global().get_window() { - if let Some(current) = ¤t_value { - logging!(info, Type::Cmd, true, "向前端发送配置变更事件: {}", current); - let _ = window.emit("profile-changed", current.clone()); - } + if let Some(current) = ¤t_value { + logging!(info, Type::Cmd, true, "向前端发送配置变更事件: {}", current); + handle::Handle::notify_profile_changed(current.clone()); } Ok(true) @@ -245,9 +242,7 @@ pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult { logging!(error, Type::Timer, "刷新定时器失败: {}", e); } else { // 刷新成功后发送自定义事件,不触发配置重载 - if let Some(window) = crate::core::handle::Handle::global().get_window() { - let _ = window.emit("verge://timer-updated", index_clone); - } + crate::core::handle::Handle::notify_timer_updated(index_clone); } }); } diff --git a/src-tauri/src/core/handle.rs b/src-tauri/src/core/handle.rs index 94cc3674..807b2080 100644 --- a/src-tauri/src/core/handle.rs +++ b/src-tauri/src/core/handle.rs @@ -18,6 +18,11 @@ enum FrontendEvent { RefreshClash, RefreshVerge, NoticeMessage { status: String, message: String }, + ProfileChanged { current_profile_id: String }, + TimerUpdated { profile_index: String }, + StartupCompleted, + ProfileUpdateStarted { uid: String }, + ProfileUpdateCompleted { uid: String }, } /// 事件发送统计和监控 @@ -125,6 +130,21 @@ impl NotificationSystem { } } } + FrontendEvent::ProfileChanged { current_profile_id } => { + ("profile-changed", Ok(serde_json::json!(current_profile_id))) + } + FrontendEvent::TimerUpdated { profile_index } => { + ("verge://timer-updated", Ok(serde_json::json!(profile_index))) + } + FrontendEvent::StartupCompleted => { + ("verge://startup-completed", Ok(serde_json::json!(null))) + } + FrontendEvent::ProfileUpdateStarted { uid } => { + ("profile-update-started", Ok(serde_json::json!({ "uid": uid }))) + } + FrontendEvent::ProfileUpdateCompleted { uid } => { + ("profile-update-completed", Ok(serde_json::json!({ "uid": uid }))) + } }; if let Ok(payload) = payload_result { @@ -289,6 +309,76 @@ impl Handle { } } + pub fn notify_profile_changed(profile_id: String) { + let handle = Self::global(); + if handle.is_exiting() { + return; + } + + let system_opt = handle.notification_system.read(); + if let Some(system) = system_opt.as_ref() { + system.send_event(FrontendEvent::ProfileChanged { current_profile_id: profile_id }); + } else { + log::warn!("Notification system not initialized when trying to send ProfileChanged event."); + } + } + + pub fn notify_timer_updated(profile_index: String) { + let handle = Self::global(); + if handle.is_exiting() { + return; + } + + let system_opt = handle.notification_system.read(); + if let Some(system) = system_opt.as_ref() { + system.send_event(FrontendEvent::TimerUpdated { profile_index }); + } else { + log::warn!("Notification system not initialized when trying to send TimerUpdated event."); + } + } + + pub fn notify_startup_completed() { + let handle = Self::global(); + if handle.is_exiting() { + return; + } + + let system_opt = handle.notification_system.read(); + if let Some(system) = system_opt.as_ref() { + system.send_event(FrontendEvent::StartupCompleted); + } else { + log::warn!("Notification system not initialized when trying to send StartupCompleted event."); + } + } + + pub fn notify_profile_update_started(uid: String) { + let handle = Self::global(); + if handle.is_exiting() { + return; + } + + let system_opt = handle.notification_system.read(); + if let Some(system) = system_opt.as_ref() { + system.send_event(FrontendEvent::ProfileUpdateStarted { uid }); + } else { + log::warn!("Notification system not initialized when trying to send ProfileUpdateStarted event."); + } + } + + pub fn notify_profile_update_completed(uid: String) { + let handle = Self::global(); + if handle.is_exiting() { + return; + } + + let system_opt = handle.notification_system.read(); + if let Some(system) = system_opt.as_ref() { + system.send_event(FrontendEvent::ProfileUpdateCompleted { uid }); + } else { + log::warn!("Notification system not initialized when trying to send ProfileUpdateCompleted event."); + } + } + /// 通知前端显示消息队列 pub fn notice_message, M: Into>(status: S, msg: M) { let handle = Self::global(); diff --git a/src-tauri/src/core/timer.rs b/src-tauri/src/core/timer.rs index 5d0ab0ef..8f38275d 100644 --- a/src-tauri/src/core/timer.rs +++ b/src-tauri/src/core/timer.rs @@ -422,17 +422,10 @@ impl Timer { fn emit_update_event(_uid: &str, _is_start: bool) { #[cfg(any(feature = "verge-dev", feature = "default"))] { - use serde_json::json; - use tauri::Emitter; - - let event_name = if _is_start { - "profile-update-started" + if _is_start { + super::handle::Handle::notify_profile_update_started(_uid.to_string()); } else { - "profile-update-completed" - }; - - if let Some(window) = super::handle::Handle::global().get_window() { - let _ = window.emit(event_name, json!({ "uid": _uid })); + super::handle::Handle::notify_profile_update_completed(_uid.to_string()); } } } diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index b37df470..5a15a34b 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -16,7 +16,7 @@ use std::{ sync::Arc, time::{Duration, Instant}, }; -use tauri::{AppHandle, Emitter, Manager}; +use tauri::{AppHandle, Manager}; use tokio::net::TcpListener; use tauri::Url; @@ -334,22 +334,7 @@ pub fn create_window(is_show: bool) -> bool { true, "延时后,尝试发送 verge://startup-completed 事件" ); - if let Err(e) = newly_created_window.emit("verge://startup-completed", ()) { - logging!( - error, - Type::Window, - true, - "发送 verge://startup-completed 事件失败: {}", - e - ); - } else { - logging!( - info, - Type::Window, - true, - "已发送 verge://startup-completed 事件" - ); - } + handle::Handle::notify_startup_completed(); let timeout_seconds = if crate::module::lightweight::is_in_lightweight_mode() { 2 @@ -396,22 +381,7 @@ pub fn create_window(is_show: bool) -> bool { true, "is_show为false,窗口保持隐藏。尝试发送启动事件。" ); - if let Err(e) = newly_created_window.emit("verge://startup-completed", ()) { - logging!( - warn, - Type::Window, - true, - "发送 verge://startup-completed 事件失败 (is_show=false, 窗口隐藏): {}", - e - ); - } else { - logging!( - debug, - Type::Window, - true, - "已发送 verge://startup-completed 事件 (is_show=false, 窗口隐藏)" - ); - } + handle::Handle::notify_startup_completed(); } }); true