From 1f3be9d059876f7f3994056b1ac8ce307628a52a Mon Sep 17 00:00:00 2001 From: tv Date: Sun, 1 Nov 2026 19:35:20 +0100 Subject: document: allow writes during rejoin --- src/document/replica.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'src/document/replica.rs') diff --git a/src/document/replica.rs b/src/document/replica.rs index 61f2b3b..df2fafd 100644 --- a/src/document/replica.rs +++ b/src/document/replica.rs @@ -182,19 +182,7 @@ impl Replica { at: crate::clock::UnixMillis, f: impl FnOnce(&mut Transaction<'_>) -> Result, ) -> Result { - let mut transaction = self.doc.transaction(); - let out = f(&mut transaction); - match &out { - Ok(_) => { - transaction.commit_with( - automerge::transaction::CommitOptions::default().with_time(at.as_u64() as i64), - ); - } - Err(_) => { - transaction.rollback(); - } - } - out + transact(&mut self.doc, at, f) } } @@ -272,3 +260,25 @@ mod tests { assert_eq!(epoch(2, 0xab).to_string(), "2:abababababab"); } } + +/// The writes `f` makes to `doc`, as one change stamped `at` if it succeeds, dropped if +/// not ([`Replica::transact`], and writes into the document before a rejoin). +pub(crate) fn transact( + doc: &mut Document, + at: crate::clock::UnixMillis, + f: impl FnOnce(&mut Transaction<'_>) -> Result, +) -> Result { + let mut transaction = doc.transaction(); + let out = f(&mut transaction); + match &out { + Ok(_) => { + transaction.commit_with( + automerge::transaction::CommitOptions::default().with_time(at.as_u64() as i64), + ); + } + Err(_) => { + transaction.rollback(); + } + } + out +} -- cgit v1.3.1