mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-21 03:32:09 +08:00
fix: dispatch frontend events on main thread
This commit is contained in:
@@ -90,7 +90,7 @@ impl Handle {
|
||||
return;
|
||||
}
|
||||
|
||||
NotificationSystem::send_event(event);
|
||||
NotificationSystem::send_event(Self::app_handle().clone(), event);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use crate::utils::window_manager::WindowManager;
|
||||
use clash_verge_logging::{Type, logging};
|
||||
use serde_json::json;
|
||||
use smartstring::alias::String;
|
||||
|
||||
use tauri::{Emitter as _, WebviewWindow};
|
||||
use tauri::{AppHandle, Emitter as _, Manager as _, WebviewWindow};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum FrontendEvent<'a> {
|
||||
@@ -21,11 +20,7 @@ pub enum FrontendEvent<'a> {
|
||||
pub struct NotificationSystem {}
|
||||
|
||||
impl NotificationSystem {
|
||||
fn emit_to_window(window: &WebviewWindow, event: FrontendEvent) {
|
||||
let (event_name, Ok(payload)) = Self::serialize_event(event) else {
|
||||
return;
|
||||
};
|
||||
|
||||
fn emit_to_window(window: &WebviewWindow, event_name: &'static str, payload: serde_json::Value) {
|
||||
if let Err(e) = window.emit(event_name, payload) {
|
||||
logging!(warn, Type::Frontend, "Event emit failed: {}", e);
|
||||
}
|
||||
@@ -46,9 +41,19 @@ impl NotificationSystem {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn send_event(event: FrontendEvent) {
|
||||
if let Some(window) = WindowManager::get_main_window() {
|
||||
Self::emit_to_window(&window, event);
|
||||
pub(crate) fn send_event(app_handle: AppHandle, event: FrontendEvent) {
|
||||
let (event_name, Ok(payload)) = Self::serialize_event(event) else {
|
||||
return;
|
||||
};
|
||||
let dispatch_handle = app_handle.clone();
|
||||
// Emitting from a runtime worker can deadlock on macOS when WebKit's protocol handler
|
||||
// waits for Tauri's webview lock while emit waits synchronously for the main thread.
|
||||
if let Err(err) = app_handle.run_on_main_thread(move || {
|
||||
if let Some(window) = dispatch_handle.get_webview_window("main") {
|
||||
Self::emit_to_window(&window, event_name, payload);
|
||||
}
|
||||
}) {
|
||||
logging!(warn, Type::Frontend, "Failed to dispatch event on main thread: {err}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user