tsuki_scheduler/runtime/
promise.rs

1use std::future::Future;
2
3use crate::schedule::IntoSchedule;
4use crate::{Runtime, Task};
5use wasm_bindgen_futures::spawn_local;
6
7/// The runtime for javascript environment.
8///
9/// the task is spawned using [`wasm_bindgen_futures::spawn_local`], and be executed with promise.
10///
11/// # Create a new task
12/// see [`Task::promise`]
13pub struct Promise;
14
15impl Runtime for Promise {
16    type Handle = ();
17}
18
19impl Task<Promise> {
20    /// Create a new task that will be executed with promise.
21    ///
22    /// # Example
23    /// ```
24    /// # use tsuki_scheduler::prelude::*;
25    /// async fn some_task() {
26    ///
27    /// }
28    ///
29    /// let task = Task::promise(now(), some_task);
30    /// ```
31    pub fn promise<S, F, Fut>(schedule: S, task: F) -> Self
32    where
33        S: IntoSchedule,
34        S::Output: Send + 'static,
35        F: Fn() -> Fut + Send + 'static,
36        Fut: Future<Output = ()> + 'static,
37    {
38        Task {
39            schedule: Box::new(schedule.into_schedule()),
40            run: Box::new(move |_: _, _: _| spawn_local(task())),
41        }
42    }
43}