Skip to content

Commit dc3d23c

Browse files
committed
when doc changes, always generate sync message, but skip saving if doc didn't change since last save
1 parent b55cf0f commit dc3d23c

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/repo.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ impl DocumentInfo {
704704
let count = {
705705
let doc = self.document.read();
706706
let changes = doc.automerge.get_changes(&self.last_heads);
707+
println!("last: {:?}, current: {:?}", self.last_heads, doc.automerge.get_heads());
708+
//self.last_heads = doc.automerge.get_heads();
707709
changes.len()
708710
};
709711
let has_patches = count > 0;
@@ -1231,16 +1233,13 @@ impl Repo {
12311233
// Handle doc changes: sync the document.
12321234
let local_repo_id = self.get_repo_id().clone();
12331235
if let Some(info) = self.documents.get_mut(&doc_id) {
1234-
if !info.note_changes() {
1235-
println!("Doc didn't change");
1236-
// Stop here if the document wasn't actually changed.
1237-
return;
1236+
if info.note_changes() {
1237+
self.documents_with_changes.push(doc_id.clone());
12381238
}
12391239
let is_first_edit = matches!(info.state, DocState::LocallyCreatedNotEdited);
12401240
if is_first_edit {
12411241
info.state = DocState::Sync(vec![]);
12421242
}
1243-
self.documents_with_changes.push(doc_id.clone());
12441243
for (to_repo_id, message) in info.generate_sync_messages().into_iter() {
12451244
let outgoing = NetworkMessage::Sync {
12461245
from_repo_id: local_repo_id.clone(),
@@ -1255,6 +1254,7 @@ impl Repo {
12551254
self.sinks_to_poll.insert(to_repo_id);
12561255
}
12571256
if is_first_edit {
1257+
println!("First edit");
12581258
// Send a sync message to all other repos we are connected with.
12591259
for repo_id in self.remote_repos.keys() {
12601260
if let Some(message) = info.generate_first_sync_message(repo_id.clone())
@@ -1348,6 +1348,7 @@ impl Repo {
13481348
let state = info.document.read();
13491349
state.automerge.get_heads()
13501350
};
1351+
println!("Change observer: {:?} {:?}", current_heads, change_hash);
13511352
if current_heads == change_hash {
13521353
info.change_observers.push(observer);
13531354
} else {
@@ -1473,8 +1474,9 @@ impl Repo {
14731474
.expect("Doc should have an info by now.");
14741475

14751476
if info.receive_sync_message(per_remote) {
1476-
info.note_changes();
1477-
self.documents_with_changes.push(document_id.clone());
1477+
if info.note_changes() {
1478+
self.documents_with_changes.push(document_id.clone());
1479+
}
14781480
}
14791481

14801482
// Note: since receiving and generating sync messages is done

tests/document_changed.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,21 @@ fn test_document_changed_over_sync() {
6161
// Request the document.
6262
let doc_handle = repo_handle_2.request_document(doc_id).await.unwrap();
6363
doc_handle.with_doc_mut(|doc| {
64+
println!("Heads when 2 makes edit: {:?}", doc.get_heads());
6465
let id = doc
6566
.get(automerge::ROOT, "repo_id")
6667
.expect("Failed to read the document.")
6768
.unwrap();
68-
let mut tx = doc.transaction();
69+
println!("Id when two makes edit: {:?}", id);
70+
{let mut tx = doc.transaction();
6971
tx.put(
7072
automerge::ROOT,
7173
"repo_id",
7274
format!("{}", repo_handle_2.get_repo_id()),
7375
)
7476
.expect("Failed to change the document.");
75-
tx.commit();
77+
tx.commit();}
78+
println!("Heads after 2 makes edit: {:?}", doc.get_heads());
7679
});
7780
});
7881

@@ -87,7 +90,7 @@ fn test_document_changed_over_sync() {
8790
.get(automerge::ROOT, "repo_id")
8891
.expect("Failed to read the document.")
8992
.unwrap();
90-
println!("Val: {:?}", val.0.to_str().unwrap());
93+
println!("Val: {:?}", val);
9194
val.0.to_str().unwrap() == format!("{}", expected_repo_id)
9295
});
9396
if equals {

0 commit comments

Comments
 (0)