mod time;
use std::future::Future;
use tokio_with_wasm::alias as tokio;
pub type JoinHandle<T> = tokio::task::JoinHandle<T>;
pub type Duration = std::time::Duration;
pub type Instant = web_time::Instant;
pub fn sleep(dur: Duration) -> impl Future<Output = ()> + Send {
time::sleep(dur)
}
pub type Interval = time::Interval;
pub fn interval(dur: Duration) -> Interval {
time::interval(dur)
}
pub type JoinSet<T> = tokio::task::JoinSet<T>;
pub fn spawn<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
spawn_named(None, future)
}
pub fn spawn_local<F>(future: F) -> JoinHandle<F::Output>
where
F: Future + 'static,
{
tokio_with_wasm::spawn(future)
}
pub fn spawn_named<F>(name: Option<&str>, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
#[cfg(tokio_unstable)]
{
let mut builder = tokio::task::Builder::new();
if let Some(name) = name {
builder = builder.name(name);
}
builder.spawn(future).expect("Tokio task spawn failed")
}
#[cfg(not(tokio_unstable))]
{
let _ = name;
tokio::task::spawn(future)
}
}
pub async fn timeout<F, T>(dur: super::Duration, future: F) -> Result<T, super::Timeout>
where
F: Future<Output = T> + Send,
T: 'static,
{
time::timeout(dur, future).await.map_err(|_| super::Timeout)
}
macro_rules! select {
($($tokens:tt)*) => {{
tokio::select! {
biased;
$( $tokens )*
}
}}
}
pub(crate) use select;
#[cfg(test)]
pub use wasm_bindgen_test::wasm_bindgen_test as test;