Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve document handling to counter desync #64

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading