aboutsummaryrefslogtreecommitdiffstats
path: root/src/state/mod.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-31 19:53:27 +0100
committertv <tv@krebsco.de>2026-10-31 19:53:27 +0100
commit9c12bf2e8815dc9b42be62f1e1fd2932dd02938b (patch)
tree09cd985fae9142a247a2285c755f9d002a2c7b30 /src/state/mod.rs
parentf4b0a7c5159d418e22682d2bd7d413fb3587ca7f (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.rs14
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