Skip to content

Commit 82a25ee

Browse files
[Akaze] Do not crash in web workers when querying performance (#76)
1 parent 8575691 commit 82a25ee

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

akaze/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ impl Instant {
2727
fn now() -> Self {
2828
#[cfg(feature = "web-sys")]
2929
{
30-
Self(web_sys::window().unwrap().performance().unwrap().now())
30+
Self(
31+
web_sys::window()
32+
.and_then(|w| w.performance())
33+
.map_or(0., |p| p.now()),
34+
)
3135
}
3236
#[cfg(not(feature = "web-sys"))]
3337
{
@@ -37,9 +41,7 @@ impl Instant {
3741
fn elapsed(&self) -> std::time::Duration {
3842
#[cfg(feature = "web-sys")]
3943
{
40-
std::time::Duration::from_secs_f64(
41-
(web_sys::window().unwrap().performance().unwrap().now() - self.0) * 0.001,
42-
)
44+
std::time::Duration::from_secs_f64((Self::now().0 - self.0) * 0.001)
4345
}
4446
#[cfg(not(feature = "web-sys"))]
4547
{

0 commit comments

Comments
 (0)