Refactor configuration access to use latest_arc() instead of latest_ref()

- Updated multiple instances in the codebase to replace calls to latest_ref() with latest_arc() for improved performance and memory management.
- This change affects various modules including validate, enhance, feat (backup, clash, config, profile, proxy, window), utils (draft, i18n, init, network, resolve, server).
- Ensured that all references to configuration data are now using the new arc-based approach to enhance concurrency and reduce cloning overhead.

refactor: update imports to explicitly include ClashInfo and Config in command files
This commit is contained in:
Tunglies
2025-11-03 05:41:53 +08:00
parent 48a19f99e2
commit 2287ea5f0b
37 changed files with 533 additions and 331 deletions

View File

@@ -87,7 +87,7 @@ impl WebDavClient {
(*cfg_arc).clone()
} else {
// 释放锁后获取异步配置
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
if verge.webdav_url.is_none()
|| verge.webdav_username.is_none()
|| verge.webdav_password.is_none()
@@ -99,11 +99,13 @@ impl WebDavClient {
let config = WebDavConfig {
url: verge
.webdav_url
.as_ref()
.cloned()
.unwrap_or_default()
.trim_end_matches('/')
.into(),
username: verge.webdav_username.unwrap_or_default(),
password: verge.webdav_password.unwrap_or_default(),
username: verge.webdav_username.as_ref().cloned().unwrap_or_default(),
password: verge.webdav_password.as_ref().cloned().unwrap_or_default(),
};
// 存储配置到 ArcSwapOption

View File

@@ -387,7 +387,7 @@ impl EventDrivenProxyManager {
async fn get_proxy_config() -> ProxyConfig {
let (sys_enabled, pac_enabled, guard_enabled, guard_duration) = {
let verge_config = Config::verge().await;
let verge = verge_config.latest_ref();
let verge = verge_config.latest_arc();
(
verge.enable_system_proxy.unwrap_or(false),
verge.proxy_auto_config.unwrap_or(false),
@@ -406,7 +406,7 @@ impl EventDrivenProxyManager {
async fn get_expected_pac_config() -> Autoproxy {
let proxy_host = {
let verge_config = Config::verge().await;
let verge = verge_config.latest_ref();
let verge = verge_config.latest_arc();
verge
.proxy_host
.clone()
@@ -424,13 +424,13 @@ impl EventDrivenProxyManager {
let (verge_mixed_port, proxy_host) = {
let verge_config = Config::verge().await;
let verge_ref = verge_config.latest_ref();
let verge_ref = verge_config.latest_arc();
(verge_ref.verge_mixed_port, verge_ref.proxy_host.clone())
};
let default_port = {
let clash_config = Config::clash().await;
clash_config.latest_ref().get_mixed_port()
clash_config.latest_arc().get_mixed_port()
};
let port = verge_mixed_port.unwrap_or(default_port);
@@ -450,7 +450,7 @@ impl EventDrivenProxyManager {
use crate::constants::bypass;
let verge_config = Config::verge().await;
let verge = verge_config.latest_ref();
let verge = verge_config.latest_arc();
let use_default = verge.use_default_bypass.unwrap_or(true);
let custom = verge.system_proxy_bypass.as_deref().unwrap_or("");

View File

@@ -237,7 +237,7 @@ impl Hotkey {
let is_enable_global_hotkey = Config::verge()
.await
.latest_ref()
.latest_arc()
.enable_global_hotkey
.unwrap_or(true);
@@ -274,7 +274,7 @@ singleton_with_logging!(Hotkey, INSTANCE, "Hotkey");
impl Hotkey {
pub async fn init(&self, skip: bool) -> Result<()> {
let verge = Config::verge().await;
let enable_global_hotkey = !skip && verge.latest_ref().enable_global_hotkey.unwrap_or(true);
let enable_global_hotkey = !skip && verge.latest_arc().enable_global_hotkey.unwrap_or(true);
logging!(
debug,
@@ -284,7 +284,7 @@ impl Hotkey {
);
// Extract hotkeys data before async operations
let hotkeys = verge.latest_ref().hotkeys.as_ref().cloned();
let hotkeys = verge.latest_arc().hotkeys.as_ref().cloned();
if let Some(hotkeys) = hotkeys {
logging!(

View File

@@ -17,13 +17,15 @@ impl CoreManager {
use crate::constants::files::RUNTIME_CONFIG;
let runtime_path = dirs::app_home_dir()?.join(RUNTIME_CONFIG);
let clash_config = Config::clash().await.latest_ref().0.clone();
let clash_config = &Config::clash().await.latest_arc().0;
**Config::runtime().await.draft_mut() = IRuntime {
config: Some(clash_config.clone()),
exists_keys: vec![],
chain_logs: Default::default(),
};
Config::runtime().await.edit_draft(|d| {
*d = IRuntime {
config: Some(clash_config.to_owned()),
exists_keys: vec![],
chain_logs: Default::default(),
}
});
help::save_yaml(&runtime_path, &clash_config, Some("# Clash Verge Runtime")).await?;
handle::Handle::notice_message(error_key, error_msg);

View File

@@ -47,10 +47,12 @@ impl CoreManager {
return Err(format!("Invalid clash core: {}", clash_core).into());
}
Config::verge().await.draft_mut().clash_core = clash_core.to_owned().into();
Config::verge().await.edit_draft(|d| {
d.clash_core = Some(clash_core.to_owned());
});
Config::verge().await.apply();
let verge_data = Config::verge().await.latest_ref().clone();
let verge_data = Config::verge().await.latest_arc();
verge_data.save_file().await.map_err(|e| e.to_string())?;
let run_path = Config::generate_file(ConfigType::Run)
@@ -82,7 +84,7 @@ impl CoreManager {
let needs_service = Config::verge()
.await
.latest_ref()
.latest_arc()
.enable_tun_mode
.unwrap_or(false);

View File

@@ -32,7 +32,7 @@ impl CoreManager {
let config_file = Config::generate_file(crate::config::ConfigType::Run).await?;
let app_handle = handle::Handle::app_handle();
let clash_core = Config::verge().await.latest_ref().get_valid_clash_core();
let clash_core = Config::verge().await.latest_arc().get_valid_clash_core();
let config_dir = dirs::app_home_dir()?;
let (mut rx, child) = app_handle

View File

@@ -353,7 +353,7 @@ pub(super) async fn start_with_existing_service(config_file: &PathBuf) -> Result
logging!(info, Type::Service, "尝试使用现有服务启动核心");
let verge_config = Config::verge().await;
let clash_core = verge_config.latest_ref().get_valid_clash_core();
let clash_core = verge_config.latest_arc().get_valid_clash_core();
drop(verge_config);
let bin_ext = if cfg!(windows) { ".exe" } else { "" };

View File

@@ -31,12 +31,12 @@ static DEFAULT_BYPASS: &str = "127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12
async fn get_bypass() -> String {
let use_default = Config::verge()
.await
.latest_ref()
.latest_arc()
.use_default_bypass
.unwrap_or(true);
let res = {
let verge = Config::verge().await;
let verge = verge.latest_ref();
let verge = verge.latest_arc();
verge.system_proxy_bypass.clone()
};
let custom_bypass = match res {
@@ -124,17 +124,17 @@ impl Sysopt {
}
let port = {
let verge_port = Config::verge().await.latest_ref().verge_mixed_port;
let verge_port = Config::verge().await.latest_arc().verge_mixed_port;
match verge_port {
Some(port) => port,
None => Config::clash().await.latest_ref().get_mixed_port(),
None => Config::clash().await.latest_arc().get_mixed_port(),
}
};
let pac_port = IVerge::get_singleton_port();
let (sys_enable, pac_enable, proxy_host) = {
let verge = Config::verge().await;
let verge = verge.latest_ref();
let verge = verge.latest_arc();
(
verge.enable_system_proxy.unwrap_or(false),
verge.proxy_auto_config.unwrap_or(false),
@@ -266,7 +266,7 @@ impl Sysopt {
/// update the startup
pub async fn update_launch(&self) -> Result<()> {
let enable_auto_launch = { Config::verge().await.latest_ref().enable_auto_launch };
let enable_auto_launch = { Config::verge().await.latest_arc().enable_auto_launch };
let is_enable = enable_auto_launch.unwrap_or(false);
logging!(
info,

View File

@@ -100,7 +100,7 @@ impl Timer {
// Collect profiles that need immediate update
let profiles_to_update =
if let Some(items) = Config::profiles().await.latest_ref().get_items() {
if let Some(items) = Config::profiles().await.latest_arc().get_items() {
items
.iter()
.filter_map(|item| {
@@ -273,7 +273,7 @@ impl Timer {
async fn gen_map(&self) -> HashMap<String, u64> {
let mut new_map = HashMap::new();
if let Some(items) = Config::profiles().await.latest_ref().get_items() {
if let Some(items) = Config::profiles().await.latest_arc().get_items() {
for item in items.iter() {
if let Some(option) = item.option.as_ref()
&& let Some(allow_auto_update) = option.allow_auto_update
@@ -427,7 +427,7 @@ impl Timer {
// Get the profile updated timestamp - now safe to await
let items = {
let profiles = Config::profiles().await;
let profiles_guard = profiles.latest_ref();
let profiles_guard = profiles.latest_arc();
match profiles_guard.get_items() {
Some(i) => i.clone(),
None => {
@@ -489,7 +489,7 @@ impl Timer {
match tokio::time::timeout(std::time::Duration::from_secs(40), async {
Self::emit_update_event(uid, true);
let is_current = Config::profiles().await.latest_ref().current.as_ref() == Some(uid);
let is_current = Config::profiles().await.latest_arc().current.as_ref() == Some(uid);
logging!(
info,
Type::Timer,

View File

@@ -86,7 +86,7 @@ pub struct Tray {
impl TrayState {
pub async fn get_common_tray_icon() -> (bool, Vec<u8>) {
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let is_common_tray_icon = verge.common_tray_icon.unwrap_or(false);
if is_common_tray_icon
&& let Ok(Some(common_icon_path)) = find_target_icons("common")
@@ -123,7 +123,7 @@ impl TrayState {
}
pub async fn get_sysproxy_tray_icon() -> (bool, Vec<u8>) {
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let is_sysproxy_tray_icon = verge.sysproxy_tray_icon.unwrap_or(false);
if is_sysproxy_tray_icon
&& let Ok(Some(sysproxy_icon_path)) = find_target_icons("sysproxy")
@@ -160,7 +160,7 @@ impl TrayState {
}
pub async fn get_tun_tray_icon() -> (bool, Vec<u8>) {
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let is_tun_tray_icon = verge.tun_tray_icon.unwrap_or(false);
if is_tun_tray_icon
&& let Ok(Some(tun_icon_path)) = find_target_icons("tun")
@@ -243,7 +243,7 @@ impl Tray {
}
let app_handle = handle::Handle::app_handle();
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
let tray_event = { Config::verge().await.latest_arc().tray_event.clone() };
let tray_event = tray_event.unwrap_or_else(|| "main_window".into());
let tray = app_handle
.tray_by_id("main")
@@ -303,7 +303,7 @@ impl Tray {
}
async fn update_menu_internal(&self, app_handle: &AppHandle) -> Result<()> {
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
let tun_mode_available = cmd::system::is_admin().unwrap_or_default()
@@ -311,7 +311,7 @@ impl Tray {
let mode = {
Config::clash()
.await
.latest_ref()
.latest_arc()
.0
.get("mode")
.map(|val| val.as_str().unwrap_or("rule"))
@@ -320,7 +320,7 @@ impl Tray {
};
let profile_uid_and_name = Config::profiles()
.await
.data_mut()
.latest_arc()
.all_profile_uid_and_name()
.unwrap_or_default();
let is_lightweight_mode = is_in_lightweight_mode();
@@ -375,7 +375,7 @@ impl Tray {
}
};
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false);
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
@@ -418,7 +418,7 @@ impl Tray {
}
};
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let system_mode = verge.enable_system_proxy.as_ref().unwrap_or(&false);
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
@@ -460,7 +460,7 @@ impl Tray {
let app_handle = handle::Handle::app_handle();
let verge = Config::verge().await.latest_ref().clone();
let verge = Config::verge().await.latest_arc();
let system_proxy = verge.enable_system_proxy.as_ref().unwrap_or(&false);
let tun_mode = verge.enable_tun_mode.as_ref().unwrap_or(&false);
@@ -474,7 +474,7 @@ impl Tray {
let mut current_profile_name = "None".into();
{
let profiles = Config::profiles().await;
let profiles = profiles.latest_ref();
let profiles = profiles.latest_arc();
if let Some(current_profile_uid) = profiles.get_current()
&& let Ok(profile) = profiles.get_item(&current_profile_uid)
{
@@ -552,7 +552,7 @@ impl Tray {
#[cfg(any(target_os = "macos", target_os = "windows"))]
let show_menu_on_left_click = {
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
let tray_event = { Config::verge().await.latest_arc().tray_event.clone() };
let tray_event: String = tray_event.unwrap_or_else(|| "main_window".into());
tray_event.as_str() == "tray_menu"
};
@@ -583,7 +583,7 @@ impl Tray {
}
AsyncHandler::spawn(|| async move {
let tray_event = { Config::verge().await.latest_ref().tray_event.clone() };
let tray_event = { Config::verge().await.latest_arc().tray_event.clone() };
let tray_event: String = tray_event.unwrap_or_else(|| "main_window".into());
logging!(debug, Type::Tray, "tray event: {tray_event:?}");
@@ -675,7 +675,7 @@ async fn create_profile_menu_item(
async move {
let is_current_profile = Config::profiles()
.await
.latest_ref()
.latest_arc()
.is_current_profile_index(profile_uid);
CheckMenuItem::with_id(
&app_handle,
@@ -878,7 +878,7 @@ async fn create_tray_menu(
// 获取当前配置文件的选中代理组信息
let current_profile_selected = {
let profiles_config = Config::profiles().await;
let profiles_ref = profiles_config.latest_ref();
let profiles_ref = profiles_config.latest_arc();
profiles_ref
.get_current()
.and_then(|uid| profiles_ref.get_item(&uid).ok())
@@ -924,7 +924,7 @@ async fn create_tray_menu(
.collect::<HashMap<String, usize>>()
});
let verge_settings = Config::verge().await.latest_ref().clone();
let verge_settings = Config::verge().await.latest_arc();
let show_proxy_groups_inline = verge_settings.tray_inline_proxy_groups.unwrap_or(false);
let version = env!("CARGO_PKG_VERSION");

View File

@@ -266,7 +266,7 @@ impl CoreConfigValidator {
logging!(info, Type::Validate, "开始验证配置文件: {}", config_path);
let clash_core = Config::verge().await.latest_ref().get_valid_clash_core();
let clash_core = Config::verge().await.latest_arc().get_valid_clash_core();
logging!(info, Type::Validate, "使用内核: {}", clash_core);
let app_handle = handle::Handle::app_handle();