Skip to content

Commit 3cb73d0

Browse files
authored
fix(core): mark event commands as async (#11355)
* fix(core): mark event commands as async this fixes a deadlock on certain situations * add tag
1 parent ef2482d commit 3cb73d0

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

.changes/event-commands-async.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch:enhance
3+
---
4+
5+
Mark the event commands as async so they do not block the main thread.

crates/tauri/src/event/plugin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl<'de> Deserialize<'de> for WebviewLabel {
6565
}
6666

6767
#[command(root = "crate")]
68-
pub fn listen<R: Runtime>(
68+
pub async fn listen<R: Runtime>(
6969
webview: Webview<R>,
7070
event: EventName,
7171
target: EventTarget,
@@ -75,7 +75,7 @@ pub fn listen<R: Runtime>(
7575
}
7676

7777
#[command(root = "crate")]
78-
pub fn unlisten<R: Runtime>(
78+
pub async fn unlisten<R: Runtime>(
7979
webview: Webview<R>,
8080
event: EventName,
8181
event_id: EventId,
@@ -84,7 +84,7 @@ pub fn unlisten<R: Runtime>(
8484
}
8585

8686
#[command(root = "crate")]
87-
pub fn emit<R: Runtime>(
87+
pub async fn emit<R: Runtime>(
8888
app: AppHandle<R>,
8989
event: EventName,
9090
payload: Option<JsonValue>,
@@ -93,7 +93,7 @@ pub fn emit<R: Runtime>(
9393
}
9494

9595
#[command(root = "crate")]
96-
pub fn emit_to<R: Runtime>(
96+
pub async fn emit_to<R: Runtime>(
9797
app: AppHandle<R>,
9898
target: EventTarget,
9999
event: EventName,

crates/tauri/src/manager/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,14 @@ impl<R: Runtime> AppManager<R> {
532532
let emit_args = EmitArgs::new(event, payload)?;
533533

534534
let listeners = self.listeners();
535-
536-
listeners.emit_js(self.webview.webviews_lock().values(), event, &emit_args)?;
535+
let webviews = self
536+
.webview
537+
.webviews_lock()
538+
.values()
539+
.cloned()
540+
.collect::<Vec<_>>();
541+
542+
listeners.emit_js(webviews.iter(), event, &emit_args)?;
537543
listeners.emit(emit_args)?;
538544

539545
Ok(())

crates/tauri/src/manager/webview.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,9 @@ impl<R: Runtime> WebviewManager<R> {
624624

625625
pub fn eval_script_all<S: Into<String>>(&self, script: S) -> crate::Result<()> {
626626
let script = script.into();
627-
self
628-
.webviews_lock()
629-
.values()
627+
let webviews = self.webviews_lock().values().cloned().collect::<Vec<_>>();
628+
webviews
629+
.iter()
630630
.try_for_each(|webview| webview.eval(&script))
631631
}
632632

0 commit comments

Comments
 (0)