Skip to content

Commit

Permalink
Add note to lacking worklet support
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jan 29, 2024
1 parent e682b09 commit 848ca47
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .config/topic.dic
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
17
18
1G
1M
1ns
Expand All @@ -16,3 +16,4 @@ v1
Versioning
WASI
Wasm
worklet
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ 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
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:
Expand Down Expand Up @@ -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
Expand All @@ -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
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
//!
//! # 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
//! 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:
Expand Down Expand Up @@ -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
Expand All @@ -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))]
Expand Down
8 changes: 8 additions & 0 deletions src/time/instant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down

0 comments on commit 848ca47

Please sign in to comment.