From 63fff7e674192493edc2ebb8f77974f9d7cfe7e2 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Wed, 28 Jun 2023 15:33:08 +0200 Subject: [PATCH] sdk: add `async-utility` dep Remove `gloo-timers` and `wasm-bindgen-futures` deps --- crates/nostr-sdk/Cargo.toml | 5 +- crates/nostr-sdk/src/client/signer/remote.rs | 2 +- crates/nostr-sdk/src/lib.rs | 4 +- crates/nostr-sdk/src/relay/mod.rs | 2 +- crates/nostr-sdk/src/relay/pool.rs | 2 +- crates/nostr-sdk/src/util/mod.rs | 7 -- crates/nostr-sdk/src/util/thread/mod.rs | 125 ------------------- crates/nostr-sdk/src/util/thread/wasm.rs | 40 ------ crates/nostr-sdk/src/util/time.rs | 42 ------- 9 files changed, 6 insertions(+), 223 deletions(-) delete mode 100644 crates/nostr-sdk/src/util/mod.rs delete mode 100644 crates/nostr-sdk/src/util/thread/mod.rs delete mode 100644 crates/nostr-sdk/src/util/thread/wasm.rs delete mode 100644 crates/nostr-sdk/src/util/time.rs diff --git a/crates/nostr-sdk/Cargo.toml b/crates/nostr-sdk/Cargo.toml index 300076e89..8f8b8c9e0 100644 --- a/crates/nostr-sdk/Cargo.toml +++ b/crates/nostr-sdk/Cargo.toml @@ -13,7 +13,7 @@ keywords = ["nostr", "sdk", "rust"] [features] default = ["all-nips"] -blocking = ["dep:once_cell", "nostr/blocking"] +blocking = ["dep:once_cell", "async-utility/blocking", "nostr/blocking"] vanity = ["nostr/vanity"] all-nips = ["nip04", "nip05", "nip06", "nip11", "nip19", "nip46", "nip47"] nip03 = ["nostr/nip03"] @@ -26,6 +26,7 @@ nip46 = ["nostr/nip46"] nip47 = ["nostr/nip47"] [dependencies] +async-utility = "0.1" log = "0.4" nostr = { version = "0.22", path = "../nostr", default-features = false } nostr-sdk-net = { version = "0.22", path = "../nostr-sdk-net" } @@ -36,9 +37,7 @@ thiserror = "1.0" tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "time", "macros", "sync"] } [target.'cfg(target_arch = "wasm32")'.dependencies] -gloo-timers = { version = "0.2", features = ["futures"] } tokio = { version = "1", default-features = false, features = ["rt", "macros", "sync"] } -wasm-bindgen-futures = "0.4" [dev-dependencies] env_logger = "0.10" diff --git a/crates/nostr-sdk/src/client/signer/remote.rs b/crates/nostr-sdk/src/client/signer/remote.rs index e0145f728..9763bee29 100644 --- a/crates/nostr-sdk/src/client/signer/remote.rs +++ b/crates/nostr-sdk/src/client/signer/remote.rs @@ -8,6 +8,7 @@ use std::sync::Arc; use std::time::Duration; +use async_utility::time; use nostr::nips::nip04; use nostr::nips::nip46::{Message, Request, Response}; use nostr::secp256k1::XOnlyPublicKey; @@ -19,7 +20,6 @@ use tokio::sync::Mutex; use crate::client::blocking::Client as BlockingClient; use crate::client::{Client, Error}; use crate::relay::RelayPoolNotification; -use crate::util::time; #[cfg(feature = "blocking")] use crate::RUNTIME; diff --git a/crates/nostr-sdk/src/lib.rs b/crates/nostr-sdk/src/lib.rs index 729dbe8aa..eb96bd849 100644 --- a/crates/nostr-sdk/src/lib.rs +++ b/crates/nostr-sdk/src/lib.rs @@ -14,6 +14,7 @@ #[cfg(all(target_arch = "wasm32", feature = "blocking"))] compile_error!("`blocking` feature can't be enabled for WASM targets"); +pub use nostr::{self, *}; #[cfg(feature = "blocking")] use nostr_sdk_net::futures_util::Future; #[cfg(feature = "blocking")] @@ -21,12 +22,9 @@ use once_cell::sync::Lazy; #[cfg(feature = "blocking")] use tokio::runtime::Runtime; -pub use nostr::{self, *}; - pub mod client; pub mod prelude; pub mod relay; -pub mod util; #[cfg(feature = "blocking")] pub use self::client::blocking; diff --git a/crates/nostr-sdk/src/relay/mod.rs b/crates/nostr-sdk/src/relay/mod.rs index 0da2bf36c..b725bb7ce 100644 --- a/crates/nostr-sdk/src/relay/mod.rs +++ b/crates/nostr-sdk/src/relay/mod.rs @@ -10,6 +10,7 @@ use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; use std::sync::Arc; use std::time::Duration; +use async_utility::{thread, time}; #[cfg(feature = "nip11")] use nostr::nips::nip11::RelayInformationDocument; use nostr::{ClientMessage, Event, Filter, RelayMessage, SubscriptionId, Timestamp, Url}; @@ -21,7 +22,6 @@ use tokio::sync::{broadcast, oneshot, Mutex}; pub mod pool; pub use self::pool::{RelayPoolMessage, RelayPoolNotification}; -use crate::util::{thread, time}; #[cfg(feature = "blocking")] use crate::RUNTIME; diff --git a/crates/nostr-sdk/src/relay/pool.rs b/crates/nostr-sdk/src/relay/pool.rs index 475e7e7a0..36fcf49dc 100644 --- a/crates/nostr-sdk/src/relay/pool.rs +++ b/crates/nostr-sdk/src/relay/pool.rs @@ -10,13 +10,13 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; use std::time::Duration; +use async_utility::thread; use nostr::url::Url; use nostr::{ClientMessage, Event, EventId, Filter, RelayMessage}; use tokio::sync::mpsc::{self, Receiver, Sender}; use tokio::sync::{broadcast, Mutex}; use super::{Error as RelayError, Relay, RelayOptions}; -use crate::util::thread; /// [`RelayPool`] error #[derive(Debug, thiserror::Error)] diff --git a/crates/nostr-sdk/src/util/mod.rs b/crates/nostr-sdk/src/util/mod.rs deleted file mode 100644 index 25f712caf..000000000 --- a/crates/nostr-sdk/src/util/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) 2022-2023 Yuki Kishimoto -// Distributed under the MIT software license - -//! Util - -pub mod thread; -pub mod time; diff --git a/crates/nostr-sdk/src/util/thread/mod.rs b/crates/nostr-sdk/src/util/thread/mod.rs deleted file mode 100644 index 2f41072fb..000000000 --- a/crates/nostr-sdk/src/util/thread/mod.rs +++ /dev/null @@ -1,125 +0,0 @@ -// Copyright (c) 2022-2023 Yuki Kishimoto -// Distributed under the MIT software license - -//! Thread - -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}; - -#[cfg(target_arch = "wasm32")] -mod wasm; - -#[cfg(feature = "blocking")] -fn new_current_thread() -> nostr::Result { - Ok(Builder::new_current_thread().enable_all().build()?) -} - -/// Thread Error -#[derive(Debug, thiserror::Error)] -pub enum Error { - /// Join Error - #[error("Impossible to join thread")] - JoinError, -} - -/// Join Handle -pub enum JoinHandle { - /// Std - #[cfg(not(target_arch = "wasm32"))] - Std(std::thread::JoinHandle), - /// Tokio - #[cfg(not(target_arch = "wasm32"))] - Tokio(tokio::task::JoinHandle), - /// Wasm - #[cfg(target_arch = "wasm32")] - Wasm(self::wasm::JoinHandle), -} - -impl JoinHandle { - /// Join - pub async fn join(self) -> Result { - match self { - #[cfg(not(target_arch = "wasm32"))] - Self::Std(handle) => handle.join().map_err(|_| Error::JoinError), - #[cfg(not(target_arch = "wasm32"))] - Self::Tokio(handle) => handle.await.map_err(|_| Error::JoinError), - #[cfg(target_arch = "wasm32")] - Self::Wasm(handle) => handle.join().await.map_err(|_| Error::JoinError), - } - } -} - -/// Spawn -#[cfg(not(target_arch = "wasm32"))] -pub fn spawn(future: T) -> Option> -where - T: Future + Send + 'static, - T::Output: Send + 'static, -{ - #[cfg(feature = "blocking")] - match new_current_thread() { - Ok(rt) => { - let handle = std::thread::spawn(move || { - let res = rt.block_on(future); - rt.shutdown_timeout(Duration::from_millis(100)); - res - }); - Some(JoinHandle::Std(handle)) - } - Err(e) => { - log::error!("Impossible to create new thread: {:?}", e); - None - } - } - - #[cfg(not(feature = "blocking"))] - { - let handle = tokio::task::spawn(future); - Some(JoinHandle::Tokio(handle)) - } -} - -/// Spawn -#[cfg(target_arch = "wasm32")] -pub fn spawn(future: T) -> Option> -where - T: Future + 'static, -{ - let handle = self::wasm::spawn(future); - 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"))] - tokio::time::sleep(duration).await; - #[cfg(target_arch = "wasm32")] - gloo_timers::future::sleep(duration).await; -} diff --git a/crates/nostr-sdk/src/util/thread/wasm.rs b/crates/nostr-sdk/src/util/thread/wasm.rs deleted file mode 100644 index d9c2ee52e..000000000 --- a/crates/nostr-sdk/src/util/thread/wasm.rs +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2022-2023 Yuki Kishimoto -// Distributed under the MIT software license - -use std::any::Any; -use std::fmt; -use std::thread::Result; - -use nostr_sdk_net::futures_util::Future; -use tokio::sync::oneshot::{self, Receiver}; -use wasm_bindgen_futures::spawn_local; - -pub struct JoinHandle(Receiver); - -impl JoinHandle { - pub async fn join(self) -> Result { - let res = self.0.await; - res.map_err(|e| Box::new(e) as Box<(dyn Any + Send + 'static)>) - } -} - -impl fmt::Debug for JoinHandle { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.pad("JoinHandle { .. }") - } -} - -pub fn spawn(f: T) -> JoinHandle -where - T: Future + 'static, - T::Output: 'static, -{ - let (sender, receiver) = oneshot::channel(); - - spawn_local(async { - let res = f.await; - sender.send(res).ok(); - }); - - JoinHandle(receiver) -} diff --git a/crates/nostr-sdk/src/util/time.rs b/crates/nostr-sdk/src/util/time.rs deleted file mode 100644 index 0223c36a9..000000000 --- a/crates/nostr-sdk/src/util/time.rs +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright (c) 2022-2023 Yuki Kishimoto -// Distributed under the MIT software license - -//! Time module - -use std::future::Future; -use std::time::Duration; - -#[cfg(target_arch = "wasm32")] -use nostr_sdk_net::futures_util::future::{AbortHandle, Abortable}; -#[cfg(target_arch = "wasm32")] -use wasm_bindgen_futures::spawn_local; - -/// Timeout -pub async fn timeout(timeout: Option, future: F) -> Option -where - F: Future, -{ - #[cfg(not(target_arch = "wasm32"))] - if let Some(timeout) = timeout { - tokio::time::timeout(timeout, future).await.ok() - } else { - Some(future.await) - } - - #[cfg(target_arch = "wasm32")] - { - if let Some(timeout) = timeout { - let (abort_handle, abort_registration) = AbortHandle::new_pair(); - let future = Abortable::new(future, abort_registration); - spawn_local(async move { - gloo_timers::callback::Timeout::new(timeout.as_millis() as u32, move || { - abort_handle.abort(); - }) - .forget(); - }); - future.await.ok() - } else { - Some(future.await) - } - } -}