mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
refactor: simplify frontend event dispatch by restructuring notification system
This commit is contained in:
@@ -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<S: Into<String>, M: Into<String>>(status: S, msg: M) {
|
||||
let handle = Self::global();
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user