diff --git a/src-tauri/src/core/hotkey.rs b/src-tauri/src/core/hotkey.rs index 7423affa1..fb72551f7 100755 --- a/src-tauri/src/core/hotkey.rs +++ b/src-tauri/src/core/hotkey.rs @@ -1,9 +1,7 @@ use crate::process::AsyncHandler; +use crate::singleton; use crate::utils::notification::{NotificationEvent, notify_event}; -use crate::{ - config::Config, core::handle, feat, module::lightweight::entry_lightweight_mode, - singleton_with_logging, -}; +use crate::{config::Config, core::handle, feat, module::lightweight::entry_lightweight_mode}; use anyhow::{Result, bail}; use arc_swap::ArcSwap; use clash_verge_logging::{Type, logging}; @@ -296,8 +294,7 @@ impl Hotkey { } } -// Use unified singleton macro -singleton_with_logging!(Hotkey, INSTANCE, "Hotkey"); +singleton!(Hotkey, INSTANCE); impl Hotkey { pub async fn init(&self, skip: bool) -> Result<()> { diff --git a/src-tauri/src/core/manager/mod.rs b/src-tauri/src/core/manager/mod.rs index 8ec5a3c9d..ed0f1e53d 100644 --- a/src-tauri/src/core/manager/mod.rs +++ b/src-tauri/src/core/manager/mod.rs @@ -7,7 +7,7 @@ use arc_swap::{ArcSwap, ArcSwapOption}; use std::{fmt, sync::Arc, time::Instant}; use tauri_plugin_shell::process::CommandChild; -use crate::singleton_lazy; +use crate::singleton; #[derive(Debug, serde::Serialize, PartialEq, Eq)] pub enum RunningMode { @@ -57,6 +57,10 @@ impl Default for CoreManager { } impl CoreManager { + fn new() -> Self { + Self::default() + } + pub fn get_running_mode(&self) -> Arc { Arc::clone(&self.state.load().running_mode.load()) } @@ -93,4 +97,4 @@ impl CoreManager { } } -singleton_lazy!(CoreManager, CORE_MANAGER, CoreManager::default); +singleton!(CoreManager, CORE_MANAGER); diff --git a/src-tauri/src/core/sysopt.rs b/src-tauri/src/core/sysopt.rs index 46dd88bbb..8ee3259ad 100644 --- a/src-tauri/src/core/sysopt.rs +++ b/src-tauri/src/core/sysopt.rs @@ -3,7 +3,7 @@ use crate::utils::autostart as startup_shortcut; use crate::{ config::{Config, IVerge}, core::handle::Handle, - singleton_lazy, + singleton, }; use anyhow::Result; use clash_verge_logging::{Type, logging, logging_error}; @@ -103,10 +103,13 @@ async fn execute_sysproxy_command(args: Vec) -> Result<()> Ok(()) } -// Use simplified singleton_lazy macro -singleton_lazy!(Sysopt, SYSOPT, Sysopt::default); +singleton!(Sysopt, SYSOPT); impl Sysopt { + fn new() -> Self { + Self::default() + } + pub fn is_initialed(&self) -> bool { self.initialed.load(Ordering::SeqCst) } diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 35fcba177..29a4837fd 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -8,13 +8,13 @@ use crate::config::{IProfilePreview, IVerge, PrfSelected}; use crate::core::service; use crate::module::lightweight; use crate::process::AsyncHandler; +use crate::singleton; use crate::utils::window_manager::WindowManager; use crate::{ Type, cmd, config::Config, feat, logging, module::lightweight::is_in_lightweight_mode, - singleton_lazy, utils::{dirs::find_target_icons, i18n}, }; @@ -199,10 +199,13 @@ impl Default for Tray { } } -// Use simplified singleton_lazy macro -singleton_lazy!(Tray, TRAY, Tray::default); +singleton!(Tray, TRAY); impl Tray { + fn new() -> Self { + Self::default() + } + pub async fn init(&self) -> Result<()> { if handle::Handle::global().is_exiting() { logging!(debug, Type::Tray, "应用正在退出,跳过托盘初始化"); diff --git a/src-tauri/src/core/validate.rs b/src-tauri/src/core/validate.rs index 96dd03959..153daa118 100644 --- a/src-tauri/src/core/validate.rs +++ b/src-tauri/src/core/validate.rs @@ -7,7 +7,7 @@ use tokio::fs; use crate::config::{Config, ConfigType}; use crate::core::handle; -use crate::singleton_lazy; +use crate::singleton; use crate::utils::dirs; use clash_verge_logging::{Type, logging}; @@ -361,8 +361,4 @@ fn contains_any_keyword<'a>(buf: &'a [u8], keywords: &'a [&str]) -> bool { false } -singleton_lazy!( - CoreConfigValidator, - CORECONFIGVALIDATOR, - CoreConfigValidator::new -); +singleton!(CoreConfigValidator, CORECONFIGVALIDATOR); diff --git a/src-tauri/src/utils/singleton.rs b/src-tauri/src/utils/singleton.rs index 5e31f3a85..44127e2cc 100644 --- a/src-tauri/src/utils/singleton.rs +++ b/src-tauri/src/utils/singleton.rs @@ -37,65 +37,6 @@ macro_rules! singleton { }; } -/// Macro for singleton pattern with logging -#[macro_export] -macro_rules! singleton_with_logging { - ($struct_name:ty, $instance_name:ident, $struct_name_str:literal) => { - static $instance_name: std::sync::OnceLock<$struct_name> = std::sync::OnceLock::new(); - - impl $struct_name { - pub fn global() -> &'static $struct_name { - $instance_name.get_or_init(|| { - let instance = Self::new(); - clash_verge_logging::logging!( - info, - clash_verge_logging::Type::Setup, - concat!($struct_name_str, " initialized") - ); - instance - }) - } - } - }; -} - -/// Macro for singleton pattern with lazy initialization using a closure -/// This replaces patterns like lazy_static! or complex OnceLock initialization -#[macro_export] -macro_rules! singleton_lazy { - ($struct_name:ty, $instance_name:ident, $init_closure:expr) => { - static $instance_name: std::sync::OnceLock<$struct_name> = std::sync::OnceLock::new(); - - impl $struct_name { - pub fn global() -> &'static $struct_name { - $instance_name.get_or_init($init_closure) - } - } - }; -} - -/// Macro for singleton pattern with lazy initialization and logging -#[macro_export] -macro_rules! singleton_lazy_with_logging { - ($struct_name:ty, $instance_name:ident, $struct_name_str:literal, $init_closure:expr) => { - static $instance_name: std::sync::OnceLock<$struct_name> = std::sync::OnceLock::new(); - - impl $struct_name { - pub fn global() -> &'static $struct_name { - $instance_name.get_or_init(|| { - let instance = $init_closure(); - $crate::logging!( - info, - $crate::utils::logging::Type::Setup, - concat!($struct_name_str, " initialized") - ); - instance - }) - } - } - }; -} - #[cfg(test)] mod tests { struct TestStruct {