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

feat(shell) add wait fn to get event from child process #1947

Draft
wants to merge 4 commits into
base: v2
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions plugins/shell/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,20 @@ class Child {
pid: this.pid
})
}

/**
* Waits for the child to exit completely, returning the status that it exited with.
*
* @returns A promise with ExitStatus.
*
* @since 2.0.2
*/
async wait(): Promise<void> {
await invoke('plugin:shell|wait', {
cmd: 'waitChild',
pid: this.pid
})
}
}

interface CommandEvents {
Expand Down
15 changes: 14 additions & 1 deletion plugins/shell/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tauri::{

use crate::{
open::Program,
process::{CommandEvent, TerminatedPayload},
process::{CommandEvent, ExitStatus, TerminatedPayload},
scope::ExecuteArgs,
Shell,
};
Expand Down Expand Up @@ -302,6 +302,19 @@ pub fn kill<R: Runtime>(
Ok(())
}

#[tauri::command]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use try_wait and if it's ready then wait for the response

pub async fn wait<R: Runtime>(
_window: Window<R>,
shell: State<'_, Shell<R>>,
pid: ChildId,
) -> crate::Result<ExitStatus> {
if let Some(child) = shell.children.lock().unwrap().get(&pid) {
child.wait().await
} else {
Err(crate::Error::UnknowChildProcess)
}
}

#[tauri::command]
pub async fn open<R: Runtime>(
_window: Window<R>,
Expand Down
2 changes: 2 additions & 0 deletions plugins/shell/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub enum Error {
/// Utf8 error.
#[error(transparent)]
Utf8(#[from] std::string::FromUtf8Error),
#[error("Child process does'nt exist")]
UnknowChildProcess,
}

impl Serialize for Error {
Expand Down
8 changes: 8 additions & 0 deletions plugins/shell/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ impl CommandChild {
pub fn pid(&self) -> u32 {
self.inner.id()
}

/// Waits for the child to exit completely, returning the status that it exited with.
pub async fn wait(self) -> crate::Result<ExitStatus> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let exitstatus = self.inner.wait()?;
Ok(ExitStatus {
code: exitstatus.code(),
})
}
}

/// Describes the result of a process after it has terminated.
Expand Down