mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 07:14:40 +08:00
refactor: replace singleton_lazy with singleton macro across multiple modules
This commit is contained in:
@@ -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<()> {
|
||||
|
||||
@@ -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<RunningMode> {
|
||||
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);
|
||||
|
||||
@@ -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<std::string::String>) -> 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)
|
||||
}
|
||||
|
||||
@@ -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, "应用正在退出,跳过托盘初始化");
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user