#[cfg(feature = "spawn")]
pub mod spawn;
#[cfg(feature = "spawn")]
pub use spawn::spawn;
#[cfg(feature = "sleep")]
pub mod sleep;
#[cfg(feature = "sleep")]
pub use sleep::sleep;
#[cfg(feature = "queue")]
pub mod queue;
#[cfg(feature = "queue")]
pub use queue::Queue;
#[cfg(feature = "event")]
pub mod event;
use std::fmt::Display;
use wasm_bindgen::prelude::*;
use web_sys::{Document, HtmlElement, Window};
#[cfg(feature = "panic_hook")]
pub fn set_panic_hook() {
console_error_panic_hook::set_once();
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn warn(s: &str);
#[wasm_bindgen(js_namespace = console)]
pub fn error(s: &str);
}
#[macro_export]
macro_rules! console_log {
($($t:tt)*) => {
{
use $crate::log;
(log(&format_args!($($t)*).to_string()))
}
}
}
#[macro_export]
macro_rules! console_warn {
($($t:tt)*) => {
{
use $crate::warn;
(warn(&format_args!($($t)*).to_string()))
}
}
}
#[macro_export]
macro_rules! console_error {
($($t:tt)*) => {
{
use $crate::error;
(error(&format_args!($($t)*).to_string()))
}
}
}
#[macro_export]
macro_rules! closure {
($expression:expr) => {{
use wasm_bindgen::prelude::Closure;
Closure::wrap(Box::new($expression) as Box<dyn FnMut(_)>)
}};
}
pub fn window() -> Window {
web_sys::window().expect("no global window exists")
}
pub fn document() -> Document {
window()
.document()
.expect("should have a document on window")
}
pub fn body() -> HtmlElement {
document().body().expect("document should have a body")
}
#[derive(Debug)]
pub struct JsError(pub JsValue);
impl Display for JsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl std::error::Error for JsError {}
impl From<JsValue> for JsError {
fn from(value: JsValue) -> Self {
JsError(value)
}
}
#[cfg(test)]
mod tests {
use crate::{body, document, window};
use wasm_bindgen_test::{wasm_bindgen_test, wasm_bindgen_test_configure};
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn test_window() {
window();
}
#[wasm_bindgen_test]
fn test_body() {
body();
}
#[wasm_bindgen_test]
fn test_document() {
document();
}
}