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:
@@ -72,6 +72,7 @@
|
|||||||
- 对获取系统信息的流程进行了优化,并添加了去重检测机制,确保剔除重复的信息
|
- 对获取系统信息的流程进行了优化,并添加了去重检测机制,确保剔除重复的信息
|
||||||
- 优化窗口状态初始化逻辑和添加缺失的权限设置
|
- 优化窗口状态初始化逻辑和添加缺失的权限设置
|
||||||
- 异步化配置:优化端口查找和配置保存逻辑
|
- 异步化配置:优化端口查找和配置保存逻辑
|
||||||
|
- 重构事件通知机制到独立线程,避免前端卡死
|
||||||
|
|
||||||
## v2.2.3
|
## v2.2.3
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ use crate::{
|
|||||||
utils::{dirs, help, logging::Type},
|
utils::{dirs, help, logging::Type},
|
||||||
wrap_err,
|
wrap_err,
|
||||||
};
|
};
|
||||||
use tauri::Emitter;
|
|
||||||
|
|
||||||
/// 获取配置文件列表
|
/// 获取配置文件列表
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
@@ -160,11 +159,9 @@ pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult<bool> {
|
|||||||
Config::profiles().apply();
|
Config::profiles().apply();
|
||||||
wrap_err!(Config::profiles().data().save_file())?;
|
wrap_err!(Config::profiles().data().save_file())?;
|
||||||
|
|
||||||
if let Some(window) = handle::Handle::global().get_window() {
|
if let Some(current) = ¤t_value {
|
||||||
if let Some(current) = ¤t_value {
|
logging!(info, Type::Cmd, true, "向前端发送配置变更事件: {}", current);
|
||||||
logging!(info, Type::Cmd, true, "向前端发送配置变更事件: {}", current);
|
handle::Handle::notify_profile_changed(current.clone());
|
||||||
let _ = window.emit("profile-changed", current.clone());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(true)
|
Ok(true)
|
||||||
@@ -245,9 +242,7 @@ pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
|
|||||||
logging!(error, Type::Timer, "刷新定时器失败: {}", e);
|
logging!(error, Type::Timer, "刷新定时器失败: {}", e);
|
||||||
} else {
|
} else {
|
||||||
// 刷新成功后发送自定义事件,不触发配置重载
|
// 刷新成功后发送自定义事件,不触发配置重载
|
||||||
if let Some(window) = crate::core::handle::Handle::global().get_window() {
|
crate::core::handle::Handle::notify_timer_updated(index_clone);
|
||||||
let _ = window.emit("verge://timer-updated", index_clone);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,11 @@ enum FrontendEvent {
|
|||||||
RefreshClash,
|
RefreshClash,
|
||||||
RefreshVerge,
|
RefreshVerge,
|
||||||
NoticeMessage { status: String, message: String },
|
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 {
|
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) {
|
pub fn notice_message<S: Into<String>, M: Into<String>>(status: S, msg: M) {
|
||||||
let handle = Self::global();
|
let handle = Self::global();
|
||||||
|
|||||||
@@ -422,17 +422,10 @@ impl Timer {
|
|||||||
fn emit_update_event(_uid: &str, _is_start: bool) {
|
fn emit_update_event(_uid: &str, _is_start: bool) {
|
||||||
#[cfg(any(feature = "verge-dev", feature = "default"))]
|
#[cfg(any(feature = "verge-dev", feature = "default"))]
|
||||||
{
|
{
|
||||||
use serde_json::json;
|
if _is_start {
|
||||||
use tauri::Emitter;
|
super::handle::Handle::notify_profile_update_started(_uid.to_string());
|
||||||
|
|
||||||
let event_name = if _is_start {
|
|
||||||
"profile-update-started"
|
|
||||||
} else {
|
} else {
|
||||||
"profile-update-completed"
|
super::handle::Handle::notify_profile_update_completed(_uid.to_string());
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(window) = super::handle::Handle::global().get_window() {
|
|
||||||
let _ = window.emit(event_name, json!({ "uid": _uid }));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use std::{
|
|||||||
sync::Arc,
|
sync::Arc,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
use tauri::{AppHandle, Emitter, Manager};
|
use tauri::{AppHandle, Manager};
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
|
|
||||||
use tauri::Url;
|
use tauri::Url;
|
||||||
@@ -334,22 +334,7 @@ pub fn create_window(is_show: bool) -> bool {
|
|||||||
true,
|
true,
|
||||||
"延时后,尝试发送 verge://startup-completed 事件"
|
"延时后,尝试发送 verge://startup-completed 事件"
|
||||||
);
|
);
|
||||||
if let Err(e) = newly_created_window.emit("verge://startup-completed", ()) {
|
handle::Handle::notify_startup_completed();
|
||||||
logging!(
|
|
||||||
error,
|
|
||||||
Type::Window,
|
|
||||||
true,
|
|
||||||
"发送 verge://startup-completed 事件失败: {}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
logging!(
|
|
||||||
info,
|
|
||||||
Type::Window,
|
|
||||||
true,
|
|
||||||
"已发送 verge://startup-completed 事件"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let timeout_seconds = if crate::module::lightweight::is_in_lightweight_mode() {
|
let timeout_seconds = if crate::module::lightweight::is_in_lightweight_mode() {
|
||||||
2
|
2
|
||||||
@@ -396,22 +381,7 @@ pub fn create_window(is_show: bool) -> bool {
|
|||||||
true,
|
true,
|
||||||
"is_show为false,窗口保持隐藏。尝试发送启动事件。"
|
"is_show为false,窗口保持隐藏。尝试发送启动事件。"
|
||||||
);
|
);
|
||||||
if let Err(e) = newly_created_window.emit("verge://startup-completed", ()) {
|
handle::Handle::notify_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, 窗口隐藏)"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
true
|
true
|
||||||
|
|||||||
Reference in New Issue
Block a user