fix: simplify conditional checks and improve async handler usage across multiple files (#5156)

* fix: simplify conditional checks and improve async handler usage across multiple files

* fix: add missing AsyncHandler import in find_processes_by_name function

* fix: remove redundant AsyncHandler import in find_processes_by_name function
This commit is contained in:
Tunglies
2025-10-21 22:39:32 +08:00
committed by GitHub
parent 9c9aefe4cd
commit afb049ca17
8 changed files with 28 additions and 26 deletions

View File

@@ -39,14 +39,15 @@ impl Handle {
} }
let mut system_opt = self.notification_system.write(); let mut system_opt = self.notification_system.write();
if let Some(system) = system_opt.as_mut() { if let Some(system) = system_opt.as_mut()
if !system.is_running { && !system.is_running
system.start(); {
} system.start();
} }
} }
pub fn app_handle() -> &'static AppHandle { pub fn app_handle() -> &'static AppHandle {
#[allow(clippy::expect_used)]
APP_HANDLE.get().expect("App handle not initialized") APP_HANDLE.get().expect("App handle not initialized")
} }

View File

@@ -50,10 +50,10 @@ impl CoreManager {
let now = Instant::now(); let now = Instant::now();
let mut last = self.last_update.lock(); let mut last = self.last_update.lock();
if let Some(last_time) = *last { if let Some(last_time) = *last
if now.duration_since(last_time) < timing::CONFIG_UPDATE_DEBOUNCE { && now.duration_since(last_time) < timing::CONFIG_UPDATE_DEBOUNCE
return Ok(false); {
} return Ok(false);
} }
*last = Some(now); *last = Some(now);

View File

@@ -65,6 +65,7 @@ impl CoreManager {
} }
async fn prepare_startup(&self) -> Result<()> { async fn prepare_startup(&self) -> Result<()> {
#[cfg(target_os = "windows")]
self.wait_for_service_if_needed().await; self.wait_for_service_if_needed().await;
let mode = match SERVICE_MANAGER.lock().await.current() { let mode = match SERVICE_MANAGER.lock().await.current() {
@@ -121,7 +122,4 @@ impl CoreManager {
let _ = backoff::future::retry(backoff, operation).await; let _ = backoff::future::retry(backoff, operation).await;
} }
#[cfg(not(target_os = "windows"))]
async fn wait_for_service_if_needed(&self) {}
} }

View File

@@ -1,11 +1,14 @@
use super::CoreManager; use super::CoreManager;
#[cfg(windows)]
use crate::process::AsyncHandler;
use crate::{ use crate::{
AsyncHandler,
constants::{process, timing}, constants::{process, timing},
logging, logging,
utils::logging::Type, utils::logging::Type,
}; };
use anyhow::{Result, anyhow}; use anyhow::Result;
#[cfg(windows)]
use anyhow::anyhow;
impl CoreManager { impl CoreManager {
pub async fn cleanup_orphaned_processes(&self) -> Result<()> { pub async fn cleanup_orphaned_processes(&self) -> Result<()> {

View File

@@ -96,9 +96,9 @@ impl NotificationSystem {
while !handle.is_exiting() { while !handle.is_exiting() {
match rx.recv_timeout(std::time::Duration::from_millis(100)) { match rx.recv_timeout(std::time::Duration::from_millis(100)) {
Ok(event) => Self::process_event(&handle, event), Ok(event) => Self::process_event(handle, event),
Err(mpsc::RecvTimeoutError::Disconnected) => break, Err(mpsc::RecvTimeoutError::Disconnected) => break,
Err(mpsc::RecvTimeoutError::Timeout) => continue, Err(mpsc::RecvTimeoutError::Timeout) => break,
} }
} }
} }

View File

@@ -85,7 +85,7 @@ mod app_init {
app.deep_link().on_open_url(|event| { app.deep_link().on_open_url(|event| {
let url = event.urls().first().map(|u| u.to_string()); let url = event.urls().first().map(|u| u.to_string());
if let Some(url) = url { if let Some(url) = url {
let _ = AsyncHandler::spawn(|| async { AsyncHandler::spawn(|| async {
if let Err(e) = resolve::resolve_scheme(url).await { if let Err(e) = resolve::resolve_scheme(url).await {
logging!(error, Type::Setup, "Failed to resolve scheme: {}", e); logging!(error, Type::Setup, "Failed to resolve scheme: {}", e);
} }
@@ -300,7 +300,7 @@ pub fn run() {
} }
pub fn handle_window_focus(focused: bool) { pub fn handle_window_focus(focused: bool) {
let _ = AsyncHandler::spawn(move || async move { AsyncHandler::spawn(move || async move {
let is_enable_global_hotkey = Config::verge() let is_enable_global_hotkey = Config::verge()
.await .await
.latest_ref() .latest_ref()
@@ -339,7 +339,7 @@ pub fn run() {
} }
pub fn handle_window_destroyed() { pub fn handle_window_destroyed() {
let _ = AsyncHandler::spawn(|| async { AsyncHandler::spawn(|| async {
let _ = handle::Handle::mihomo() let _ = handle::Handle::mihomo()
.await .await
.clear_all_ws_connections() .clear_all_ws_connections()
@@ -396,7 +396,7 @@ pub fn run() {
if core::handle::Handle::global().is_exiting() { if core::handle::Handle::global().is_exiting() {
return; return;
} }
let _ = AsyncHandler::spawn(move || async move { AsyncHandler::spawn(move || async move {
event_handlers::handle_reopen(has_visible_windows).await; event_handlers::handle_reopen(has_visible_windows).await;
}); });
} }

View File

@@ -26,14 +26,14 @@ pub fn resolve_setup_handle() {
} }
pub fn resolve_setup_sync() { pub fn resolve_setup_sync() {
let _ = AsyncHandler::spawn(|| async { AsyncHandler::spawn(|| async {
let _ = AsyncHandler::spawn_blocking(init_scheme); AsyncHandler::spawn_blocking(init_scheme);
let _ = AsyncHandler::spawn_blocking(init_embed_server); AsyncHandler::spawn_blocking(init_embed_server);
}); });
} }
pub fn resolve_setup_async() { pub fn resolve_setup_async() {
let _ = AsyncHandler::spawn(|| async { AsyncHandler::spawn(|| async {
#[cfg(not(feature = "tauri-dev"))] #[cfg(not(feature = "tauri-dev"))]
resolve_setup_logger().await; resolve_setup_logger().await;
logging!( logging!(
@@ -53,7 +53,7 @@ pub fn resolve_setup_async() {
init_service_manager().await; init_service_manager().await;
init_core_manager().await; init_core_manager().await;
init_system_proxy().await; init_system_proxy().await;
let _ = AsyncHandler::spawn_blocking(init_system_proxy_guard); AsyncHandler::spawn_blocking(init_system_proxy_guard);
}); });
let tray_init = async { let tray_init = async {

View File

@@ -67,7 +67,7 @@ pub fn embed_server() {
.expect("failed to set shutdown signal for embedded server"); .expect("failed to set shutdown signal for embedded server");
let port = IVerge::get_singleton_port(); let port = IVerge::get_singleton_port();
let _ = AsyncHandler::spawn(move || async move { AsyncHandler::spawn(move || async move {
let visible = warp::path!("commands" / "visible").and_then(|| async { let visible = warp::path!("commands" / "visible").and_then(|| async {
logging!(info, Type::Window, "检测到从单例模式恢复应用窗口"); logging!(info, Type::Window, "检测到从单例模式恢复应用窗口");
if !lightweight::exit_lightweight_mode().await { if !lightweight::exit_lightweight_mode().await {
@@ -108,7 +108,7 @@ pub fn embed_server() {
.and(warp::query::<QueryParam>()) .and(warp::query::<QueryParam>())
.map(|query: QueryParam| { .map(|query: QueryParam| {
let param = query.param.clone(); let param = query.param.clone();
let _ = tokio::task::spawn_local(async move { tokio::task::spawn_local(async move {
logging_error!(Type::Setup, resolve::resolve_scheme(param).await); logging_error!(Type::Setup, resolve::resolve_scheme(param).await);
}); });
warp::reply::with_status::<String>("ok".into(), warp::http::StatusCode::OK) warp::reply::with_status::<String>("ok".into(), warp::http::StatusCode::OK)