use js_sys::Promise;
use wasm_bindgen::{closure::Closure, JsCast, JsValue};
pub use wasm_bindgen_futures::spawn_local;
use wasm_bindgen_futures::JsFuture;
thread_local!(static WINDOW: web_sys::Window = web_sys::window().expect("no global `window` exists"));
thread_local!(static DOCUMENT: web_sys::Document = window().document().expect("should have a document on window"));
pub fn window() -> web_sys::Window {
WINDOW.with(|window| window.clone())
}
pub fn document() -> web_sys::Document {
DOCUMENT.with(|document| document.clone())
}
pub fn history() -> web_sys::History {
window().history().expect("should have a history object")
}
pub fn request_animation_frame<F>(f: F) -> Result<i32, JsValue>
where
F: FnMut() + 'static,
{
let closure_raf: Closure<dyn FnMut() + 'static> = Closure::once(f);
let handle = window().request_animation_frame(closure_raf.as_ref().unchecked_ref())?;
closure_raf.forget();
Ok(handle)
}
pub fn cancel_animation_frame(handle: i32) -> Result<(), JsValue>{
window().cancel_animation_frame(handle)
}
pub fn request_idle_callback<F>(f: F) -> Result<u32, JsValue>
where
F: Fn(web_sys::IdleDeadline) + 'static,
{
let closure = Closure::once(move |v: JsValue| {
let deadline = v
.dyn_into::<web_sys::IdleDeadline>()
.expect("must have an idle deadline");
f(deadline);
});
let handle = window().request_idle_callback(closure.as_ref().unchecked_ref())?;
closure.forget();
Ok(handle)
}
pub fn cancel_idle_callback(handle: u32) -> Result<(), JsValue>{
window().cancel_idle_callback(handle);
Ok(())
}
pub fn request_timeout_callback<F>(f: F, timeout: i32) -> Result<i32, JsValue>
where
F: FnMut() + 'static,
{
let closure = Closure::once(f);
let handle = window().set_timeout_with_callback_and_timeout_and_arguments_0(closure.as_ref().unchecked_ref(), timeout)?;
closure.forget();
Ok(handle)
}
pub fn cancel_timeout_callback(handle: i32) -> Result<(), JsValue>{
window().clear_timeout_with_handle(handle);
Ok(())
}
pub fn delay_exec<F>(f: F, timeout: i32) -> Result<i32, JsValue>
where
F: FnMut() + 'static,
{
let closure_delay = Closure::once(f);
let timeout_id = window().set_timeout_with_callback_and_timeout_and_arguments_0(
closure_delay.as_ref().unchecked_ref(),
timeout,
);
closure_delay.forget();
timeout_id
}
pub async fn async_delay(timeout: i32) {
let promise = Promise::new(&mut |resolve, _reject| {
let _handle = delay_exec(
move || {
resolve
.call0(&JsValue::NULL)
.expect("must be able to call resolve");
},
timeout,
).expect("must schedule it");
});
JsFuture::from(promise).await.expect("must not error");
}
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()
}