feat: some additional js_lib stuff for logging

This commit is contained in:
Jeremy Wall 2023-11-25 09:58:00 -05:00
parent 50eecf9a7c
commit a3aa579fa5

View File

@ -12,13 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use js_sys::Date;
use tracing::error;
use wasm_bindgen::JsCast;
use web_sys::{window, Element, Storage};
use web_sys::{window, Element, Storage, Window};
pub fn get_storage() -> Storage {
window()
.expect("No Window Present")
.local_storage()
get_window().local_storage()
.expect("Failed to get storage")
.expect("No storage available")
}
@ -27,13 +26,16 @@ pub fn get_ms_timestamp() -> u32 {
Date::new_0().get_milliseconds()
}
pub fn get_window() -> Window {
window()
.expect("No window present")
}
pub fn get_element_by_id<E>(id: &str) -> Result<Option<E>, Element>
where
E: JsCast,
{
match window()
.expect("No window present")
.document()
match get_window().document()
.expect("No document in window")
.get_element_by_id(id)
{
@ -41,3 +43,18 @@ where
None => Ok(None),
}
}
pub trait LogFailures<V, E> {
fn swallow_and_log(self);
}
impl<E> LogFailures<(), E> for Result<(), E>
where
E: std::fmt::Debug,
{
fn swallow_and_log(self) {
if let Err(e) = self {
error!(err = ?e, "Error: ");
}
}
}