diff --git a/.config/topic.dic b/.config/topic.dic index 4a27829..a817fe5 100644 --- a/.config/topic.dic +++ b/.config/topic.dic @@ -1,4 +1,4 @@ -17 +18 1G 1M 1ns @@ -16,3 +16,4 @@ v1 Versioning WASI Wasm +worklet diff --git a/README.md b/README.md index 0d04cbd..17e2342 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ which case multiple ecosystems could be supported. ## Note +### Ticking during sleep + Currently a known bug is affecting browsers on operating system other then Windows. This bug prevents [`Instant`] from continuing to tick when the context is asleep. This doesn't necessarily conflict with Rusts requirements @@ -47,6 +49,12 @@ of [`Instant`], but might still be unexpected. See [the MDN documentation on this](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#ticking_during_sleep) for more information. +### Context support + +The implementation of [`Instant::now()`] relies on the availability of the +[`Performance` object], a lack thereof will cause a panic. This can happen +if called from a [worklet]. + ## Usage You can simply import the types you need: @@ -105,6 +113,7 @@ conditions. [CONTRIBUTING]: https://github.com/daxpedda/web-time/blob/v1.0.0/CONTRIBUTING.md [LICENSE-MIT]: https://github.com/daxpedda/web-time/blob/v1.0.0/LICENSE-MIT [LICENSE-APACHE]: https://github.com/daxpedda/web-time/blob/v1.0.0/LICENSE-APACHE +[worklet]: https://developer.mozilla.org/en-US/docs/Web/API/Worklet [`Date.now()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now [`Instant`]: https://doc.rust-lang.org/std/time/struct.Instant.html [`Instant::now()`]: https://doc.rust-lang.org/std/time/struct.Instant.html#method.now @@ -113,4 +122,5 @@ conditions. [`std::time`]: https://doc.rust-lang.org/stable/std/time/ [`performance.now()`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now [`Performance.timeOrigin`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin +[`Performance` object]: https://developer.mozilla.org/en-US/docs/Web/API/performance_property [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen diff --git a/src/lib.rs b/src/lib.rs index 81d3b36..f696328 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,6 +32,8 @@ //! //! # Note //! +//! ## Ticking during sleep +//! //! Currently a known bug is affecting browsers on operating system other then //! Windows. This bug prevents [`Instant`] from continuing to tick when the //! context is asleep. This doesn't necessarily conflict with Rusts requirements @@ -39,6 +41,12 @@ //! //! See [the MDN documentation on this](https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#ticking_during_sleep) for more information. //! +//! ## Context support +//! +//! The implementation of [`Instant::now()`] relies on the availability of the +//! [`Performance` object], a lack thereof will cause a panic. This can happen +//! if called from a [worklet]. +//! //! # Usage //! //! You can simply import the types you need: @@ -97,6 +105,7 @@ //! [CONTRIBUTING]: https://github.com/daxpedda/web-time/blob/v1.0.0/CONTRIBUTING.md //! [LICENSE-MIT]: https://github.com/daxpedda/web-time/blob/v1.0.0/LICENSE-MIT //! [LICENSE-APACHE]: https://github.com/daxpedda/web-time/blob/v1.0.0/LICENSE-APACHE +//! [worklet]: https://developer.mozilla.org/en-US/docs/Web/API/Worklet //! [`Date.now()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now //! [`Instant`]: https://doc.rust-lang.org/std/time/struct.Instant.html //! [`Instant::now()`]: https://doc.rust-lang.org/std/time/struct.Instant.html#method.now @@ -105,6 +114,7 @@ //! [`std::time`]: https://doc.rust-lang.org/stable/std/time/ //! [`performance.now()`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now //! [`Performance.timeOrigin`]: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin +//! [`Performance` object]: https://developer.mozilla.org/en-US/docs/Web/API/performance_property //! [`wasm-bindgen`]: https://crates.io/crates/wasm-bindgen #![cfg_attr(docsrs, feature(doc_cfg))] diff --git a/src/time/instant.rs b/src/time/instant.rs index 865ae03..0d4be9a 100644 --- a/src/time/instant.rs +++ b/src/time/instant.rs @@ -16,6 +16,14 @@ pub struct Instant(Duration); impl Instant { /// See [`std::time::Instant::now()`]. + /// + /// # Panics + /// + /// This call will panic if the [`Performance` object] was not found, e.g. + /// calling from a [worklet]. + /// + /// [`Performance` object]: https://developer.mozilla.org/en-US/docs/Web/API/performance_property + /// [worklet]: https://developer.mozilla.org/en-US/docs/Web/API/Worklet #[must_use] pub fn now() -> Self { let now = PERFORMANCE.with(|performance| {