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
//! Futures support for reactive scopes.

#![deny(missing_debug_implementations)]

use std::pin::Pin;

use futures::future::abortable;
use futures::Future;
use sycamore_reactive::{on_cleanup, Scope};

/// If running on `wasm32` target, does nothing. Otherwise creates a new `tokio::task::LocalSet`
/// scope.
///
/// Normally, you do not need to call this as it is handled internally by Sycamore when creating
/// your app.
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
    }
}

/// Spawns a `!Send` future on the current scope. If the scope is destroyed before the future is
/// completed, it is aborted immediately. This ensures that it is impossible to access any
/// values referencing the scope after they are destroyed.
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);
    // SAFETY: We are just transmuting the lifetime here so that we can spawn the future.
    // This is safe because we wrap the future in an `Abortable` future which will be
    // immediately aborted once the reactive scope is dropped.
    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;
    });
}