use futures_core::Stream;
#[cfg(target_arch = "wasm32")]
use futures_util::stream::StreamExt;
use std::pin::Pin;
#[cfg(not(target_arch = "wasm32"))]
pub use tokio::sync::mpsc::Receiver;
#[cfg(target_arch = "wasm32")]
pub use futures::channel::mpsc::{channel, Receiver, Sender};
#[cfg(not(target_arch = "wasm32"))]
pub type BoxEventStream<T> = Pin<Box<dyn Stream<Item = T> + Send + Sync>>;
#[cfg(target_arch = "wasm32")]
pub type BoxEventStream<T> = Pin<Box<dyn Stream<Item = T> + Send>>;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn receiver_into_stream<T: 'static + Send>(rx: Receiver<T>) -> BoxEventStream<T> {
use tokio_stream::wrappers::ReceiverStream;
Box::pin(ReceiverStream::new(rx))
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn receiver_into_stream<T: 'static + Send>(rx: Receiver<T>) -> BoxEventStream<T> {
rx.boxed()
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn spawn_future<F>(fut: F) -> tokio::task::JoinHandle<F::Output>
where
F: std::future::Future + Send + 'static,
F::Output: Send + 'static,
{
tokio::spawn(fut)
}
#[cfg(target_arch = "wasm32")]
pub(crate) fn spawn_future<F>(fut: F)
where
F: std::future::Future<Output = ()> + 'static,
{
wasm_bindgen_futures::spawn_local(fut)
}
#[cfg(target_arch = "wasm32")]
use futures::channel::mpsc::unbounded as unbounded_channel;
#[cfg(target_arch = "wasm32")]
pub type CustomMutex<T> = futures::lock::Mutex<T>;
#[cfg(not(target_arch = "wasm32"))]
pub type CustomMutex<T> = tokio::sync::Mutex<T>;
#[cfg(not(target_arch = "wasm32"))]
pub async fn spawn_blocking<F, R>(f: F) -> Result<R, String>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
tokio::task::spawn_blocking(f)
.await
.map_err(|e| format!("Task join error: {}", e))
}
#[cfg(target_arch = "wasm32")]
pub async fn spawn_blocking<F, R>(f: F) -> Result<R, String>
where
F: FnOnce() -> R + 'static,
R: 'static,
{
Ok(f())
}