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
Unverified
parent 48a19f99e2
commit 2287ea5f0b
37 changed files with 533 additions and 331 deletions

View File

@@ -78,7 +78,7 @@ pub async fn delete_webdav_backup(filename: String) -> Result<()> {
/// Restore WebDAV backup
pub async fn restore_webdav_backup(filename: String) -> Result<()> {
let verge = Config::verge().await;
let verge_data = verge.latest_ref().clone();
let verge_data = verge.latest_arc();
let webdav_url = verge_data.webdav_url.clone();
let webdav_username = verge_data.webdav_username.clone();
let webdav_password = verge_data.webdav_password.clone();
@@ -243,7 +243,7 @@ pub async fn restore_local_backup(filename: String) -> Result<()> {
let (webdav_url, webdav_username, webdav_password) = {
let verge = Config::verge().await;
let verge = verge.latest_ref();
let verge = verge.latest_arc();
(
verge.webdav_url.clone(),
verge.webdav_username.clone(),

View File

@@ -72,10 +72,12 @@ pub async fn change_clash_mode(mode: String) {
{
Ok(_) => {
// 更新订阅
Config::clash().await.data_mut().patch_config(mapping);
Config::clash()
.await
.edit_draft(|d| d.patch_config(mapping));
// 分离数据获取和异步调用
let clash_data = Config::clash().await.data_mut().clone();
let clash_data = Config::clash().await.data_arc();
if clash_data.save_config().await.is_ok() {
handle::Handle::refresh_clash();
logging_error!(Type::Tray, tray::Tray::global().update_menu().await);
@@ -84,7 +86,7 @@ pub async fn change_clash_mode(mode: String) {
let is_auto_close_connection = Config::verge()
.await
.data_mut()
.data_arc()
.auto_close_connection
.unwrap_or(false);
if is_auto_close_connection {
@@ -102,7 +104,7 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
let tun_mode = Config::verge()
.await
.latest_ref()
.latest_arc()
.enable_tun_mode
.unwrap_or(false);

View File

@@ -12,8 +12,7 @@ use serde_yaml_ng::Mapping;
pub async fn patch_clash(patch: Mapping) -> Result<()> {
Config::clash()
.await
.draft_mut()
.patch_config(patch.clone());
.edit_draft(|d| d.patch_config(patch.clone()));
let res = {
// 激活订阅
@@ -25,7 +24,9 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
logging_error!(Type::Tray, tray::Tray::global().update_menu().await);
logging_error!(Type::Tray, tray::Tray::global().update_icon().await);
}
Config::runtime().await.draft_mut().patch_config(patch);
Config::runtime()
.await
.edit_draft(|d| d.patch_config(patch));
CoreManager::global().update_config().await?;
}
handle::Handle::refresh_clash();
@@ -35,7 +36,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
Ok(()) => {
Config::clash().await.apply();
// 分离数据获取和异步调用
let clash_data = Config::clash().await.data_mut().clone();
let clash_data = Config::clash().await.data_arc();
clash_data.save_config().await?;
Ok(())
}
@@ -190,7 +191,9 @@ async fn process_terminated_flags(update_flags: i32, patch: &IVerge) -> Result<(
handle::Handle::refresh_clash();
}
if (update_flags & (UpdateFlags::VergeConfig as i32)) != 0 {
Config::verge().await.draft_mut().enable_global_hotkey = patch.enable_global_hotkey;
Config::verge()
.await
.edit_draft(|d| d.enable_global_hotkey = patch.enable_global_hotkey);
handle::Handle::refresh_verge();
}
if (update_flags & (UpdateFlags::Launch as i32)) != 0 {
@@ -227,7 +230,7 @@ async fn process_terminated_flags(update_flags: i32, patch: &IVerge) -> Result<(
}
pub async fn patch_verge(patch: &IVerge, not_save_file: bool) -> Result<()> {
Config::verge().await.draft_mut().patch_config(patch);
Config::verge().await.edit_draft(|d| d.patch_config(patch));
let update_flags = determine_update_flags(patch);
let process_flag_result: std::result::Result<(), anyhow::Error> = {
@@ -242,7 +245,7 @@ pub async fn patch_verge(patch: &IVerge, not_save_file: bool) -> Result<()> {
Config::verge().await.apply();
if !not_save_file {
// 分离数据获取和异步调用
let verge_data = Config::verge().await.data_ref().clone();
let verge_data = Config::verge().await.data_arc();
verge_data.save_file().await?;
}
Ok(())

View File

@@ -28,7 +28,7 @@ async fn should_update_profile(
ignore_auto_update: bool,
) -> Result<Option<(String, Option<PrfOption>)>> {
let profiles = Config::profiles().await;
let profiles = profiles.latest_ref();
let profiles = profiles.latest_arc();
let item = profiles.get_item(uid)?;
let is_remote = item.itype.as_ref().is_some_and(|s| s == "remote");
@@ -89,12 +89,12 @@ async fn perform_profile_update(
let mut merged_opt = PrfOption::merge(opt, option);
let is_current = {
let profiles = Config::profiles().await;
profiles.latest_ref().is_current_profile_index(uid)
profiles.latest_arc().is_current_profile_index(uid)
};
let profile_name = {
let profiles = Config::profiles().await;
profiles
.latest_ref()
.latest_arc()
.get_name_by_uid(uid)
.unwrap_or_default()
};

View File

@@ -10,8 +10,8 @@ use tauri_plugin_clipboard_manager::ClipboardExt;
/// Toggle system proxy on/off
pub async fn toggle_system_proxy() {
let verge = Config::verge().await;
let enable = verge.latest_ref().enable_system_proxy.unwrap_or(false);
let auto_close_connection = verge.latest_ref().auto_close_connection.unwrap_or(false);
let enable = verge.latest_arc().enable_system_proxy.unwrap_or(false);
let auto_close_connection = verge.latest_arc().auto_close_connection.unwrap_or(false);
// 如果当前系统代理即将关闭且自动关闭连接设置为true则关闭所有连接
if enable
@@ -42,7 +42,7 @@ pub async fn toggle_system_proxy() {
/// Toggle TUN mode on/off
pub async fn toggle_tun_mode(not_save_file: Option<bool>) {
let enable = Config::verge().await.data_mut().enable_tun_mode;
let enable = Config::verge().await.latest_arc().enable_tun_mode;
let enable = enable.unwrap_or(false);
match super::patch_verge(
@@ -66,7 +66,7 @@ pub async fn copy_clash_env() {
Ok(ip) => ip.into(),
Err(_) => Config::verge()
.await
.latest_ref()
.latest_arc()
.proxy_host
.clone()
.unwrap_or_else(|| "127.0.0.1".into()),
@@ -76,7 +76,7 @@ pub async fn copy_clash_env() {
let port = {
Config::verge()
.await
.latest_ref()
.latest_arc()
.verge_mixed_port
.unwrap_or(7897)
};
@@ -84,7 +84,7 @@ pub async fn copy_clash_env() {
let socks5_proxy = format!("socks5://{clash_verge_rev_ip}:{port}");
let cliboard = app_handle.clipboard();
let env_type = { Config::verge().await.latest_ref().env_type.clone() };
let env_type = { Config::verge().await.latest_arc().env_type.clone() };
let env_type = match env_type {
Some(env_type) => env_type,
None => {

View File

@@ -47,7 +47,7 @@ pub async fn clean_async() -> bool {
let tun_task = async {
let tun_enabled = Config::verge()
.await
.latest_ref()
.latest_arc()
.enable_tun_mode
.unwrap_or(false);
@@ -100,7 +100,7 @@ pub async fn clean_async() -> bool {
// 检查系统代理是否开启
let sys_proxy_enabled = Config::verge()
.await
.latest_ref()
.latest_arc()
.enable_system_proxy
.unwrap_or(false);
@@ -176,7 +176,7 @@ pub async fn clean_async() -> bool {
{
let sys_proxy_enabled = Config::verge()
.await
.latest_ref()
.latest_arc()
.enable_system_proxy
.unwrap_or(false);
@@ -316,7 +316,7 @@ pub async fn hide() {
let enable_auto_light_weight_mode = Config::verge()
.await
.data_mut()
.latest_arc()
.enable_auto_light_weight_mode
.unwrap_or(false);