Skip to content

Commit

Permalink
fix(core): mark event commands as async (#11355)
Browse files Browse the repository at this point in the history
* fix(core): mark event commands as async

this fixes a deadlock on certain situations

* add tag
  • Loading branch information
lucasfernog authored Oct 15, 2024
1 parent ef2482d commit 3cb73d0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changes/event-commands-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch:enhance
---

Mark the event commands as async so they do not block the main thread.
8 changes: 4 additions & 4 deletions crates/tauri/src/event/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl<'de> Deserialize<'de> for WebviewLabel {
}

#[command(root = "crate")]
pub fn listen<R: Runtime>(
pub async fn listen<R: Runtime>(
webview: Webview<R>,
event: EventName,
target: EventTarget,
Expand All @@ -75,7 +75,7 @@ pub fn listen<R: Runtime>(
}

#[command(root = "crate")]
pub fn unlisten<R: Runtime>(
pub async fn unlisten<R: Runtime>(
webview: Webview<R>,
event: EventName,
event_id: EventId,
Expand All @@ -84,7 +84,7 @@ pub fn unlisten<R: Runtime>(
}

#[command(root = "crate")]
pub fn emit<R: Runtime>(
pub async fn emit<R: Runtime>(
app: AppHandle<R>,
event: EventName,
payload: Option<JsonValue>,
Expand All @@ -93,7 +93,7 @@ pub fn emit<R: Runtime>(
}

#[command(root = "crate")]
pub fn emit_to<R: Runtime>(
pub async fn emit_to<R: Runtime>(
app: AppHandle<R>,
target: EventTarget,
event: EventName,
Expand Down
10 changes: 8 additions & 2 deletions crates/tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,14 @@ impl<R: Runtime> AppManager<R> {
let emit_args = EmitArgs::new(event, payload)?;

let listeners = self.listeners();

listeners.emit_js(self.webview.webviews_lock().values(), event, &emit_args)?;
let webviews = self
.webview
.webviews_lock()
.values()
.cloned()
.collect::<Vec<_>>();

listeners.emit_js(webviews.iter(), event, &emit_args)?;
listeners.emit(emit_args)?;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions crates/tauri/src/manager/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,9 @@ impl<R: Runtime> WebviewManager<R> {

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

Expand Down

0 comments on commit 3cb73d0

Please sign in to comment.