From c742a9a17c4b18cc870fb5c2e59d01e0db49dfd5 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 3 Dec 2026 19:38:18 +0800 Subject: [PATCH] refactor(profile): look up the refresh target once --- crates/clash-verge-profile/src/lib.rs | 27 ++++++++-------- src-tauri/src/config/profiles.rs | 45 +++++++++------------------ src-tauri/src/feat/subscription.rs | 4 +-- 3 files changed, 30 insertions(+), 46 deletions(-) diff --git a/crates/clash-verge-profile/src/lib.rs b/crates/clash-verge-profile/src/lib.rs index 6f2a354d0..261e0079c 100644 --- a/crates/clash-verge-profile/src/lib.rs +++ b/crates/clash-verge-profile/src/lib.rs @@ -157,21 +157,20 @@ impl IProfiles { } pub fn get_item(&self, uid: impl AsRef) -> Result<&PrfItem, ProfileNotFound> { - let uid_str = uid.as_ref(); + let uid = uid.as_ref(); + self.items + .iter() + .flatten() + .find(|item| item.uid.as_deref() == Some(uid)) + .ok_or_else(|| ProfileNotFound { uid: uid.to_owned() }) + } - if let Some(items) = self.items.as_ref() { - for each in items.iter() { - if let Some(uid_val) = &each.uid - && uid_val.as_str() == uid_str - { - return Ok(each); - } - } - } - - Err(ProfileNotFound { - uid: uid_str.to_owned(), - }) + pub fn get_item_mut(&mut self, uid: &str) -> Result<&mut PrfItem, ProfileNotFound> { + self.items + .iter_mut() + .flatten() + .find(|item| item.uid.as_deref() == Some(uid)) + .ok_or_else(|| ProfileNotFound { uid: uid.to_owned() }) } /// Inserts a prepared entry after any required body write. diff --git a/src-tauri/src/config/profiles.rs b/src-tauri/src/config/profiles.rs index 3ae3ed2f1..3b25a8fcc 100644 --- a/src-tauri/src/config/profiles.rs +++ b/src-tauri/src/config/profiles.rs @@ -193,32 +193,6 @@ fn validate_profile_file(file: &str) -> Result<()> { Ok(()) } -/// Updates fields returned by a remote profile refresh. -async fn apply_remote_refresh(profiles: &mut IProfiles, uid: &str, refresh: RemoteProfileRefresh) -> Result<()> { - let _ = profiles.get_item(uid)?; - - if let Some(each) = profiles - .items - .iter_mut() - .flatten() - .find(|item| item.uid.as_deref() == Some(uid)) - { - each.extra = refresh.extra; - each.updated = Some(refresh.updated); - each.home = refresh.home; - each.option = PrfOption::merge(each.option.as_ref(), Some(&refresh.option)); - let file = each - .file - .get_or_insert_with(|| format!("{}.yaml", help::get_uid("R")).into()); - let path = dirs::app_profiles_dir()?.join(file.as_str()); - fs::write(&path, refresh.body.as_bytes()) - .await - .with_context(|| format!("failed to write to file \"{file}\""))?; - } - - save_profiles(profiles).await -} - /// Raise intervals below `min_minutes`. `None`/`0` mean "never auto-update", left alone. /// Returns how many changed so the caller can skip a pointless write. pub(crate) fn raise_short_update_intervals(profiles: &mut IProfiles, min_minutes: u64) -> usize { @@ -315,14 +289,25 @@ pub(crate) async fn profiles_save_file_safe() -> Result<()> { with_profiles_edit(|profiles| async move { save_profiles(&profiles).await.map(|_| profiles) }).await } -pub(crate) async fn apply_remote_refresh_safe(index: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> { +pub(crate) async fn save_remote_refresh(uid: &str, url: &str, refresh: RemoteProfileRefresh) -> Result<()> { with_profiles_edit(|mut profiles| async move { - let current = profiles.get_item(index)?; + let profile = profiles.get_item_mut(uid)?; anyhow::ensure!( - current.itype.as_deref() == Some("remote") && current.url.as_deref() == Some(url), + profile.itype.as_deref() == Some("remote") && profile.url.as_deref() == Some(url), "profile source changed during refresh" ); - apply_remote_refresh(&mut profiles, index, refresh).await?; + profile.extra = refresh.extra; + profile.updated = Some(refresh.updated); + profile.home = refresh.home; + profile.option = PrfOption::merge(profile.option.as_ref(), Some(&refresh.option)); + let file = profile + .file + .get_or_insert_with(|| format!("{}.yaml", help::get_uid("R")).into()); + let path = dirs::app_profiles_dir()?.join(file.as_str()); + fs::write(&path, refresh.body.as_bytes()) + .await + .with_context(|| format!("failed to write to file \"{file}\""))?; + save_profiles(&profiles).await?; Ok(profiles) }) .await diff --git a/src-tauri/src/feat/subscription.rs b/src-tauri/src/feat/subscription.rs index af4125341..469b628b2 100644 --- a/src-tauri/src/feat/subscription.rs +++ b/src-tauri/src/feat/subscription.rs @@ -1,5 +1,5 @@ use crate::{ - config::{Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::apply_remote_refresh_safe}, + config::{Config, PrfOption, download_subscription, prepare_remote_refresh, profiles::save_remote_refresh}, core::{ CoreManager, handle, notify::{NoticeStatus, Refresh, announce}, @@ -133,7 +133,7 @@ async fn perform_profile_update( "profile source changed during refresh" ); let refresh = prepare_remote_refresh(downloaded, merged_opt.as_ref()).await?; - apply_remote_refresh_safe(uid, url, refresh).await?; + save_remote_refresh(uid, url, refresh).await?; if !matches!(strategy, Strategy::Configured) { handle::Handle::notice(NoticeStatus::UpdateWithClashProxy, profile_name.as_str()); }