diff --git a/akaze/Cargo.toml b/akaze/Cargo.toml index 96a2733..e6751ff 100644 --- a/akaze/Cargo.toml +++ b/akaze/Cargo.toml @@ -31,7 +31,7 @@ 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 } +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 6456554..8433f14 100644 --- a/akaze/src/lib.rs +++ b/akaze/src/lib.rs @@ -27,11 +27,13 @@ impl Instant { fn now() -> Self { #[cfg(feature = "web-sys")] { - Self( - web_sys::window() - .and_then(|w| w.performance()) - .map_or(0., |p| p.now()), - ) + use web_sys::wasm_bindgen::JsCast; + let now = web_sys::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"))] { @@ -39,14 +41,7 @@ impl Instant { } } fn elapsed(&self) -> std::time::Duration { - #[cfg(feature = "web-sys")] - { - std::time::Duration::from_secs_f64((Self::now().0 - 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) } }