Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): mark event commands as async #11355

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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