// Copyright 2021 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 use wasm_bindgen::prelude::*; use wasm_bindgen::JsValue; #[cfg(feature = "websocket")] pub mod websocket; #[cfg(feature = "crypto")] pub mod crypto; #[cfg(feature = "storage")] pub mod storage; // will cause messages to be written as if console.log("...") was called #[macro_export] macro_rules! console_log { ($($t:tt)*) => ($crate::log(&format_args!($($t)*).to_string())) } // will cause messages to be written as if console.warn("...") was called #[macro_export] macro_rules! console_warn { ($($t:tt)*) => ($crate::warn(&format_args!($($t)*).to_string())) } // will cause messages to be written as if console.error("...") was called #[macro_export] macro_rules! console_error { ($($t:tt)*) => ($crate::error(&format_args!($($t)*).to_string())) } #[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); } #[cfg(feature = "sleep")] pub async fn sleep(ms: i32) -> Result<(), JsValue> { let promise = js_sys::Promise::new(&mut |yes, _| { let win = web_sys::window().expect("no window available!"); win.set_timeout_with_callback_and_timeout_and_arguments_0(&yes, ms) .unwrap(); }); let js_fut = wasm_bindgen_futures::JsFuture::from(promise); js_fut.await?; Ok(()) } /// A helper that construct a `JsValue` containing an error with the provided message. pub fn simple_js_error>(message: S) -> JsValue { let js_error = js_sys::Error::new(message.as_ref()); JsValue::from(js_error) } #[macro_export] macro_rules! js_error { ($($t:tt)*) => {{ let js_error = js_sys::Error::new(&format!($($t)*)); wasm_bindgen::JsValue::from(js_error) }} } /// Maps provided `Result`'s inner values into a pair of `JsValue` that can be returned /// inside a promise (and in particular from inside `future_to_promise`) pub fn into_promise_result(res: Result) -> Result where T: Into, E: Into, { res.map(Into::into).map_err(Into::into) } pub fn map_promise_err(res: Result) -> Result where E: Into, { res.map_err(Into::into) } pub trait PromisableResult { fn into_promise_result(self) -> Result; } // this should probably get renamed : ) pub trait PromisableResultError { type Ok; fn map_promise_err(self) -> Result; } impl PromisableResult for Result where T: Into, E: Into, { fn into_promise_result(self) -> Result { into_promise_result(self) } } impl PromisableResultError for Result where E: Into, { type Ok = T; fn map_promise_err(self) -> Result { map_promise_err(self) } } #[macro_export] macro_rules! check_promise_result { ( $x:expr ) => { match $crate::PromisableResultError::map_promise_err($x) { Ok(r) => r, Err(err) => return js_sys::Promise::reject(&err), } }; }