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<()> {
loop {
let document = global::document();
let state = document.ready_state();
if state == "complete" || state == "loaded" {
return Ok(());
}
if state != "loading" {
let links = document.query_selector_all("link[rel=stylesheet]")?;
let applied = (0..links.length()).all(|i| {
links
.item(i)
.and_then(|node| node.dyn_into::<web_sys::HtmlLinkElement>().ok())
.is_none_or(|link| link.sheet().is_some())
});
if applied {
return Ok(());
}
}
set_timeout(50).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?)
}