1use std::future::Future;
2use wasm_bindgen::prelude::*;
3use wasm_bindgen_futures::future_to_promise;
4use worker_sys::{ScheduleContext as EdgeScheduleContext, ScheduledEvent as EdgeScheduledEvent};
5
6#[derive(Debug, Clone)]
8pub struct ScheduledEvent {
9 cron: String,
10 scheduled_time: f64,
11 ty: String,
12}
13
14impl From<EdgeScheduledEvent> for ScheduledEvent {
15 fn from(schedule: EdgeScheduledEvent) -> Self {
16 Self {
17 cron: schedule.cron().unwrap(),
18 scheduled_time: schedule.scheduled_time().unwrap(),
19 ty: String::from("scheduled"),
20 }
21 }
22}
23
24impl ScheduledEvent {
25 pub fn cron(&self) -> String {
27 self.cron.clone()
28 }
29
30 pub fn ty(&self) -> String {
32 self.ty.clone()
33 }
34
35 pub fn schedule(&self) -> f64 {
37 self.scheduled_time
38 }
39}
40
41#[derive(Debug, Clone)]
42pub struct ScheduleContext {
43 edge: EdgeScheduleContext,
44}
45
46impl From<EdgeScheduleContext> for ScheduleContext {
47 fn from(edge: EdgeScheduleContext) -> Self {
48 Self { edge }
49 }
50}
51
52impl ScheduleContext {
53 pub fn wait_until<T>(&self, handler: T)
54 where
55 T: Future<Output = ()> + 'static,
56 {
57 self.edge
58 .wait_until(future_to_promise(async {
59 handler.await;
60 Ok(JsValue::null())
61 }))
62 .unwrap()
63 }
64}