aboutsummaryrefslogtreecommitdiffstats
path: root/src/document/store.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/document/store.rs')
-rw-r--r--src/document/store.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/document/store.rs b/src/document/store.rs
index 8b8894c..9afb5d3 100644
--- a/src/document/store.rs
+++ b/src/document/store.rs
@@ -258,6 +258,27 @@ fn signs_values(doc: &impl ReadDoc) -> bool {
})
}
+/// The change that wrote the value a read of `path` shows (`candidates`), if `doc` holds
+/// one there.
+pub(crate) fn written_by(doc: &Automerge, path: &[String]) -> Option<automerge::ChangeHash> {
+ let (leaf, parents) = path.split_last()?;
+ let Some(Resolved::Maps(maps)) = resolve_through(doc, parents).ok()? else {
+ return None;
+ };
+ let (_, id) = candidates(doc, &maps, leaf.as_str())
+ .ok()?
+ .into_iter()
+ .next()?;
+ let ObjId::Id(counter, actor, _) = id else {
+ return None;
+ };
+ doc.get_changes(&[]).into_iter().find_map(|change| {
+ let start = change.start_op().get();
+ (change.actor_id() == &actor && (start..start + change.len() as u64).contains(&counter))
+ .then(|| change.hash())
+ })
+}
+
/// What `prop` of `parent` holds as a read shows it, of the values written there at
/// once (`candidates`): the value, and its operation's ID.
pub(crate) fn shown(