Skip to content

Commit 80b9d73

Browse files
fix: correctly sync initially untracked local documents
Addresses the issue where newly created files, or existing files that were opened for the first time while sync was running, did not propagate their initial state to the peer correctly. - The `update_doc_from_file` and `load_or_create_doc` routines now detect and load the initial plaintext directly into the Automerge `ObjType::Text` before emitting the first sync payload. - Fixed the receiver side to automatically construct parent directories and a dummy file stub when receiving a payload for a completely unknown path before attempting to apply the CRDT data. Co-authored-by: Keshav-writes-code <95571677+Keshav-writes-code@users.noreply.github.com>
1 parent 412cf51 commit 80b9d73

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

apps/app/src-tauri/src/sync/commands.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,14 @@ pub async fn sync_file(state: State<'_, AppSyncState>, file_path: String) -> Res
114114
// First ensure the file is loaded into the CRDT manager so we have an active tracking state
115115
// This is necessary if it's the very first time the file is being synced.
116116
if !crdt_manager.documents.contains_key(&relative_path) {
117-
let _ = crdt_manager.load_or_create_doc(&relative_path).await;
117+
if let Err(e) = crdt_manager.load_or_create_doc(&relative_path).await {
118+
eprintln!("Failed to initialize doc for sync: {}", e);
119+
}
118120
}
119121

120-
let _ = crdt_manager.update_doc_from_file(&relative_path).await;
122+
if let Err(e) = crdt_manager.update_doc_from_file(&relative_path).await {
123+
eprintln!("Failed to update doc for sync: {}", e);
124+
}
121125

122126
// Fetch the raw bytes of the automerge CRDT state to send
123127
let payload_content = match crdt_manager.documents.get(&relative_path) {

apps/app/src-tauri/src/sync/server.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,12 @@ async fn sync_handler(
131131

132132
let mut crdt_manager = state.sync.crdt_manager.write().await;
133133

134-
// First ensure we have tracking for this file initialized on this end too
134+
// Wait for file creation / tracking initialization before applying merge
135+
// otherwise the new file merge has no context.
135136
if !crdt_manager.documents.contains_key(&relative_path) {
136-
let _ = crdt_manager.load_or_create_doc(&relative_path).await;
137+
if let Err(e) = crdt_manager.load_or_create_doc(&relative_path).await {
138+
eprintln!("Failed to init tracking for new sync file: {}", e);
139+
}
137140
}
138141

139142
match crdt_manager.apply_sync_data(&relative_path, &payload.content).await {

apps/app/src/lib/window_listeners/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ async function on_window_blur() {
3636
editor_view.data.state.doc.toString()
3737
);
3838

39-
// Trigger sync if enabled
40-
try {
41-
await invoke('sync_file', { filePath: opened_filenode.data.path });
42-
} catch (e) {
43-
console.warn("Failed to trigger sync: ", e);
44-
}
39+
// The main_section/text_editor handles sync_file immediately on write_to_file
40+
// so we don't strictly need to double fire it here unless we're caching.
41+
// For safety, re-triggering is fine, but it might just be redundant.
4542
}
4643
}

0 commit comments

Comments
 (0)