Skip to content

Commit

Permalink
Add inaccurate time option
Browse files Browse the repository at this point in the history
For environments where `performance.now()` is not available such as
Cloudflare Workers.
  • Loading branch information
modestmicha authored and sebcrozet committed Nov 19, 2020
1 parent 3819881 commit c023a97
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ edition = "2018"
[features]
now = [ "time" ]
wasm-bindgen = ["js-sys", "wasm-bindgen_rs", "web-sys"]
inaccurate = []

[dependencies]
cfg-if = "1.0"
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@ fn main() {
}
```

-----

### Using `instant` for a WASM platform where `performance.now()` is not available.
This example shows the use of the `inaccurate` feature.

_Cargo.toml_:
```toml
[dependencies]
instant = { version = "0.1", features = [ "wasm-bindgen", "inaccurate" ] }
```

_main.rs_:
```rust
fn main() {
// Will emulate `std::time::Instant` based on `Date.now()`.
let now = instant::Instant::new();
}
```


-----

### Using `instant` for any platform enabling a feature transitively.
Expand Down
21 changes: 15 additions & 6 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,27 @@ pub fn now() -> f64 {
use stdweb::unstable::TryInto;

// https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
#[cfg(not(feature = "inaccurate"))]
let v = js! { return performance.now(); };
#[cfg(feature = "inaccurate")]
let v = js! { return Date.now(); };
v.try_into().unwrap()
}

#[cfg(feature = "wasm-bindgen")]
pub fn now() -> f64 {
use wasm_bindgen_rs::prelude::*;
use wasm_bindgen_rs::JsCast;
js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance"))
.expect("failed to get performance from global object")
.unchecked_into::<web_sys::Performance>()
.now()
#[cfg(not(feature = "inaccurate"))]
let now = {
use wasm_bindgen_rs::prelude::*;
use wasm_bindgen_rs::JsCast;
js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("performance"))
.expect("failed to get performance from global object")
.unchecked_into::<web_sys::Performance>()
.now()
};
#[cfg(feature = "inaccurate")]
let now = js_sys::Date::now();
now
}

// The JS now function is in a module so it won't have to be renamed
Expand Down
3 changes: 3 additions & 0 deletions tests/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn test_instant_now() {
let now = Instant::now();
#[cfg(feature = "inaccurate")]
while now.elapsed().as_millis() == 0 {}
#[cfg(not(feature = "inaccurate"))]
assert!(now.elapsed().as_nanos() > 0);
}

Expand Down

0 comments on commit c023a97

Please sign in to comment.