From 6b65b3de94faeb5f4f8776350668501cb3740783 Mon Sep 17 00:00:00 2001 From: amrbashir Date: Wed, 8 May 2024 03:53:32 +0300 Subject: [PATCH] fix(shell/command): retry sending events when it fails ref: https://github.com/tauri-apps/tauri/issues/7684 --- .changes/shell-command-lost-events.md | 5 +++++ Cargo.lock | 1 + plugins/shell/Cargo.toml | 1 + plugins/shell/src/commands.rs | 18 ++++++++++++++++-- 4 files changed, 23 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 000000000..072bf83d3 --- /dev/null +++ b/.changes/shell-command-lost-events.md @@ -0,0 +1,5 @@ +--- +"shell": "patch" +--- + +Fix the JS `Command` API losing events for `stdout`. diff --git a/Cargo.lock b/Cargo.lock index d5762788b..f6cc3a19e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6347,6 +6347,7 @@ dependencies = [ "tauri", "tauri-plugin", "thiserror", + "tokio", ] [[package]] diff --git a/plugins/shell/Cargo.toml b/plugins/shell/Cargo.toml index d031d5239..66240fb44 100644 --- a/plugins/shell/Cargo.toml +++ b/plugins/shell/Cargo.toml @@ -23,6 +23,7 @@ serde = { workspace = true } schemars = { workspace = true } serde_json = { workspace = true } tauri = { workspace = true } +tokio = { version = "1", features = [ "time" ] } log = { workspace = true } thiserror = { workspace = true } shared_child = "1" diff --git a/plugins/shell/src/commands.rs b/plugins/shell/src/commands.rs index 3d860cc6b..ab00a64eb 100644 --- a/plugins/shell/src/commands.rs +++ b/plugins/shell/src/commands.rs @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT -use std::{collections::HashMap, path::PathBuf, string::FromUtf8Error}; +use std::{collections::HashMap, future::Future, path::PathBuf, pin::Pin, string::FromUtf8Error}; use encoding_rs::Encoding; use serde::{Deserialize, Serialize}; @@ -180,7 +180,21 @@ pub fn execute( children.lock().unwrap().remove(&pid); }; let js_event = JSCommandEvent::new(event, encoding); - let _ = on_event.send(&js_event); + + if on_event.send(&js_event).is_err() { + fn send<'a>( + on_event: &'a Channel, + js_event: &'a JSCommandEvent, + ) -> Pin + Send + 'a>> { + Box::pin(async move { + tokio::time::sleep(std::time::Duration::from_millis(15)).await; + if on_event.send(js_event).is_err() { + send(on_event, js_event).await; + } + }) + } + send(&on_event, &js_event).await; + } } });