aboutsummaryrefslogtreecommitdiffstats
path: root/src/document
diff options
context:
space:
mode:
Diffstat (limited to 'src/document')
-rw-r--r--src/document/file.rs5
-rw-r--r--src/document/replica.rs36
2 files changed, 28 insertions, 13 deletions
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<usize> {
+ 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<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
+}