use std::future::Future;
#[cfg(not(target_arch = "wasm32"))]
static RUNTIME_HANDLE: std::sync::OnceLock<tokio::runtime::Handle> = std::sync::OnceLock::new();
#[cfg(not(target_arch = "wasm32"))]
pub fn set_runtime_handle(handle: tokio::runtime::Handle) { RUNTIME_HANDLE.set(handle).ok(); }
#[cfg(not(target_arch = "wasm32"))]
pub fn spawn<F>(future: F)
where
F: Future + Send + 'static,
<F as futures::Future>::Output: std::marker::Send,
{
match tokio::runtime::Handle::try_current() {
Ok(handle) => {
handle.spawn(future);
}
Err(_) => {
RUNTIME_HANDLE
.get()
.expect("task::spawn: no Tokio runtime context on this thread. Call ankurah::set_runtime_handle() at init time.")
.spawn(future);
}
}
}
#[cfg(target_arch = "wasm32")]
pub fn spawn<F>(future: F)
where F: Future<Output = ()> + Send + 'static {
wasm_bindgen_futures::spawn_local(future);
}