1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#![deny(missing_debug_implementations)]
use std::pin::Pin;
use futures::future::abortable;
use futures::Future;
use sycamore_reactive::{on_cleanup, Scope};
pub async fn provide_executor_scope<U>(f: impl Future<Output = U>) -> U {
#[cfg(target_arch = "wasm32")]
{
f.await
}
#[cfg(not(target_arch = "wasm32"))]
{
let local = tokio::task::LocalSet::new();
local.run_until(f).await
}
}
pub fn spawn_local_scoped<'a>(cx: Scope<'a>, f: impl Future<Output = ()> + 'a) {
let boxed: Pin<Box<dyn Future<Output = ()> + 'a>> = Box::pin(f);
let extended: Pin<Box<dyn Future<Output = ()> + 'static>> =
unsafe { std::mem::transmute(boxed) };
let (abortable, handle) = abortable(extended);
on_cleanup(cx, move || handle.abort());
#[cfg(not(target_arch = "wasm32"))]
tokio::task::spawn_local(abortable);
#[cfg(target_arch = "wasm32")]
wasm_bindgen_futures::spawn_local(async move {
let _ = abortable.await;
});
}