Skip to content

Commit

Permalink
sdk: add abortable thread
Browse files Browse the repository at this point in the history
  • Loading branch information
yukibtc committed Jun 25, 2023
1 parent 21ffe47 commit 31253b2
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions crates/nostr-sdk/src/util/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -92,6 +93,29 @@ where
Some(JoinHandle::Wasm(handle))
}

/// Spawn abortable thread
#[cfg(not(target_arch = "wasm32"))]
pub fn abortable<T>(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<T>(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"))]
Expand Down

0 comments on commit 31253b2

Please sign in to comment.