diff options
Diffstat (limited to 'src/document/replica.rs')
| -rw-r--r-- | src/document/replica.rs | 36 |
1 files changed, 23 insertions, 13 deletions
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<T, E>, ) -> Result<T, E> { - 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<T, E>( + doc: &mut Document, + at: crate::clock::UnixMillis, + f: impl FnOnce(&mut Transaction<'_>) -> Result<T, E>, +) -> Result<T, E> { + 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 +} |
