diff --git a/akaze/Cargo.toml b/akaze/Cargo.toml index 96a2733..b1444a0 100644 --- a/akaze/Cargo.toml +++ b/akaze/Cargo.toml @@ -31,7 +31,9 @@ serde = { version = "1.0", default-features = false, features = ["derive"], opti rayon = { version = "1.7.0", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] -web-sys = { version = "0.3.64", features = ["Window", "Performance"], optional = true } +wasm-bindgen = "0.2.87" +js-sys = "0.3.64" +web-sys = { version = "0.3.64", features = ["Window", "Performance", "WorkerGlobalScope"], optional = true } [dev-dependencies] eight-point = { version = "0.8.0", path = "../eight-point" } diff --git a/akaze/src/lib.rs b/akaze/src/lib.rs index de9b104..61c0afd 100644 --- a/akaze/src/lib.rs +++ b/akaze/src/lib.rs @@ -27,7 +27,13 @@ impl Instant { fn now() -> Self { #[cfg(feature = "web-sys")] { - Self(web_sys::window().unwrap().performance().unwrap().now()) + use wasm_bindgen::cast::JsCast; + let now = js_sys::global() + .dyn_into::() + .and_then(|w| w.performance()) + .or_else(|| web_sys::window().and_then(|w| w.performance())) + .map_or(0., |p| p.now()); + Self(now) } #[cfg(not(feature = "web-sys"))] { @@ -35,16 +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, - ) - } - #[cfg(not(feature = "web-sys"))] - { - std::time::Duration::from_secs(0) - } + std::time::Duration::from_secs_f64((Self::now().0 - self.0) * 0.001) } }