use wasm_bindgen::closure::Closure;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
pub fn next<F: FnOnce() + 'static>(f: F) {
next_dyn(Box::new(f));
}
pub fn next_frame<F: FnOnce() + 'static>(f: F) {
next_frame_dyn(Box::new(f));
}
pub fn after_flush<F: FnOnce() + 'static>(f: F) {
after_flush_dyn(Box::new(f));
}
fn next_dyn(f: Box<dyn FnOnce() + 'static>) {
let Some(window) = web_sys::window() else {
return;
};
let js = Closure::once_into_js(f);
window.queue_microtask(js.unchecked_ref());
}
fn next_frame_dyn(f: Box<dyn FnOnce() + 'static>) {
let Some(window) = web_sys::window() else {
return;
};
let js = Closure::once_into_js(move |_: JsValue| f());
let _ = window.request_animation_frame(js.unchecked_ref());
}
fn after_flush_dyn(f: Box<dyn FnOnce() + 'static>) {
let Some(window) = web_sys::window() else {
return;
};
let js = Closure::once_into_js(f);
let _ = window.set_timeout_with_callback_and_timeout_and_arguments_0(js.unchecked_ref(), 0);
}