Skip to content

Commit

Permalink
fix(shell/command): retry sending events when it fails
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed May 8, 2024
1 parent 56dde76 commit 6b65b3d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changes/shell-command-lost-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"shell": "patch"
---

Fix the JS `Command` API losing events for `stdout`.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions plugins/shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 16 additions & 2 deletions plugins/shell/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
}
}
});

Expand Down

0 comments on commit 6b65b3d

Please sign in to comment.