use ::turso::core::{Clock, MonotonicInstant, WallClockInstant};
pub(crate) struct WebClock;
impl Clock for WebClock {
fn current_time_monotonic(&self) -> MonotonicInstant {
MonotonicInstant::from_nanos(monotonic_nanos())
}
fn current_time_wall_clock(&self) -> WallClockInstant {
wall_clock_now()
}
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
fn performance_millis() -> Option<f64> {
use wasm_bindgen::{JsCast, JsValue};
let global = js_sys::global();
let performance = js_sys::Reflect::get(&global, &JsValue::from_str("performance")).ok()?;
let now = js_sys::Reflect::get(&performance, &JsValue::from_str("now")).ok()?;
let now: js_sys::Function = now.dyn_into().ok()?;
now.call0(&performance).ok()?.as_f64()
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
fn monotonic_nanos() -> u128 {
let millis = performance_millis().unwrap_or_else(js_sys::Date::now);
(millis * 1_000_000.0) as u128
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn monotonic_nanos() -> u128 {
use std::sync::OnceLock;
use std::time::Instant;
static EPOCH: OnceLock<Instant> = OnceLock::new();
EPOCH.get_or_init(Instant::now).elapsed().as_nanos()
}
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
fn wall_clock_now() -> WallClockInstant {
let millis = js_sys::Date::now();
let secs = (millis / 1000.0).floor();
let micros = ((millis - secs * 1000.0) * 1000.0).round() as u32;
WallClockInstant {
secs: secs as i64,
micros: micros.min(999_999),
}
}
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
fn wall_clock_now() -> WallClockInstant {
use std::time::{SystemTime, UNIX_EPOCH};
let since_epoch = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default();
WallClockInstant {
secs: since_epoch.as_secs() as i64,
micros: since_epoch.subsec_micros(),
}
}