diff options
| author | tv <tv@krebsco.de> | 2026-10-31 19:53:27 +0100 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-10-31 19:53:27 +0100 |
| commit | 6105507200231e67ba544c12aa10b988e1c23826 (patch) | |
| tree | 880530b7401cb5d43f52e06a68b4d704326aa272 /src/state/mod.rs | |
| parent | 8d7adfbdb7798cca9d9783761f290e3bd85d9a3a (diff) | |
use Automerge as Document instead of AutoCommit
This allows a lot of operations to drop mut.
Diffstat (limited to 'src/state/mod.rs')
| -rw-r--r-- | src/state/mod.rs | 14 |
1 files changed, 5 insertions, 9 deletions
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<T, E>( &self, - f: impl FnOnce(&mut Document) -> Result<T, E> + Send + 'static, + f: impl FnOnce(&mut Transaction<'_>) -> Result<T, E> + Send + 'static, ) -> Result<T, E> 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 |
