From 5640bacf735c6728acf4bc22217b0c07ac4bf7f9 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/file.rs | 5 +++++ src/document/replica.rs | 36 +++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 13 deletions(-) (limited to 'src/document') diff --git a/src/document/file.rs b/src/document/file.rs index 9efae42..3ea3afd 100644 --- a/src/document/file.rs +++ b/src/document/file.rs @@ -197,6 +197,11 @@ impl DocumentFile { /// What is due for a document of `epoch` at `heads`, written by this node's `actors` /// (see the module docs). + /// How many of this node's actors the file holds; None before it was written. + pub(crate) fn actors(&self) -> Option { + self.lock().as_ref().map(|written| written.actors) + } + pub(crate) fn due(&self, epoch: Epoch, actors: usize, heads: &[ChangeHash]) -> Due { let written = self.lock().clone(); let reason = match &written { 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