From 31253b29dd5dcb6873f7aac8e6416d4c949ddb7c Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Sun, 25 Jun 2023 12:38:01 +0200 Subject: [PATCH] sdk: add `abortable` thread --- crates/nostr-sdk/src/util/thread/mod.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/crates/nostr-sdk/src/util/thread/mod.rs b/crates/nostr-sdk/src/util/thread/mod.rs index dd42b5a79..2f41072fb 100644 --- a/crates/nostr-sdk/src/util/thread/mod.rs +++ b/crates/nostr-sdk/src/util/thread/mod.rs @@ -5,6 +5,7 @@ use std::time::Duration; +use nostr_sdk_net::futures_util::stream::{AbortHandle, Abortable}; use nostr_sdk_net::futures_util::Future; #[cfg(feature = "blocking")] use tokio::runtime::{Builder, Runtime}; @@ -92,6 +93,29 @@ where Some(JoinHandle::Wasm(handle)) } +/// Spawn abortable thread +#[cfg(not(target_arch = "wasm32"))] +pub fn abortable(future: T) -> AbortHandle +where + T: Future + Send + 'static, + T::Output: Send + 'static, +{ + let (abort_handle, abort_registration) = AbortHandle::new_pair(); + spawn(Abortable::new(future, abort_registration)); + abort_handle +} + +/// Spawn abortable thread +#[cfg(target_arch = "wasm32")] +pub fn abortable(future: T) -> AbortHandle +where + T: Future + 'static, +{ + let (abort_handle, abort_registration) = AbortHandle::new_pair(); + spawn(Abortable::new(future, abort_registration)); + abort_handle +} + /// Sleep pub async fn sleep(duration: Duration) { #[cfg(not(target_arch = "wasm32"))]