mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-18 16:30:32 +08:00
refactor: simplify profile retrieval and remove unused template method
This commit is contained in:
parent
ed08fadb5a
commit
dce349586c
@ -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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 增强配置文件
|
/// 增强配置文件
|
||||||
|
|||||||
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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)?;
|
||||||
|
|||||||
@ -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))?;
|
||||||
|
|||||||
1
src/services/types.d.ts
vendored
1
src/services/types.d.ts
vendored
@ -280,7 +280,6 @@ interface IProfileOption {
|
|||||||
|
|
||||||
interface IProfilesConfig {
|
interface IProfilesConfig {
|
||||||
current?: string;
|
current?: string;
|
||||||
valid?: string[];
|
|
||||||
items?: IProfileItem[];
|
items?: IProfileItem[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user