From f273b1a07e0ea78fb5b91d7916f596db899ab884 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 13:26:25 +0100 Subject: commits go through the replica --- src/document/replica.rs | 13 +++++++++++++ src/state/mod.rs | 17 ++++------------- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/document/replica.rs b/src/document/replica.rs index 8640c7e..5d3d7a7 100644 --- a/src/document/replica.rs +++ b/src/document/replica.rs @@ -167,6 +167,19 @@ impl Replica { pub(crate) fn stamps(&self) -> watch::Receiver { self.stamp.subscribe() } + + /// The writes since the last commit, as one change stamped `at`: receivers tell by + /// it how long the change took to reach them (`swarm::stats`). + pub(crate) fn commit(&mut self, at: crate::clock::UnixMillis) { + self.doc.commit_with( + automerge::transaction::CommitOptions::default().with_time(at.as_u64() as i64), + ); + } + + /// The writes since the last commit, discarded. + pub(crate) fn rollback(&mut self) { + self.doc.rollback(); + } } impl Deref for Replica { diff --git a/src/state/mod.rs b/src/state/mod.rs index efa4953..cf64dc3 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -413,7 +413,7 @@ impl AppState { .expect("document write task panicked") } - /// Mutates the document on the blocking pool as one Automerge change, then + /// Mutates the document on the blocking pool as one change, then /// publishes it. Write and notification run in the same blocking task, so a /// cancelled request can't leave a committed write unannounced. pub(crate) async fn mutate_doc( @@ -431,19 +431,10 @@ impl AppState { let mut replica = state.doc.blocking_write(); let result = f(&mut replica.doc); match &result { - // One API write = one Automerge change. - Ok(_) => { - // Stamped with our clock (milliseconds), so receivers can tell - // how long the change took to reach them (`swarm::stats`). - replica.doc.commit_with( - automerge::transaction::CommitOptions::default() - .with_time(state.clock.now_millis().as_u64() as i64), - ); - } + // One API write = one change. + Ok(_) => replica.commit(state.clock.now_millis()), // Discard partial ops (e.g. old value deleted, new one not written). - Err(_) => { - replica.doc.rollback(); - } + Err(_) => replica.rollback(), } replica.refresh_heads(); result -- cgit v1.3.1