Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WASM polyfilled Instant work in web workers. #75

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading