use std::future::Future;
use std::time::Duration;
#[cfg(feature = "native")]
pub fn spawn<F>(fut: F)
where
F: Future<Output = ()> + Send + 'static,
{
tokio::spawn(fut);
}
#[cfg(all(feature = "wasm", not(feature = "native")))]
pub fn spawn<F>(fut: F)
where
F: Future<Output = ()> + 'static,
{
wasm_bindgen_futures::spawn_local(fut);
}
#[cfg(feature = "native")]
pub async fn sleep(duration: Duration) {
tokio::time::sleep(duration).await;
}
#[cfg(all(feature = "wasm", not(feature = "native")))]
pub async fn sleep(duration: Duration) {
gloo_timers::future::TimeoutFuture::new(duration.as_millis() as u32).await;
}
pub async fn sleep_noop(_duration: Duration) {
}
#[cfg(feature = "native")]
pub fn now_ms() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as i64
}
#[cfg(all(feature = "wasm", not(feature = "native")))]
pub fn now_ms() -> i64 {
js_sys::Date::now() as i64
}
#[cfg(feature = "native")]
pub async fn yield_now() {
tokio::task::yield_now().await;
}
#[cfg(all(feature = "wasm", not(feature = "native")))]
pub async fn yield_now() {
use wasm_bindgen::JsValue;
let promise = js_sys::Promise::resolve(&JsValue::NULL);
let _ = wasm_bindgen_futures::JsFuture::from(promise).await;
}