refactor(Draft): Replace latest() with latest_ref() and data() with data_mut() in multiple files for improved mutability handling and consistency across the codebase (#3987)

* feat: add benchmarking for draft operations and new draft management structure

* Refactor Config Access: Replace `latest()` with `latest_ref()` and `data()` with `data_mut()` in multiple files for improved mutability handling and consistency across the codebase.

* refactor: remove DraftNew implementation and related benchmarks for cleaner codebase
This commit is contained in:
Tunglies
2025-07-04 22:43:23 +08:00
committed by GitHub
parent 3f95c81243
commit 764ef48fd1
36 changed files with 573 additions and 267 deletions

View File

@@ -52,7 +52,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();
let verge_data = verge.data().clone();
let verge_data = verge.latest_ref().clone();
let webdav_url = verge_data.webdav_url.clone();
let webdav_username = verge_data.webdav_username.clone();
let webdav_password = verge_data.webdav_password.clone();

View File

@@ -68,16 +68,16 @@ pub fn change_clash_mode(mode: String) {
match MihomoManager::global().patch_configs(json_value).await {
Ok(_) => {
// 更新订阅
Config::clash().data().patch_config(mapping);
Config::clash().data_mut().patch_config(mapping);
if Config::clash().data().save_config().is_ok() {
if Config::clash().data_mut().save_config().is_ok() {
handle::Handle::refresh_clash();
logging_error!(Type::Tray, true, tray::Tray::global().update_menu());
logging_error!(Type::Tray, true, tray::Tray::global().update_icon(None));
}
let is_auto_close_connection = Config::verge()
.data()
.data_mut()
.auto_close_connection
.unwrap_or(false);
if is_auto_close_connection {
@@ -94,7 +94,10 @@ pub async fn test_delay(url: String) -> anyhow::Result<u32> {
use crate::utils::network::{NetworkManager, ProxyType};
use tokio::time::Instant;
let tun_mode = Config::verge().latest().enable_tun_mode.unwrap_or(false);
let tun_mode = Config::verge()
.latest_ref()
.enable_tun_mode
.unwrap_or(false);
// 如果是TUN模式不使用代理否则使用自身代理
let proxy_type = if !tun_mode {

View File

@@ -10,7 +10,7 @@ use serde_yaml::Mapping;
/// Patch Clash configuration
pub async fn patch_clash(patch: Mapping) -> Result<()> {
Config::clash().draft().patch_config(patch.clone());
Config::clash().draft_mut().patch_config(patch.clone());
let res = {
// 激活订阅
@@ -22,7 +22,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
logging_error!(Type::Tray, true, tray::Tray::global().update_menu());
logging_error!(Type::Tray, true, tray::Tray::global().update_icon(None));
}
Config::runtime().latest().patch_config(patch);
Config::runtime().draft_mut().patch_config(patch);
CoreManager::global().update_config().await?;
}
handle::Handle::refresh_clash();
@@ -31,7 +31,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
match res {
Ok(()) => {
Config::clash().apply();
Config::clash().data().save_config()?;
Config::clash().data_mut().save_config()?;
Ok(())
}
Err(err) => {
@@ -60,7 +60,7 @@ enum UpdateFlags {
/// Patch Verge configuration
pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
Config::verge().draft().patch_config(patch.clone());
Config::verge().draft_mut().patch_config(patch.clone());
let tun_mode = patch.enable_tun_mode;
let auto_launch = patch.enable_auto_launch;
@@ -175,7 +175,7 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
handle::Handle::refresh_clash();
}
if (update_flags & (UpdateFlags::VergeConfig as i32)) != 0 {
Config::verge().draft().enable_global_hotkey = enable_global_hotkey;
Config::verge().draft_mut().enable_global_hotkey = enable_global_hotkey;
handle::Handle::refresh_verge();
}
if (update_flags & (UpdateFlags::Launch as i32)) != 0 {
@@ -213,7 +213,7 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
Ok(()) => {
Config::verge().apply();
if !not_save_file {
Config::verge().data().save_file()?;
Config::verge().data_mut().save_file()?;
}
Ok(())

View File

@@ -36,7 +36,7 @@ pub async fn update_profile(
let url_opt = {
let profiles = Config::profiles();
let profiles = profiles.latest();
let profiles = profiles.latest_ref();
let item = profiles.get_item(&uid)?;
let is_remote = item.itype.as_ref().is_some_and(|s| s == "remote");
@@ -66,7 +66,7 @@ pub async fn update_profile(
Ok(item) => {
log::info!(target: "app", "[订阅更新] 更新订阅配置成功");
let profiles = Config::profiles();
let mut profiles = profiles.latest();
let mut profiles = profiles.draft_mut();
profiles.update_item(uid.clone(), item)?;
let is_current = Some(uid.clone()) == profiles.get_current();
@@ -102,7 +102,7 @@ pub async fn update_profile(
// 更新到配置
let profiles = Config::profiles();
let mut profiles = profiles.latest();
let mut profiles = profiles.draft_mut();
profiles.update_item(uid.clone(), item.clone())?;
// 获取配置名称用于通知

View File

@@ -8,10 +8,10 @@ use tauri_plugin_clipboard_manager::ClipboardExt;
/// Toggle system proxy on/off
pub fn toggle_system_proxy() {
let enable = Config::verge().draft().enable_system_proxy;
let enable = Config::verge().draft_mut().enable_system_proxy;
let enable = enable.unwrap_or(false);
let auto_close_connection = Config::verge()
.data()
.data_mut()
.auto_close_connection
.unwrap_or(false);
@@ -43,7 +43,7 @@ pub fn toggle_system_proxy() {
/// Toggle TUN mode on/off
pub fn toggle_tun_mode(not_save_file: Option<bool>) {
let enable = Config::verge().data().enable_tun_mode;
let enable = Config::verge().data_mut().enable_tun_mode;
let enable = enable.unwrap_or(false);
AsyncHandler::spawn(async move || {
@@ -67,19 +67,24 @@ pub fn copy_clash_env() {
// 从环境变量获取IP地址如果没有则从配置中获取 proxy_host默认为 127.0.0.1
let clash_verge_rev_ip = env::var("CLASH_VERGE_REV_IP").unwrap_or_else(|_| {
Config::verge()
.latest()
.latest_ref()
.proxy_host
.clone()
.unwrap_or_else(|| "127.0.0.1".to_string())
});
let app_handle = handle::Handle::global().app_handle().unwrap();
let port = { Config::verge().latest().verge_mixed_port.unwrap_or(7897) };
let port = {
Config::verge()
.latest_ref()
.verge_mixed_port
.unwrap_or(7897)
};
let http_proxy = format!("http://{clash_verge_rev_ip}:{port}");
let socks5_proxy = format!("socks5://{clash_verge_rev_ip}:{port}");
let cliboard = app_handle.clipboard();
let env_type = { Config::verge().latest().env_type.clone() };
let env_type = { Config::verge().latest_ref().env_type.clone() };
let env_type = match env_type {
Some(env_type) => env_type,
None => {

View File

@@ -99,7 +99,7 @@ async fn clean_async() -> bool {
// 1. 处理TUN模式
let tun_task = async {
if Config::verge().data().enable_tun_mode.unwrap_or(false) {
if Config::verge().data_mut().enable_tun_mode.unwrap_or(false) {
let disable_tun = serde_json::json!({
"tun": {
"enable": false
@@ -240,7 +240,7 @@ pub fn hide() {
use crate::module::lightweight::add_light_weight_timer;
let enable_auto_light_weight_mode = Config::verge()
.data()
.data_mut()
.enable_auto_light_weight_mode
.unwrap_or(false);