mirror of
https://github.com/leptos-rs/leptos
synced 2024-11-14 00:27:12 +00:00
24 lines
703 B
Rust
24 lines
703 B
Rust
#![forbid(unsafe_code)]
|
|
use cfg_if::cfg_if;
|
|
use std::future::Future;
|
|
|
|
/// Spawns and runs a thread-local [`Future`] in a platform-independent way.
|
|
///
|
|
/// This can be used to interface with any `async` code.
|
|
pub fn spawn_local<F>(fut: F)
|
|
where
|
|
F: Future<Output = ()> + 'static,
|
|
{
|
|
cfg_if! {
|
|
if #[cfg(all(target_arch = "wasm32", any(feature = "csr", feature = "hydrate")))] {
|
|
wasm_bindgen_futures::spawn_local(fut)
|
|
}
|
|
else if #[cfg(any(test, doctest))] {
|
|
tokio_test::block_on(fut);
|
|
} else if #[cfg(feature = "ssr")] {
|
|
tokio::task::spawn_local(fut);
|
|
} else {
|
|
futures::executor::block_on(fut)
|
|
}
|
|
}
|
|
}
|