use std::time::Duration;
#[cfg(not(feature = "js"))]
pub use std::time::Instant;
#[cfg(feature = "js")]
pub use web_time::Instant;
#[cfg(not(feature = "js"))]
pub async fn sleep(duration: Duration) {
tokio::time::sleep(duration).await;
}
#[cfg(feature = "js")]
pub async fn sleep(duration: Duration) {
futures_timer::Delay::new(duration).await;
}
#[cfg(not(feature = "js"))]
pub async fn sleep_until(deadline: Instant) {
tokio::time::sleep_until(deadline.into()).await;
}
#[cfg(feature = "js")]
pub async fn sleep_until(deadline: Instant) {
let now = Instant::now();
if let Some(duration) = deadline.checked_duration_since(now) {
futures_timer::Delay::new(duration).await;
}
}
#[cfg(not(feature = "js"))]
pub async fn timeout<F: std::future::Future>(
duration: Duration,
future: F,
) -> Result<F::Output, tokio::time::error::Elapsed> {
tokio::time::timeout(duration, future).await
}
#[cfg(feature = "js")]
pub async fn timeout<F: std::future::Future>(duration: Duration, future: F) -> Result<F::Output, Elapsed> {
use futures::future::Either;
use std::pin::pin;
let delay = pin!(futures_timer::Delay::new(duration));
let future = pin!(future);
match futures::future::select(future, delay).await {
Either::Left((output, _)) => Ok(output),
Either::Right((_, _)) => Err(Elapsed),
}
}
#[cfg(feature = "js")]
#[derive(Debug)]
pub struct Elapsed;
#[cfg(feature = "js")]
impl std::fmt::Display for Elapsed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "deadline has elapsed")
}
}
#[cfg(feature = "js")]
impl std::error::Error for Elapsed {}