re_viewer_context/
async_runtime_handle.rs1#[cfg(not(target_arch = "wasm32"))]
2pub trait WasmNotSend: Send {}
3
4#[cfg(target_arch = "wasm32")]
5pub trait WasmNotSend {}
6
7#[cfg(not(target_arch = "wasm32"))]
8impl<T: Send> WasmNotSend for T {}
9
10#[cfg(target_arch = "wasm32")]
11impl<T> WasmNotSend for T {}
12
13#[derive(Debug, thiserror::Error)]
14pub enum AsyncRuntimeError {
15 #[error("Tokio error: {0}")]
19 TokioError(String),
20}
21
22#[derive(Clone)]
26pub struct AsyncRuntimeHandle {
27 #[cfg(not(target_arch = "wasm32"))]
28 tokio: tokio::runtime::Handle,
29}
30
31impl AsyncRuntimeHandle {
32 #[cfg(not(target_arch = "wasm32"))]
33 pub fn new_native(tokio: tokio::runtime::Handle) -> Self {
34 Self { tokio }
35 }
36
37 #[cfg(target_arch = "wasm32")]
38 pub fn new_web() -> Self {
39 Self {}
40 }
41
42 #[cfg_attr(target_arch = "wasm32", expect(clippy::unnecessary_wraps))]
44 pub fn from_current_tokio_runtime_or_wasmbindgen() -> Result<Self, AsyncRuntimeError> {
45 #[cfg(target_arch = "wasm32")]
46 {
47 Ok(Self::new_web())
48 }
49 #[cfg(not(target_arch = "wasm32"))]
50 {
51 Ok(Self::new_native(
52 tokio::runtime::Handle::try_current()
53 .map_err(|err| AsyncRuntimeError::TokioError(err.to_string()))?
54 .clone(),
55 ))
56 }
57 }
58
59 #[cfg(target_arch = "wasm32")]
60 #[expect(clippy::unused_self)]
61 pub fn spawn_future<F>(&self, future: F)
62 where
63 F: std::future::Future<Output = ()> + WasmNotSend + 'static,
64 {
65 wasm_bindgen_futures::spawn_local(future);
66 }
67
68 #[cfg(not(target_arch = "wasm32"))]
69 pub fn spawn_future<F>(&self, future: F)
70 where
71 F: std::future::Future<Output = ()> + WasmNotSend + 'static,
72 {
73 self.tokio.spawn(future);
74 }
75}