From c5b3751373e6da2a308828f3e5fd57957fe8938f Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 02:40:01 +0300 Subject: [PATCH] fix(core/shell/command): retry sending events when it fails, closes #7684 --- .changes/shell-command-lost-events.md | 5 +++++ core/tauri/Cargo.toml | 2 +- core/tauri/src/endpoints/shell.rs | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 .changes/shell-command-lost-events.md diff --git a/.changes/shell-command-lost-events.md b/.changes/shell-command-lost-events.md new file mode 100644 index 000000000000..d2d71d568f96 --- /dev/null +++ b/.changes/shell-command-lost-events.md @@ -0,0 +1,5 @@ +--- +"tauri": "patch:bug" +--- + +Fix the JS `Command` API from `shell` module, losing events for `stdout`. diff --git a/core/tauri/Cargo.toml b/core/tauri/Cargo.toml index 35ccded9c7ce..4bcc29d3f1d8 100644 --- a/core/tauri/Cargo.toml +++ b/core/tauri/Cargo.toml @@ -51,7 +51,7 @@ normal = [ "reqwest", "nix" ] [dependencies] serde_json = { version = "1.0", features = [ "raw_value", "preserve_order" ] } serde = { version = "1.0", features = [ "derive" ] } -tokio = { version = "1", features = [ "rt", "rt-multi-thread", "sync", "fs", "io-util" ] } +tokio = { version = "1", features = ["time", "rt", "rt-multi-thread", "sync", "fs", "io-util" ] } futures-util = "0.3" uuid = { version = "1", features = [ "v4" ] } url = { version = "2.3" } diff --git a/core/tauri/src/endpoints/shell.rs b/core/tauri/src/endpoints/shell.rs index daf0d47b161e..ed559e4626a9 100644 --- a/core/tauri/src/endpoints/shell.rs +++ b/core/tauri/src/endpoints/shell.rs @@ -91,6 +91,9 @@ impl Cmd { on_event_fn: CallbackFn, options: CommandOptions, ) -> super::Result { + use std::future::Future; + use std::pin::Pin; + let mut command = if options.sidecar { #[cfg(not(shell_sidecar))] return Err(crate::Error::ApiNotAllowlisted("shell > sidecar".to_string()).into_anyhow()); @@ -170,7 +173,19 @@ impl Cmd { let js = crate::api::ipc::format_callback(on_event_fn, &event) .expect("unable to serialize CommandEvent"); - let _ = context.window.eval(js.as_str()); + fn eval<'a, R: Runtime>( + window: &'a crate::Window, + js: &'a str, + ) -> Pin + Send + 'a>> { + Box::pin(async move { + if window.eval(js).is_err() { + tokio::time::sleep(std::time::Duration::from_millis(15)).await; + eval(window, js).await; + } + }) + } + + eval(&context.window, js.as_str()).await; } });