refactor: fix

This commit is contained in:
GyDi 2022-11-14 22:50:47 +08:00
parent 09965f1cc6
commit dc941575fe
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A
4 changed files with 30 additions and 57 deletions

View File

@ -44,49 +44,32 @@ pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResu
} }
#[tauri::command] #[tauri::command]
pub fn select_profile(index: String) -> CmdResult { pub async fn select_profile(index: String) -> CmdResult {
let mut profiles = ProfilesN::global().config.lock(); wrap_err!({ ProfilesN::global().config.lock().put_current(index) })?;
wrap_err!(profiles.put_current(index))?; wrap_err!(CoreManager::global().activate_config().await)
drop(profiles);
wrap_err!(tauri::async_runtime::block_on(async {
CoreManager::global().activate_config().await
}))
} }
/// change the profile chain /// change the profile chain
#[tauri::command] #[tauri::command]
pub fn change_profile_chain(chain: Option<Vec<String>>) -> CmdResult { pub async fn change_profile_chain(chain: Option<Vec<String>>) -> CmdResult {
let mut profiles = ProfilesN::global().config.lock(); wrap_err!({ ProfilesN::global().config.lock().put_chain(chain) })?;
wrap_err!(profiles.put_chain(chain))?; wrap_err!(CoreManager::global().activate_config().await)
drop(profiles);
wrap_err!(tauri::async_runtime::block_on(async {
CoreManager::global().activate_config().await
}))
} }
#[tauri::command] #[tauri::command]
pub fn change_profile_valid(valid: Option<Vec<String>>) -> CmdResult { pub async fn change_profile_valid(valid: Option<Vec<String>>) -> CmdResult {
let mut profiles = ProfilesN::global().config.lock(); wrap_err!({ ProfilesN::global().config.lock().put_valid(valid) })?;
wrap_err!(profiles.put_valid(valid))?; wrap_err!(CoreManager::global().activate_config().await)
drop(profiles);
wrap_err!(tauri::async_runtime::block_on(async {
CoreManager::global().activate_config().await
}))
} }
#[tauri::command] #[tauri::command]
pub fn delete_profile(index: String) -> CmdResult { pub async fn delete_profile(index: String) -> CmdResult {
let mut profiles = ProfilesN::global().config.lock(); let should_update = { wrap_err!(ProfilesN::global().config.lock().delete_item(index))? };
if wrap_err!(profiles.delete_item(index))? {
drop(profiles);
wrap_err!(tauri::async_runtime::block_on(async { if should_update {
CoreManager::global().activate_config().await wrap_err!(CoreManager::global().activate_config().await)?;
}))?;
} }
Ok(()) Ok(())
} }
@ -185,10 +168,8 @@ pub fn patch_verge_config(payload: IVerge) -> CmdResult {
} }
#[tauri::command] #[tauri::command]
pub fn change_clash_core(clash_core: Option<String>) -> CmdResult { pub async fn change_clash_core(clash_core: Option<String>) -> CmdResult {
wrap_err!(tauri::async_runtime::block_on(async { wrap_err!(CoreManager::global().change_core(clash_core).await)
CoreManager::global().change_core(clash_core).await
}))
} }
/// restart the sidecar /// restart the sidecar

View File

@ -58,22 +58,11 @@ impl ClashN {
pub fn patch_config(&self, patch: Mapping) -> Result<()> { pub fn patch_config(&self, patch: Mapping) -> Result<()> {
let mut config = self.config.lock(); let mut config = self.config.lock();
let port_key = Value::from("mixed-port");
let server_key = Value::from("external-controller");
let secret_key = Value::from("secret");
let change_info = patch.contains_key(&port_key)
|| patch.contains_key(&server_key)
|| patch.contains_key(&secret_key);
for (key, value) in patch.into_iter() { for (key, value) in patch.into_iter() {
config.insert(key, value); config.insert(key, value);
} }
if change_info { drop(config);
let mut info = self.info.lock();
*info = ClashInfoN::from(&*config);
}
self.save_config() self.save_config()
} }

View File

@ -67,6 +67,7 @@ impl VergeN {
patch!(auto_close_connection); patch!(auto_close_connection);
patch!(default_latency_test); patch!(default_latency_test);
drop(config);
self.save_file() self.save_file()
} }

View File

@ -79,12 +79,12 @@ impl CoreManager {
let clash_core = { self.clash_core.lock().clone() }; let clash_core = { self.clash_core.lock().clone() };
let output = Command::new_sidecar(clash_core)? let output = Command::new_sidecar(clash_core)?
.args(["-t", config_path]) .args(["-t", "-f", config_path])
.output()?; .output()?;
if !output.status.success() { if !output.status.success() {
Logger::global().set_log(output.stderr.clone()); Logger::global().set_log(output.stdout.clone());
bail!("{}", output.stderr); // 过滤掉终端颜色值 bail!("{}", output.stdout); // 过滤掉终端颜色值
} }
Ok(()) Ok(())
@ -177,17 +177,19 @@ impl CoreManager {
// 清掉旧日志 // 清掉旧日志
Logger::global().clear_log(); Logger::global().clear_log();
let old_core = {
let mut self_core = self.clash_core.lock(); let mut self_core = self.clash_core.lock();
let old_core = self_core.clone(); // 保存一下旧值 let old_core = self_core.to_owned(); // 保存一下旧值
*self_core = clash_core.clone(); *self_core = clash_core.clone();
drop(self_core); old_core
};
match self.run_core() { match self.run_core() {
Ok(_) => { Ok(_) => {
// 更新到配置文件 // 更新到配置文件
let mut verge = VergeN::global().config.lock(); {
verge.clash_core = Some(clash_core); VergeN::global().config.lock().clash_core = Some(clash_core);
drop(verge); }
let _ = VergeN::global().save_file(); let _ = VergeN::global().save_file();