Skip to main content

flare_core/client/
runtime.rs

1//! Platform runtime hooks for client background tasks.
2
3#[cfg(not(target_arch = "wasm32"))]
4pub fn spawn_client_task<F>(future: F)
5where
6    F: std::future::Future<Output = ()> + Send + 'static,
7{
8    if let Ok(handle) = tokio::runtime::Handle::try_current() {
9        handle.spawn(future);
10        return;
11    }
12    if let Ok(runtime) = tokio::runtime::Runtime::new() {
13        runtime.spawn(future);
14    }
15}
16
17#[cfg(target_arch = "wasm32")]
18pub fn spawn_client_task<F>(future: F)
19where
20    F: std::future::Future<Output = ()> + 'static,
21{
22    wasm_bindgen_futures::spawn_local(future);
23}
24
25#[cfg(not(target_arch = "wasm32"))]
26pub fn run_client_async<F, T>(future: F) -> T
27where
28    F: std::future::Future<Output = T>,
29{
30    tokio::task::block_in_place(|| {
31        if let Ok(handle) = tokio::runtime::Handle::try_current() {
32            return handle.block_on(future);
33        }
34        let rt = tokio::runtime::Runtime::new().expect("failed to build tokio runtime");
35        rt.block_on(future)
36    })
37}