From 86c2952ea2c3c648cb445b2087652bc64703e4bb Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Wed, 31 Dec 2025 00:36:18 +0800 Subject: [PATCH] refactor(notification): update send_event method to use AsyncHandler for improved event handling --- src-tauri/src/core/handle.rs | 2 +- src-tauri/src/core/notification.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/core/handle.rs b/src-tauri/src/core/handle.rs index aea3f59ab..5ccafa896 100644 --- a/src-tauri/src/core/handle.rs +++ b/src-tauri/src/core/handle.rs @@ -80,7 +80,7 @@ impl Handle { return; } let webview = Self::get_window(); - NotificationSystem::send_event(webview.as_ref(), event); + NotificationSystem::send_event(webview, event); } pub fn set_is_exiting(&self) { diff --git a/src-tauri/src/core/notification.rs b/src-tauri/src/core/notification.rs index c86061942..9d106f443 100644 --- a/src-tauri/src/core/notification.rs +++ b/src-tauri/src/core/notification.rs @@ -3,6 +3,8 @@ use serde_json::json; use smartstring::alias::String; use tauri::{Emitter as _, WebviewWindow}; +use crate::process::AsyncHandler; + // TODO 重构或优化,避免 Clone 过多 #[derive(Debug, Clone)] pub(super) enum FrontendEvent { @@ -18,13 +20,15 @@ pub(super) enum FrontendEvent { pub(super) struct NotificationSystem; impl NotificationSystem { - pub(super) fn send_event(window: Option<&WebviewWindow>, event: FrontendEvent) { + pub(super) fn send_event(window: Option, event: FrontendEvent) { if let Some(window) = window { - Self::emit_to_window(window, event); + AsyncHandler::spawn_blocking(move || { + Self::emit_to_window(window, event); + }); } } - fn emit_to_window(window: &WebviewWindow, event: FrontendEvent) { + fn emit_to_window(window: WebviewWindow, event: FrontendEvent) { let (event_name, payload) = Self::serialize_event(event); let Ok(payload) = payload else { return;