From 82a25ee3a88c1200274182951ccd7dfeae4708d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Magnenat?= Date: Thu, 20 Jul 2023 14:23:27 +0200 Subject: [PATCH] [Akaze] Do not crash in web workers when querying performance (#76) --- akaze/src/lib.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/akaze/src/lib.rs b/akaze/src/lib.rs index de9b1043..6456554d 100644 --- a/akaze/src/lib.rs +++ b/akaze/src/lib.rs @@ -27,7 +27,11 @@ impl Instant { fn now() -> Self { #[cfg(feature = "web-sys")] { - Self(web_sys::window().unwrap().performance().unwrap().now()) + Self( + web_sys::window() + .and_then(|w| w.performance()) + .map_or(0., |p| p.now()), + ) } #[cfg(not(feature = "web-sys"))] { @@ -37,9 +41,7 @@ impl Instant { fn elapsed(&self) -> std::time::Duration { #[cfg(feature = "web-sys")] { - std::time::Duration::from_secs_f64( - (web_sys::window().unwrap().performance().unwrap().now() - self.0) * 0.001, - ) + std::time::Duration::from_secs_f64((Self::now().0 - self.0) * 0.001) } #[cfg(not(feature = "web-sys"))] {