Skip to content

Commit 8bb15fe

Browse files
Make WASM polyfilled Instant work in web workers.
1 parent 82a25ee commit 8bb15fe

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

akaze/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ serde = { version = "1.0", default-features = false, features = ["derive"], opti
3131
rayon = { version = "1.7.0", optional = true }
3232

3333
[target.'cfg(target_arch = "wasm32")'.dependencies]
34-
web-sys = { version = "0.3.64", features = ["Window", "Performance"], optional = true }
34+
web-sys = { version = "0.3.64", features = ["Window", "Performance", "WorkerGlobalScope"], optional = true }
3535

3636
[dev-dependencies]
3737
eight-point = { version = "0.8.0", path = "../eight-point" }

akaze/src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,21 @@ impl Instant {
2727
fn now() -> Self {
2828
#[cfg(feature = "web-sys")]
2929
{
30-
Self(
31-
web_sys::window()
32-
.and_then(|w| w.performance())
33-
.map_or(0., |p| p.now()),
34-
)
30+
use web_sys::wasm_bindgen::JsCast;
31+
let now = web_sys::js_sys::global()
32+
.dyn_into::<web_sys::WorkerGlobalScope>()
33+
.and_then(|w| w.performance())
34+
.or_else(|_| web_sys::window().and_then(|w| w.performance()))
35+
.map_or(0., |p| p.now());
36+
Self(now)
3537
}
3638
#[cfg(not(feature = "web-sys"))]
3739
{
3840
Self(0.)
3941
}
4042
}
4143
fn elapsed(&self) -> std::time::Duration {
42-
#[cfg(feature = "web-sys")]
43-
{
44-
std::time::Duration::from_secs_f64((Self::now().0 - self.0) * 0.001)
45-
}
46-
#[cfg(not(feature = "web-sys"))]
47-
{
48-
std::time::Duration::from_secs(0)
49-
}
44+
std::time::Duration::from_secs_f64((Self::now().0 - self.0) * 0.001)
5045
}
5146
}
5247

0 commit comments

Comments
 (0)