-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
32 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,38 @@ | ||
use web_time::Instant; | ||
use lazy_static::lazy_static; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
mod platform_time { | ||
use wasm_bindgen::prelude::*; | ||
|
||
#[wasm_bindgen] | ||
extern "C" { | ||
#[wasm_bindgen(js_namespace = performance)] | ||
fn now() -> f64; | ||
} | ||
|
||
pub fn now() -> f64 { | ||
now() / 1000.0 // Convert milliseconds to seconds | ||
} | ||
} | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
mod platform_time { | ||
use lazy_static::lazy_static; | ||
use std::time::Instant; | ||
|
||
lazy_static! { | ||
static ref TIME_AT_START: Instant = Instant::now(); | ||
} | ||
|
||
pub fn now() -> f64 { | ||
TIME_AT_START.elapsed().as_secs_f64() | ||
} | ||
} | ||
|
||
lazy_static! { | ||
static ref TIME_AT_START: Instant = Instant::now(); | ||
static ref TIME_AT_START: f64 = platform_time::now(); | ||
} | ||
|
||
pub fn now() -> f32 { | ||
Instant::now().duration_since(*TIME_AT_START).as_secs_f64() as f32 | ||
} | ||
(platform_time::now() - *TIME_AT_START) as f32 | ||
} |