refactor: simplify profile retrieval and remove unused template method

This commit is contained in:
Tunglies 2025-11-03 03:17:33 +08:00
parent ed08fadb5a
commit dce349586c
No known key found for this signature in database
GPG Key ID: B9B01B389469B3E8
5 changed files with 19 additions and 70 deletions

View File

@ -26,57 +26,10 @@ static CURRENT_SWITCHING_PROFILE: AtomicBool = AtomicBool::new(false);
#[tauri::command] #[tauri::command]
pub async fn get_profiles() -> CmdResult<IProfiles> { pub async fn get_profiles() -> CmdResult<IProfiles> {
// 策略1: 尝试快速获取latest数据 logging!(debug, Type::Cmd, "获取配置文件列表");
let latest_result = tokio::time::timeout(Duration::from_millis(500), async { let draft = Config::profiles().await;
let profiles = Config::profiles().await; let latest = draft.latest_ref();
let latest = profiles.latest_ref(); Ok((**latest).clone())
IProfiles {
current: latest.current.clone(),
items: latest.items.clone(),
}
})
.await;
match latest_result {
Ok(profiles) => {
logging!(info, Type::Cmd, "快速获取配置列表成功");
return Ok(profiles);
}
Err(_) => {
logging!(warn, Type::Cmd, "快速获取配置超时(500ms)");
}
}
// 策略2: 如果快速获取失败尝试获取data()
let data_result = tokio::time::timeout(Duration::from_secs(2), async {
let profiles = Config::profiles().await;
let data = profiles.latest_ref();
IProfiles {
current: data.current.clone(),
items: data.items.clone(),
}
})
.await;
match data_result {
Ok(profiles) => {
logging!(info, Type::Cmd, "获取draft配置列表成功");
return Ok(profiles);
}
Err(join_err) => {
logging!(
error,
Type::Cmd,
"获取draft配置任务失败或超时: {}",
join_err
);
}
}
// 策略3: fallback尝试重新创建配置
logging!(warn, Type::Cmd, "所有获取配置策略都失败尝试fallback");
Ok(IProfiles::new().await)
} }
/// 增强配置文件 /// 增强配置文件

View File

@ -69,23 +69,16 @@ impl IProfiles {
} }
Err(err) => { Err(err) => {
logging!(error, Type::Config, "{err}"); logging!(error, Type::Config, "{err}");
Self::template() Self::default()
} }
}, },
Err(err) => { Err(err) => {
logging!(error, Type::Config, "{err}"); logging!(error, Type::Config, "{err}");
Self::template() Self::default()
} }
} }
} }
pub fn template() -> Self {
Self {
items: Some(vec![]),
..Self::default()
}
}
pub async fn save_file(&self) -> Result<()> { pub async fn save_file(&self) -> Result<()> {
help::save_yaml( help::save_yaml(
&dirs::profiles_path()?, &dirs::profiles_path()?,
@ -101,12 +94,12 @@ impl IProfiles {
self.items = Some(vec![]); self.items = Some(vec![]);
} }
if let Some(current) = patch.current if let Some(current) = &patch.current
&& let Some(items) = self.items.as_ref() && let Some(items) = self.items.as_ref()
{ {
let some_uid = Some(current); let some_uid = Some(current);
if items.iter().any(|e| e.uid == some_uid) { if items.iter().any(|e| e.uid.as_ref() == some_uid) {
self.current = some_uid; self.current = some_uid.cloned();
} }
} }

View File

@ -241,11 +241,15 @@ pub async fn restore_local_backup(filename: String) -> Result<()> {
return Err(anyhow!("Backup file not found: {}", filename)); return Err(anyhow!("Backup file not found: {}", filename));
} }
let verge = Config::verge().await; let (webdav_url, webdav_username, webdav_password) = {
let verge_data = verge.latest_ref().clone(); let verge = Config::verge().await;
let webdav_url = verge_data.webdav_url.clone(); let verge = verge.latest_ref();
let webdav_username = verge_data.webdav_username.clone(); (
let webdav_password = verge_data.webdav_password.clone(); verge.webdav_url.clone(),
verge.webdav_username.clone(),
verge.webdav_password.clone(),
)
};
let file = AsyncHandler::spawn_blocking(move || std::fs::File::open(&target_path)).await??; let file = AsyncHandler::spawn_blocking(move || std::fs::File::open(&target_path)).await??;
let mut zip = zip::ZipArchive::new(file)?; let mut zip = zip::ZipArchive::new(file)?;

View File

@ -365,7 +365,7 @@ async fn initialize_config_files() -> Result<()> {
if let Ok(path) = dirs::profiles_path() if let Ok(path) = dirs::profiles_path()
&& !path.exists() && !path.exists()
{ {
let template = IProfiles::template(); let template = IProfiles::default();
help::save_yaml(&path, &template, Some("# Clash Verge")) help::save_yaml(&path, &template, Some("# Clash Verge"))
.await .await
.map_err(|e| anyhow::anyhow!("Failed to create profiles config: {}", e))?; .map_err(|e| anyhow::anyhow!("Failed to create profiles config: {}", e))?;

View File

@ -280,7 +280,6 @@ interface IProfileOption {
interface IProfilesConfig { interface IProfilesConfig {
current?: string; current?: string;
valid?: string[];
items?: IProfileItem[]; items?: IProfileItem[];
} }