executor_core/
web.rs

1use crate::{Executor, LocalExecutor};
2use core::future::Future;
3use wasm_bindgen_futures::spawn_local;
4
5pub struct Web;
6
7impl LocalExecutor for Web {
8    fn spawn<T: 'static>(
9        &self,
10        fut: impl Future<Output = T> + 'static,
11    ) -> async_task::Task<T> {
12        let (runnable, task) = async_task::spawn_local(fut, |runnable: async_task::Runnable| {
13            spawn_local(async move {
14                runnable.run();
15            });
16        });
17        runnable.schedule();
18        task
19    }
20}
21
22impl Executor for Web {
23    fn spawn<T: Send + 'static>(
24        &self,
25        fut: impl Future<Output = T> + Send + 'static,
26    ) -> async_task::Task<T> {
27        // In web environment, we use spawn_local even for Send futures
28        // since web workers don't have the same threading model as native
29        let (runnable, task) = async_task::spawn_local(fut, |runnable: async_task::Runnable| {
30            spawn_local(async move {
31                runnable.run();
32            });
33        });
34        runnable.schedule();
35        task
36    }
37}