refactor: replace singleton_lazy with singleton macro across multiple modules

This commit is contained in:
Tunglies 2025-11-19 22:40:52 +08:00
parent 108a05ce96
commit d8090277af
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8
6 changed files with 23 additions and 79 deletions

View File

@ -1,9 +1,7 @@
use crate::process::AsyncHandler; use crate::process::AsyncHandler;
use crate::singleton;
use crate::utils::notification::{NotificationEvent, notify_event}; use crate::utils::notification::{NotificationEvent, notify_event};
use crate::{ use crate::{config::Config, core::handle, feat, module::lightweight::entry_lightweight_mode};
config::Config, core::handle, feat, module::lightweight::entry_lightweight_mode,
singleton_with_logging,
};
use anyhow::{Result, bail}; use anyhow::{Result, bail};
use arc_swap::ArcSwap; use arc_swap::ArcSwap;
use clash_verge_logging::{Type, logging}; use clash_verge_logging::{Type, logging};
@ -296,8 +294,7 @@ impl Hotkey {
} }
} }
// Use unified singleton macro singleton!(Hotkey, INSTANCE);
singleton_with_logging!(Hotkey, INSTANCE, "Hotkey");
impl Hotkey { impl Hotkey {
pub async fn init(&self, skip: bool) -> Result<()> { pub async fn init(&self, skip: bool) -> Result<()> {

View File

@ -7,7 +7,7 @@ use arc_swap::{ArcSwap, ArcSwapOption};
use std::{fmt, sync::Arc, time::Instant}; use std::{fmt, sync::Arc, time::Instant};
use tauri_plugin_shell::process::CommandChild; use tauri_plugin_shell::process::CommandChild;
use crate::singleton_lazy; use crate::singleton;
#[derive(Debug, serde::Serialize, PartialEq, Eq)] #[derive(Debug, serde::Serialize, PartialEq, Eq)]
pub enum RunningMode { pub enum RunningMode {
@ -57,6 +57,10 @@ impl Default for CoreManager {
} }
impl CoreManager { impl CoreManager {
fn new() -> Self {
Self::default()
}
pub fn get_running_mode(&self) -> Arc<RunningMode> { pub fn get_running_mode(&self) -> Arc<RunningMode> {
Arc::clone(&self.state.load().running_mode.load()) 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);

View File

@ -3,7 +3,7 @@ use crate::utils::autostart as startup_shortcut;
use crate::{ use crate::{
config::{Config, IVerge}, config::{Config, IVerge},
core::handle::Handle, core::handle::Handle,
singleton_lazy, singleton,
}; };
use anyhow::Result; use anyhow::Result;
use clash_verge_logging::{Type, logging, logging_error}; use clash_verge_logging::{Type, logging, logging_error};
@ -103,10 +103,13 @@ async fn execute_sysproxy_command(args: Vec<std::string::String>) -> Result<()>
Ok(()) Ok(())
} }
// Use simplified singleton_lazy macro singleton!(Sysopt, SYSOPT);
singleton_lazy!(Sysopt, SYSOPT, Sysopt::default);
impl Sysopt { impl Sysopt {
fn new() -> Self {
Self::default()
}
pub fn is_initialed(&self) -> bool { pub fn is_initialed(&self) -> bool {
self.initialed.load(Ordering::SeqCst) self.initialed.load(Ordering::SeqCst)
} }

View File

@ -8,13 +8,13 @@ use crate::config::{IProfilePreview, IVerge, PrfSelected};
use crate::core::service; use crate::core::service;
use crate::module::lightweight; use crate::module::lightweight;
use crate::process::AsyncHandler; use crate::process::AsyncHandler;
use crate::singleton;
use crate::utils::window_manager::WindowManager; use crate::utils::window_manager::WindowManager;
use crate::{ use crate::{
Type, cmd, Type, cmd,
config::Config, config::Config,
feat, logging, feat, logging,
module::lightweight::is_in_lightweight_mode, module::lightweight::is_in_lightweight_mode,
singleton_lazy,
utils::{dirs::find_target_icons, i18n}, utils::{dirs::find_target_icons, i18n},
}; };
@ -199,10 +199,13 @@ impl Default for Tray {
} }
} }
// Use simplified singleton_lazy macro singleton!(Tray, TRAY);
singleton_lazy!(Tray, TRAY, Tray::default);
impl Tray { impl Tray {
fn new() -> Self {
Self::default()
}
pub async fn init(&self) -> Result<()> { pub async fn init(&self) -> Result<()> {
if handle::Handle::global().is_exiting() { if handle::Handle::global().is_exiting() {
logging!(debug, Type::Tray, "应用正在退出,跳过托盘初始化"); logging!(debug, Type::Tray, "应用正在退出,跳过托盘初始化");

View File

@ -7,7 +7,7 @@ use tokio::fs;
use crate::config::{Config, ConfigType}; use crate::config::{Config, ConfigType};
use crate::core::handle; use crate::core::handle;
use crate::singleton_lazy; use crate::singleton;
use crate::utils::dirs; use crate::utils::dirs;
use clash_verge_logging::{Type, logging}; use clash_verge_logging::{Type, logging};
@ -361,8 +361,4 @@ fn contains_any_keyword<'a>(buf: &'a [u8], keywords: &'a [&str]) -> bool {
false false
} }
singleton_lazy!( singleton!(CoreConfigValidator, CORECONFIGVALIDATOR);
CoreConfigValidator,
CORECONFIGVALIDATOR,
CoreConfigValidator::new
);

View File

@ -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)] #[cfg(test)]
mod tests { mod tests {
struct TestStruct { struct TestStruct {