fix(shell/command): retry sending events when it fails (#1298)

ref: https://github.com/tauri-apps/tauri/issues/7684
This commit is contained in:
Amr Bashir
2024-05-09 16:38:56 +03:00
committed by GitHub
parent 6af3216fab
commit 5c1b7917e4
4 changed files with 23 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"shell": "patch"
---
Fix the JS `Command` API losing events for `stdout`.
Generated
+1
View File
@@ -6347,6 +6347,7 @@ dependencies = [
"tauri",
"tauri-plugin",
"thiserror",
"tokio",
]
[[package]]
+1
View File
@@ -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"
+16 -2
View File
@@ -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<R: Runtime>(
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<Box<dyn Future<Output = ()> + 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;
}
}
});