refactor: use async instead of block_on

This commit is contained in:
MystiPanda
2024-06-29 19:02:37 +08:00
parent c1a201f358
commit 1293d25e1b
11 changed files with 81 additions and 74 deletions

View File

@@ -219,22 +219,18 @@ impl CoreManager {
}
/// 停止核心运行
pub fn stop_core(&self) -> Result<()> {
pub async fn stop_core(&self) -> Result<()> {
// 关闭tun模式
tauri::async_runtime::block_on(async move {
let mut disable = Mapping::new();
let mut tun = Mapping::new();
tun.insert("enable".into(), false.into());
disable.insert("tun".into(), tun.into());
log::debug!(target: "app", "disable tun mode");
let _ = clash_api::patch_configs(&disable).await;
});
let mut disable = Mapping::new();
let mut tun = Mapping::new();
tun.insert("enable".into(), false.into());
disable.insert("tun".into(), tun.into());
log::debug!(target: "app", "disable tun mode");
let _ = clash_api::patch_configs(&disable).await;
if *self.use_service_mode.lock() {
log::debug!(target: "app", "stop the core by service");
tauri::async_runtime::block_on(async move {
log_err!(service::stop_core_by_service().await);
});
log_err!(service::stop_core_by_service().await);
return Ok(());
}
@@ -265,7 +261,7 @@ impl CoreManager {
Config::verge().draft().clash_core = Some(clash_core);
// 更新订阅
Config::generate()?;
Config::generate().await?;
self.check_config()?;
@@ -293,7 +289,7 @@ impl CoreManager {
log::debug!(target: "app", "try to update clash config");
// 更新订阅
Config::generate()?;
Config::generate().await?;
// 检查订阅是否正常
self.check_config()?;

View File

@@ -314,14 +314,16 @@ pub(super) async fn stop_core_by_service() -> Result<()> {
}
/// set dns by service
pub fn set_dns_by_service() -> Result<()> {
pub async fn set_dns_by_service() -> Result<()> {
let url = format!("{SERVICE_URL}/set_dns");
let res = reqwest::blocking::ClientBuilder::new()
let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url)
.send()?
.send()
.await?
.json::<JsonResponse>()
.await
.context("failed to connect to the Clash Verge Service")?;
if res.code != 0 {
@@ -332,14 +334,16 @@ pub fn set_dns_by_service() -> Result<()> {
}
/// unset dns by service
pub fn unset_dns_by_service() -> Result<()> {
pub async fn unset_dns_by_service() -> Result<()> {
let url = format!("{SERVICE_URL}/unset_dns");
let res = reqwest::blocking::ClientBuilder::new()
let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url)
.send()?
.send()
.await?
.json::<JsonResponse>()
.await
.context("failed to connect to the Clash Verge Service")?;
if res.code != 0 {