perf(profiles): optimize item removal by uid in profiles management

This commit is contained in:
Tunglies 2026-02-04 12:57:59 +08:00
parent 90e193099f
commit c7f5bc4e0d
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8

View File

@ -45,13 +45,9 @@ macro_rules! patch {
impl IProfiles { impl IProfiles {
// Helper to find and remove an item by uid from the items vec, returning its file name (if any). // Helper to find and remove an item by uid from the items vec, returning its file name (if any).
fn take_item_file_by_uid(items: &mut Vec<PrfItem>, target_uid: Option<String>) -> Option<String> { fn take_item_file_by_uid(items: &mut Vec<PrfItem>, target_uid: Option<&str>) -> Option<String> {
for (i, _) in items.iter().enumerate() { let index = items.iter().position(|item| item.uid.as_deref() == target_uid)?;
if items[i].uid == target_uid { items.remove(index).file
return items.remove(i).file;
}
}
None
} }
pub async fn new() -> Self { pub async fn new() -> Self {
@ -267,33 +263,38 @@ impl IProfiles {
pub async fn delete_item(&mut self, uid: &String) -> Result<bool> { pub async fn delete_item(&mut self, uid: &String) -> Result<bool> {
let current = self.current.as_ref().unwrap_or(uid); let current = self.current.as_ref().unwrap_or(uid);
let current = current.clone(); let current = current.clone();
let item = self.get_item(uid)?; let (merge_uid, script_uid, rules_uid, proxies_uid, groups_uid) = {
let merge_uid = item.option.as_ref().and_then(|e| e.merge.clone()); let item = self.get_item(uid)?;
let script_uid = item.option.as_ref().and_then(|e| e.script.clone()); let option = item.option.as_ref();
let rules_uid = item.option.as_ref().and_then(|e| e.rules.clone()); (
let proxies_uid = item.option.as_ref().and_then(|e| e.proxies.clone()); option.and_then(|e| e.merge.clone()),
let groups_uid = item.option.as_ref().and_then(|e| e.groups.clone()); option.and_then(|e| e.script.clone()),
option.and_then(|e| e.rules.clone()),
option.and_then(|e| e.proxies.clone()),
option.and_then(|e| e.groups.clone()),
)
};
let mut items = self.items.take().unwrap_or_default(); let mut items = self.items.take().unwrap_or_default();
// remove the main item (if exists) and delete its file // remove the main item (if exists) and delete its file
if let Some(file) = Self::take_item_file_by_uid(&mut items, Some(uid.clone())) { if let Some(file) = Self::take_item_file_by_uid(&mut items, Some(uid.as_str())) {
let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await; let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await;
} }
// remove related extension items (merge, script, rules, proxies, groups) // remove related extension items (merge, script, rules, proxies, groups)
if let Some(file) = Self::take_item_file_by_uid(&mut items, merge_uid.clone()) { if let Some(file) = Self::take_item_file_by_uid(&mut items, merge_uid.as_deref()) {
let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await; let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await;
} }
if let Some(file) = Self::take_item_file_by_uid(&mut items, script_uid.clone()) { if let Some(file) = Self::take_item_file_by_uid(&mut items, script_uid.as_deref()) {
let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await; let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await;
} }
if let Some(file) = Self::take_item_file_by_uid(&mut items, rules_uid.clone()) { if let Some(file) = Self::take_item_file_by_uid(&mut items, rules_uid.as_deref()) {
let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await; let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await;
} }
if let Some(file) = Self::take_item_file_by_uid(&mut items, proxies_uid.clone()) { if let Some(file) = Self::take_item_file_by_uid(&mut items, proxies_uid.as_deref()) {
let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await; let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await;
} }
if let Some(file) = Self::take_item_file_by_uid(&mut items, groups_uid.clone()) { if let Some(file) = Self::take_item_file_by_uid(&mut items, groups_uid.as_deref()) {
let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await; let _ = dirs::app_profiles_dir()?.join(file.as_str()).remove_if_exists().await;
} }
// delete the original uid // delete the original uid