From c52d4b5ae43c202569c8801c312c2b56954ff8a0 Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 25 Oct 2026 23:00:56 +0100 Subject: add clock module The Clock module is the system clock that every unix timestamp new goes through. It comes with an offset that only tests can set. --- src/state.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/state.rs') diff --git a/src/state.rs b/src/state.rs index 78b058c..46ebfb9 100644 --- a/src/state.rs +++ b/src/state.rs @@ -676,6 +676,8 @@ pub(crate) struct AppState { /// A catch-up source given up for not answering our sync messages, and until when it /// isn't chosen again (`swarm::source`). pub(crate) catch_up_avoided: Arc>>, + /// The node's wall clock (`clock`). + pub(crate) clock: Arc, } /// The peer a catching-up node syncs with first (`swarm::source`). @@ -913,7 +915,7 @@ impl AppState { // how long the change took to reach them (`swarm::stats`). replica.doc.commit_with( automerge::transaction::CommitOptions::default() - .with_time(unix_millis() as i64), + .with_time(state.clock.now_millis() as i64), ); } // Discard partial ops (e.g. old value deleted, new one not written). @@ -1087,19 +1089,16 @@ impl AppState { } } -pub(crate) fn unix_millis() -> u64 { - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_millis() as u64) - .unwrap_or(0) -} - /// The node as seen by the remake module. impl Host for AppState { fn remake(&self) -> &RemakeState { &self.remake } + fn clock(&self) -> Arc { + Arc::clone(&self.clock) + } + fn node_id(&self) -> String { self.local_id().to_owned() } -- cgit v1.3.1