lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

clock.rs (902B)


      1 //! The pinned SDK requires exactly one clock selection in the final WASM
      2 //! artifact. This feature is selected only by the final artifact root. Use real
      3 //! browser clocks in both pages and workers; never substitute a test timestamp.
      4 
      5 use std::time::Duration;
      6 use universal_time::{Instant, MonotonicClock, SystemTime, WallClock};
      7 use wasm_bindgen::prelude::*;
      8 
      9 #[wasm_bindgen]
     10 extern "C" {
     11     #[wasm_bindgen(js_namespace = performance, js_name = now)]
     12     fn monotonic_ms() -> f64;
     13 }
     14 
     15 struct BrowserClock;
     16 impl WallClock for BrowserClock {
     17     fn system_time(&self) -> SystemTime {
     18         SystemTime::from_unix_duration(Duration::from_secs_f64(js_sys::Date::now() / 1_000.0))
     19     }
     20 }
     21 impl MonotonicClock for BrowserClock {
     22     fn instant(&self) -> Instant {
     23         Instant::from_ticks(Duration::from_secs_f64(monotonic_ms() / 1_000.0))
     24     }
     25 }
     26 universal_time::define_time_provider!(BrowserClock);