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: Add JoinHandle::abort() #2877

Merged
merged 3 commits into from
Nov 13, 2021
Merged
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
5 changes: 5 additions & 0 deletions .changes/join-handle-abort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Added `abort` method to `tauri::async_runtime::JoinHandle`.
24 changes: 24 additions & 0 deletions core/tauri/src/async_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ static RUNTIME: OnceCell<Runtime> = OnceCell::new();
#[derive(Debug)]
pub struct JoinHandle<T>(TokioJoinHandle<T>);

impl<T> JoinHandle<T> {
/// Abort the task associated with the handle.
///
/// Awaiting a cancelled task might complete as usual if the task was
/// already completed at the time it was cancelled, but most likely it
/// will fail with a cancelled `JoinError`.
pub fn abort(&self) {
self.0.abort();
}
}

impl<T> Future for JoinHandle<T> {
type Output = crate::Result<T>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Expand Down Expand Up @@ -108,4 +119,17 @@ mod tests {
let handle = handle();
assert_eq!(handle.block_on(async { 0 }), 0);
}

#[tokio::test]
async fn handle_abort() {
let handle = handle();
let join = handle.spawn(async { 5 });
join.abort();
if let crate::Error::JoinError(raw_box) = join.await.unwrap_err() {
let raw_error = raw_box.downcast::<tokio::task::JoinError>().unwrap();
assert!(raw_error.is_cancelled());
} else {
panic!("Abort did not result in the expected `JoinError`");
}
}
}