Skip to content

Commit c136132

Browse files
gterzianteohhanhui
authored andcommitted
when doc changes, always generate sync message, but skip saving if doc didn't change since last save
1 parent 84fade0 commit c136132

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

src/repo.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,12 @@ impl DocumentInfo {
721721
let count = {
722722
let doc = self.document.read();
723723
let changes = doc.automerge.get_changes(&self.last_heads);
724+
tracing::trace!(
725+
"last: {:?}, current: {:?}",
726+
self.last_heads,
727+
doc.automerge.get_heads()
728+
);
729+
//self.last_heads = doc.automerge.get_heads();
724730
changes.len()
725731
};
726732
let has_patches = count > 0;
@@ -1245,16 +1251,16 @@ impl Repo {
12451251
// Handle doc changes: sync the document.
12461252
let local_repo_id = self.get_repo_id().clone();
12471253
if let Some(info) = self.documents.get_mut(&doc_id) {
1248-
if !info.note_changes() {
1249-
println!("Doc didn't change");
1250-
// Stop here if the document wasn't actually changed.
1251-
return;
1254+
// only run the documents_with_changes workflow if there
1255+
// was a change, but always generate potential sync messages
1256+
// (below)
1257+
if info.note_changes() {
1258+
self.documents_with_changes.push(doc_id.clone());
12521259
}
12531260
let is_first_edit = matches!(info.state, DocState::LocallyCreatedNotEdited);
12541261
if is_first_edit {
12551262
info.state = DocState::Sync(vec![]);
12561263
}
1257-
self.documents_with_changes.push(doc_id.clone());
12581264
for (to_repo_id, message) in info.generate_sync_messages().into_iter() {
12591265
let outgoing = NetworkMessage::Sync {
12601266
from_repo_id: local_repo_id.clone(),
@@ -1356,13 +1362,18 @@ impl Repo {
13561362
&self.repo_id,
13571363
);
13581364
}
1359-
RepoEvent::AddChangeObserver(doc_id, change_hash, mut observer) => {
1365+
RepoEvent::AddChangeObserver(doc_id, last_heads, mut observer) => {
13601366
if let Some(info) = self.documents.get_mut(&doc_id) {
13611367
let current_heads = {
13621368
let state = info.document.read();
13631369
state.automerge.get_heads()
13641370
};
1365-
if current_heads == change_hash {
1371+
tracing::trace!(
1372+
?current_heads,
1373+
?last_heads,
1374+
"handling AddChangeObserver event"
1375+
);
1376+
if current_heads == last_heads {
13661377
info.change_observers.push(observer);
13671378
} else {
13681379
// Resolve now if the document hash already changed.
@@ -1472,8 +1483,7 @@ impl Repo {
14721483
.get_mut(&document_id)
14731484
.expect("Doc should have an info by now.");
14741485

1475-
if info.receive_sync_message(per_remote) {
1476-
info.note_changes();
1486+
if info.receive_sync_message(per_remote) && info.note_changes() {
14771487
self.documents_with_changes.push(document_id.clone());
14781488
}
14791489

tests/network/document_changed.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ async fn test_document_changed_over_sync() {
3131
// Request the document.
3232
let doc_handle = repo_handle_2.request_document(doc_id).await.unwrap();
3333
doc_handle.with_doc_mut(|doc| {
34-
let mut tx = doc.transaction();
35-
tx.put(
36-
automerge::ROOT,
37-
"repo_id",
38-
format!("{}", repo_handle_2.get_repo_id()),
39-
)
40-
.expect("Failed to change the document.");
41-
tx.commit();
34+
let val = doc
35+
.get(automerge::ROOT, "repo_id")
36+
.expect("Failed to read the document.")
37+
.unwrap();
38+
tracing::debug!(heads=?doc.get_heads(), ?val, "before repo_handle_2 makes edit");
39+
{
40+
let mut tx = doc.transaction();
41+
tx.put(
42+
automerge::ROOT,
43+
"repo_id",
44+
format!("{}", repo_handle_2.get_repo_id()),
45+
)
46+
.expect("Failed to change the document.");
47+
tx.commit();
48+
}
49+
tracing::debug!(heads=?doc.get_heads(), "after repo_handle_2 makes edit");
4250
});
4351
});
4452

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

0 commit comments

Comments
 (0)