From 9c12bf2e8815dc9b42be62f1e1fd2932dd02938b Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 19:53:27 +0100 Subject: use Automerge as Document instead of AutoCommit This allows a lot of operations to drop mut. --- src/state/mod.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/state/mod.rs') diff --git a/src/state/mod.rs b/src/state/mod.rs index 75c3780..d8bdac5 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -36,10 +36,10 @@ use tokio::sync::{Mutex, RwLock, broadcast, mpsc, watch}; use tokio::time::Instant; use tracing::{debug, error, warn}; -use crate::document::Document; use crate::document::lock::DocumentLock; use crate::document::replica::{Epoch, Stamp}; use crate::document::store::ChangeSet; +use crate::document::{Document, Transaction}; use crate::locks::MutexExt; use crate::remake::{Host, RemakeState}; use crate::swarm::sync::SyncEvent; @@ -396,7 +396,7 @@ impl AppState { /// cancelled request can't leave a committed write unannounced. pub(crate) async fn mutate_doc( &self, - f: impl FnOnce(&mut Document) -> Result + Send + 'static, + f: impl FnOnce(&mut Transaction<'_>) -> Result + Send + 'static, ) -> Result where T: Send + 'static, @@ -407,13 +407,9 @@ impl AppState { tokio::task::spawn_blocking(move || { let result = { let mut replica = state.doc.blocking_write(); - let result = f(&mut replica.doc); - match &result { - // 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.rollback(), - } + // One API write = one change; on error, its partial writes (e.g. the old + // value deleted, the new one not written) are dropped. + let result = replica.transact(state.clock.now_millis(), f); replica.refresh_heads(); result }; // lock released before notifying -- cgit v1.3.1