use ::futures::channel::oneshot::*;
use perspective_js::utils::{global, *};
use wasm_bindgen::prelude::*;
pub async fn await_queue_microtask() -> ApiResult<()> {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = Window)]
type QMWindow;
#[wasm_bindgen(catch, method, structural, js_class = "Window", js_name = queueMicrotask)]
fn queue_microtask(this: &QMWindow, callback: &::js_sys::Function) -> Result<i32, JsValue>;
}
let (sender, receiver) = channel::<()>();
let jsfun = Closure::once_into_js(move || sender.send(()).unwrap());
js_sys::global()
.dyn_into::<QMWindow>()
.unwrap()
.queue_microtask(jsfun.unchecked_ref())?;
Ok(receiver.await?)
}
pub async fn request_animation_frame() {
let (sender, receiver) = channel::<()>();
let jsfun = Closure::once_into_js(move || sender.send(()).unwrap());
global::window()
.request_animation_frame(jsfun.unchecked_ref())
.unwrap();
receiver.await.unwrap()
}
pub async fn await_dom_loaded() -> ApiResult<()> {
let state = global::document().ready_state();
if state == "complete" || state == "loaded" {
Ok(())
} else {
let (sender, receiver) = channel::<()>();
let jsfun = Closure::once_into_js(move || sender.send(()).unwrap());
global::window().add_event_listener_with_callback("load", jsfun.unchecked_ref())?;
Ok(receiver.await?)
}
}
pub async fn set_timeout(timeout: i32) -> ApiResult<()> {
let (sender, receiver) = channel::<()>();
let jsfun = Closure::once_into_js(move || {
let _ = sender.send(());
});
global::window()
.set_timeout_with_callback_and_timeout_and_arguments_0(jsfun.unchecked_ref(), timeout)?;
Ok(receiver.await?)
}