Skip to content

Commit

Permalink
when doc changes, always generate sync message, but skip saving if do…
Browse files Browse the repository at this point in the history
…c didn't change since last save
  • Loading branch information
gterzian authored and teohhanhui committed Nov 28, 2023
1 parent 84fade0 commit c136132
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
28 changes: 19 additions & 9 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,12 @@ impl DocumentInfo {
let count = {
let doc = self.document.read();
let changes = doc.automerge.get_changes(&self.last_heads);
tracing::trace!(
"last: {:?}, current: {:?}",
self.last_heads,
doc.automerge.get_heads()
);
//self.last_heads = doc.automerge.get_heads();
changes.len()
};
let has_patches = count > 0;
Expand Down Expand Up @@ -1245,16 +1251,16 @@ impl Repo {
// Handle doc changes: sync the document.
let local_repo_id = self.get_repo_id().clone();
if let Some(info) = self.documents.get_mut(&doc_id) {
if !info.note_changes() {
println!("Doc didn't change");
// Stop here if the document wasn't actually changed.
return;
// only run the documents_with_changes workflow if there
// was a change, but always generate potential sync messages
// (below)
if info.note_changes() {
self.documents_with_changes.push(doc_id.clone());
}
let is_first_edit = matches!(info.state, DocState::LocallyCreatedNotEdited);
if is_first_edit {
info.state = DocState::Sync(vec![]);
}
self.documents_with_changes.push(doc_id.clone());
for (to_repo_id, message) in info.generate_sync_messages().into_iter() {
let outgoing = NetworkMessage::Sync {
from_repo_id: local_repo_id.clone(),
Expand Down Expand Up @@ -1356,13 +1362,18 @@ impl Repo {
&self.repo_id,
);
}
RepoEvent::AddChangeObserver(doc_id, change_hash, mut observer) => {
RepoEvent::AddChangeObserver(doc_id, last_heads, mut observer) => {
if let Some(info) = self.documents.get_mut(&doc_id) {
let current_heads = {
let state = info.document.read();
state.automerge.get_heads()
};
if current_heads == change_hash {
tracing::trace!(
?current_heads,
?last_heads,
"handling AddChangeObserver event"
);
if current_heads == last_heads {
info.change_observers.push(observer);
} else {
// Resolve now if the document hash already changed.
Expand Down Expand Up @@ -1472,8 +1483,7 @@ impl Repo {
.get_mut(&document_id)
.expect("Doc should have an info by now.");

if info.receive_sync_message(per_remote) {
info.note_changes();
if info.receive_sync_message(per_remote) && info.note_changes() {
self.documents_with_changes.push(document_id.clone());
}

Expand Down
25 changes: 17 additions & 8 deletions tests/network/document_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,22 @@ async fn test_document_changed_over_sync() {
// Request the document.
let doc_handle = repo_handle_2.request_document(doc_id).await.unwrap();
doc_handle.with_doc_mut(|doc| {
let mut tx = doc.transaction();
tx.put(
automerge::ROOT,
"repo_id",
format!("{}", repo_handle_2.get_repo_id()),
)
.expect("Failed to change the document.");
tx.commit();
let val = doc
.get(automerge::ROOT, "repo_id")
.expect("Failed to read the document.")
.unwrap();
tracing::debug!(heads=?doc.get_heads(), ?val, "before repo_handle_2 makes edit");
{
let mut tx = doc.transaction();
tx.put(
automerge::ROOT,
"repo_id",
format!("{}", repo_handle_2.get_repo_id()),
)
.expect("Failed to change the document.");
tx.commit();
}
tracing::debug!(heads=?doc.get_heads(), "after repo_handle_2 makes edit");
});
});

Expand All @@ -64,6 +72,7 @@ async fn test_document_changed_over_sync() {
.get(automerge::ROOT, "repo_id")
.expect("Failed to read the document.")
.unwrap();
tracing::debug!(?val, "after repo_handle_1 received sync");
assert_eq!(val.0.to_str().unwrap(), "repo1".to_string());
});

Expand Down

0 comments on commit c136132

Please sign in to comment.