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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use wasm_bindgen::{closure::Closure, JsCast};
use js_sys::Promise;
use wasm_bindgen::JsValue;
use wasm_bindgen_futures::JsFuture;
thread_local!(static WINDOW: web_sys::Window = web_sys::window().expect("no global `window` exists"));
pub fn window() -> web_sys::Window {
WINDOW.with(|window| window.clone())
}
pub fn history() -> web_sys::History {
window().history().expect("should have a history object")
}
pub fn request_animation_frame<F>(f: F)
where
F: FnMut() + 'static,
{
let closure_raf: Closure<dyn FnMut() + 'static> = Closure::once(f);
request_animation_frame_for_closure(closure_raf)
}
pub(crate) fn request_animation_frame_for_closure(f: Closure<dyn FnMut()>) {
window()
.request_animation_frame(f.as_ref().unchecked_ref())
.expect("should register `requestAnimationFrame` OK");
f.forget()
}
pub fn delay_exec(
closure_delay: Closure<dyn FnMut()>,
timeout: i32,
) -> Option<i32> {
let timeout_id = window()
.set_timeout_with_callback_and_timeout_and_arguments_0(
closure_delay.as_ref().unchecked_ref(),
timeout,
)
.expect("should register the setTimeout call");
closure_delay.forget();
Some(timeout_id)
}
fn future_delay(timeout: i32) -> JsFuture{
let promise = Promise::new(&mut |resolve, _reject|{
delay_exec(Closure::once(move||{
resolve.call0(&JsValue::NULL).expect("must be able to call resolve");
}),
timeout);
});
JsFuture::from(promise)
}
pub async fn async_delay(timeout:i32) {
future_delay(timeout).await.expect("must not error");
}
thread_local!(static DOCUMENT: web_sys::Document = window().document().expect("should have a document on window"));
pub fn document() -> web_sys::Document {
DOCUMENT.with(|document| document.clone())
}
pub fn body() -> web_sys::HtmlElement {
document().body().expect("document should have a body")
}
pub fn performance() -> web_sys::Performance {
window()
.performance()
.expect("should have performance on window")
}
pub fn now() -> f64 {
performance().now()
}