Skip to content

Commit

Permalink
Make WASM polyfilled Instant work in web workers.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanemagnenat committed Jul 20, 2023
1 parent 82a25ee commit 8bb15fe
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion akaze/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
21 changes: 8 additions & 13 deletions akaze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,21 @@ 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::<web_sys::WorkerGlobalScope>()
.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"))]
{
Self(0.)
}
}
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)
}
}

Expand Down

0 comments on commit 8bb15fe

Please sign in to comment.