mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 07:14:40 +08:00
refactor(notification): simplify notification system by removing unused fields and methods
This commit is contained in:
@@ -1,10 +1,6 @@
|
||||
use crate::{APP_HANDLE, singleton};
|
||||
use parking_lot::Mutex;
|
||||
use smartstring::alias::String;
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tauri::{AppHandle, Manager as _, WebviewWindow};
|
||||
use tauri_plugin_mihomo::{Mihomo, MihomoExt as _};
|
||||
use tokio::sync::RwLockReadGuard;
|
||||
@@ -13,7 +9,6 @@ use super::notification::{FrontendEvent, NotificationSystem};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Handle {
|
||||
pub(super) notification_system: Arc<Mutex<NotificationSystem>>,
|
||||
is_exiting: AtomicBool,
|
||||
}
|
||||
|
||||
@@ -24,13 +19,6 @@ impl Handle {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn init(&self) {
|
||||
if self.is_exiting() {
|
||||
return;
|
||||
}
|
||||
self.notification_system.lock().start();
|
||||
}
|
||||
|
||||
pub fn app_handle() -> &'static AppHandle {
|
||||
#[allow(clippy::expect_used)]
|
||||
APP_HANDLE.get().expect("App handle not initialized")
|
||||
@@ -91,12 +79,12 @@ impl Handle {
|
||||
if handle.is_exiting() {
|
||||
return;
|
||||
}
|
||||
handle.notification_system.lock().send_event(event);
|
||||
let webview = Self::get_window();
|
||||
NotificationSystem::send_event(webview.as_ref(), event);
|
||||
}
|
||||
|
||||
pub fn set_is_exiting(&self) {
|
||||
self.is_exiting.store(true, Ordering::Release);
|
||||
self.notification_system.lock().shutdown();
|
||||
}
|
||||
|
||||
pub fn is_exiting(&self) -> bool {
|
||||
|
||||
@@ -1,16 +1,7 @@
|
||||
use std::sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
};
|
||||
|
||||
use super::handle::Handle;
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use serde_json::json;
|
||||
use smartstring::alias::String;
|
||||
use tauri::{
|
||||
Emitter as _, WebviewWindow,
|
||||
async_runtime::{JoinHandle, Receiver, Sender, channel},
|
||||
};
|
||||
use tauri::{Emitter as _, WebviewWindow};
|
||||
|
||||
// TODO 重构或优化,避免 Clone 过多
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -24,80 +15,17 @@ pub(super) enum FrontendEvent {
|
||||
ProfileUpdateCompleted { uid: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct NotificationSystem {
|
||||
sender: Arc<Option<Sender<FrontendEvent>>>,
|
||||
worker_task: Arc<Option<JoinHandle<()>>>,
|
||||
pub(super) is_running: AtomicBool,
|
||||
}
|
||||
pub(super) struct NotificationSystem;
|
||||
|
||||
impl NotificationSystem {
|
||||
pub fn start(&mut self) {
|
||||
if self
|
||||
.is_running
|
||||
.compare_exchange(false, true, Ordering::Release, Ordering::Relaxed)
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
let (tx, rx) = channel(32);
|
||||
if let Some(s) = Arc::get_mut(&mut self.sender) {
|
||||
*s = Some(tx);
|
||||
}
|
||||
let task = tauri::async_runtime::spawn(async move {
|
||||
Self::worker_loop(rx).await;
|
||||
});
|
||||
if let Some(t) = Arc::get_mut(&mut self.worker_task) {
|
||||
*t = Some(task);
|
||||
pub(super) fn send_event(window: Option<&WebviewWindow>, event: FrontendEvent) {
|
||||
if let Some(window) = window {
|
||||
Self::emit_to_window(window, event);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn shutdown(&mut self) {
|
||||
if self
|
||||
.is_running
|
||||
.compare_exchange(true, false, Ordering::Release, Ordering::Relaxed)
|
||||
.is_err()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
self.sender = Arc::new(None);
|
||||
|
||||
let value = Arc::get_mut(&mut self.worker_task).and_then(|t| t.take());
|
||||
if let Some(task) = value {
|
||||
task.abort();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send_event(&self, event: FrontendEvent) -> bool {
|
||||
if let Some(sender) = &*self.sender {
|
||||
sender.try_send(event).is_ok()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl NotificationSystem {
|
||||
async fn worker_loop(mut rx: Receiver<FrontendEvent>) {
|
||||
let handle = Handle::global();
|
||||
while let Some(event) = rx.recv().await {
|
||||
if handle.is_exiting() {
|
||||
break;
|
||||
}
|
||||
Self::process_event_sync(handle, event);
|
||||
}
|
||||
}
|
||||
|
||||
fn process_event_sync(handle: &super::handle::Handle, event: FrontendEvent) {
|
||||
if let Some(window) = super::handle::Handle::get_window() {
|
||||
handle.notification_system.lock().emit_to_window(&window, event);
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_to_window(&self, window: &WebviewWindow, event: FrontendEvent) {
|
||||
let (event_name, payload) = self.serialize_event(event);
|
||||
fn emit_to_window(window: &WebviewWindow, event: FrontendEvent) {
|
||||
let (event_name, payload) = Self::serialize_event(event);
|
||||
let Ok(payload) = payload else {
|
||||
return;
|
||||
};
|
||||
@@ -106,7 +34,7 @@ impl NotificationSystem {
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize_event(&self, event: FrontendEvent) -> (&'static str, Result<serde_json::Value, serde_json::Error>) {
|
||||
fn serialize_event(event: FrontendEvent) -> (&'static str, Result<serde_json::Value, serde_json::Error>) {
|
||||
match event {
|
||||
FrontendEvent::RefreshClash => ("verge://refresh-clash-config", Ok(json!("yes"))),
|
||||
FrontendEvent::RefreshVerge => ("verge://refresh-verge-config", Ok(json!("yes"))),
|
||||
|
||||
@@ -251,7 +251,6 @@ pub fn run() {
|
||||
logging!(error, Type::Setup, "Failed to setup window state: {}", e);
|
||||
}
|
||||
|
||||
resolve::resolve_setup_handle();
|
||||
resolve::resolve_setup_async();
|
||||
resolve::resolve_setup_sync();
|
||||
resolve::init_signal();
|
||||
@@ -284,7 +283,6 @@ pub fn run() {
|
||||
}
|
||||
|
||||
logging!(info, Type::System, "应用就绪");
|
||||
handle::Handle::global().init();
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(window) = _app_handle.get_webview_window("main") {
|
||||
@@ -294,8 +292,6 @@ pub fn run() {
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
pub async fn handle_reopen(has_visible_windows: bool) {
|
||||
handle::Handle::global().init();
|
||||
|
||||
if lightweight::is_in_lightweight_mode() {
|
||||
lightweight::exit_lightweight_mode().await;
|
||||
return;
|
||||
|
||||
@@ -6,7 +6,7 @@ use flexi_logger::LoggerHandle;
|
||||
use crate::{
|
||||
config::Config,
|
||||
core::{
|
||||
CoreManager, Timer, handle,
|
||||
CoreManager, Timer,
|
||||
hotkey::Hotkey,
|
||||
service::{SERVICE_MANAGER, ServiceManager, is_service_ipc_path_exists},
|
||||
sysopt,
|
||||
@@ -45,10 +45,6 @@ pub fn init_work_dir_and_logger() -> Option<LoggerHandle> {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resolve_setup_handle() {
|
||||
init_handle();
|
||||
}
|
||||
|
||||
pub fn resolve_setup_sync() {
|
||||
AsyncHandler::spawn(|| async {
|
||||
AsyncHandler::spawn_blocking(init_scheme);
|
||||
@@ -101,10 +97,6 @@ pub async fn resolve_reset_async() -> Result<(), anyhow::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn init_handle() {
|
||||
handle::Handle::global().init();
|
||||
}
|
||||
|
||||
pub(super) fn init_scheme() {
|
||||
logging_error!(Type::Setup, init::init_scheme());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user