Skip to content

Commit

Permalink
Improve document handling to counter desync (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew authored Sep 3, 2024
1 parent 93afb98 commit 00988ea
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions packages/open-collaboration-vscode/src/collaboration-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,23 +647,23 @@ export class CollaborationInstance implements vscode.Disposable {
} else {
this.options.connection.editor.open(this.options.hostId, path);
}
const resyncThrottle = this.getOrCreateThrottle(path, document);
const resyncThrottle = this.getOrCreateThrottle(path);
const observer = (textEvent: Y.YTextEvent) => {
if (textEvent.transaction.local || yjsText.toString() === document.getText()) {
const document = this.findDocument(uri);
if (textEvent.transaction.local || !document || yjsText.toString() === document.getText()) {
// Ignore own events or if the document is already in sync
return;
}
let index = 0;
const edit = new vscode.WorkspaceEdit();
textEvent.delta.forEach(delta => {
if (delta.retain !== undefined) {
if (typeof delta.retain === 'number') {
index += delta.retain;
} else if (delta.insert !== undefined) {
} else if (typeof delta.insert === 'string') {
const pos = document.positionAt(index);
const insert = delta.insert as string;
edit.insert(uri, pos, insert);
index += insert.length;
} else if (delta.delete !== undefined) {
edit.insert(uri, pos, delta.insert);
index += delta.insert.length;
} else if (typeof delta.delete === 'number') {
const pos = document.positionAt(index);
const endPos = document.positionAt(index + delta.delete);
const range = new vscode.Range(pos.line, pos.character, endPos.line, endPos.character);
Expand Down Expand Up @@ -697,26 +697,33 @@ export class CollaborationInstance implements vscode.Disposable {
ytext.insert(change.rangeOffset, change.text);
}
});
this.getOrCreateThrottle(path, event.document)();
this.getOrCreateThrottle(path)();
}, 500);
}
}

private getOrCreateThrottle(path: string, document: vscode.TextDocument): () => void {
private getOrCreateThrottle(path: string): () => void {
let value = this.throttles.get(path);
if (!value) {
const uri = CollaborationUri.getResourceUri(path);
if (uri && !value) {
value = debounce(() => {
this.yjsMutex.runExclusive(async () => {
const yjsText = this.yjs.getText(path);
const newContent = yjsText.toString();
if (newContent !== document.getText()) {
this.updates.add(path);
await this.applyEdit(this.createFullDocumentEdit(document, newContent));
this.updates.delete(path);
const document = this.findDocument(uri);
if (document) {
const yjsText = this.yjs.getText(path);
const newContent = yjsText.toString();
if (newContent !== document.getText()) {
this.updates.add(path);
await this.applyEdit(this.createFullDocumentEdit(document, newContent));
this.updates.delete(path);
}
}
});
}, 200);
this.throttles.set(path, value);
} else {
console.error('Could not determine URI for path', path);
value = () => { };
}
return value;
}
Expand Down

0 comments on commit 00988ea

Please sign in to comment.