Skip to content

Commit

Permalink
fix(buffer): avoid sync conflicts during startup (#1414)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyaowong committed Sep 4, 2023
1 parent 7094dc7 commit 0b3ab8f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/buffer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {

import { Logger } from "./logger";
import { NeovimExtensionRequestProcessable, NeovimRedrawProcessable } from "./neovim_events_processable";
import { callAtomic, convertByteNumToCharNum } from "./utils";
import { ManualPromise, callAtomic, convertByteNumToCharNum } from "./utils";
import { MainController } from "./main_controller";

// !Note: document and editors in vscode events and namespace are reference stable
Expand All @@ -45,8 +45,7 @@ export class BufferManager implements Disposable, NeovimRedrawProcessable, Neovi
/**
* Internal sync promise
*/
private changeLayoutPromise?: Promise<void>;
private changeLayoutPromiseResolve?: () => void;
private changeLayoutPromise?: ManualPromise;
private cancelTokenSource: CancellationTokenSource = new CancellationTokenSource();
/**
* Currently opened editors
Expand Down Expand Up @@ -110,15 +109,18 @@ export class BufferManager implements Disposable, NeovimRedrawProcessable, Neovi

public async forceResync(): Promise<void> {
this.logger.debug(`${LOG_PREFIX}: force resyncing layout`);
if (!this.changeLayoutPromise) {
this.changeLayoutPromise = new ManualPromise();
}
// this.cancelTokenSource will always be cancelled when the visible editors change
await this.syncLayout(this.cancelTokenSource.token);
await this.syncActiveEditor();
await this.syncLayoutDebounced(this.cancelTokenSource.token);
await this.syncActiveEditorDebounced();
}

public async waitForLayoutSync(): Promise<void> {
if (this.changeLayoutPromise) {
this.logger.debug(`${LOG_PREFIX}: Waiting for completing layout resyncing`);
await this.changeLayoutPromise;
await this.changeLayoutPromise.promise;
this.logger.debug(`${LOG_PREFIX}: Waiting done`);
}
}
Expand Down Expand Up @@ -312,7 +314,7 @@ export class BufferManager implements Disposable, NeovimRedrawProcessable, Neovi
// !for this init a promise early, then resolve it after processing
this.logger.debug(`${LOG_PREFIX}: onDidChangeVisibleTextEditors`);
if (!this.changeLayoutPromise) {
this.changeLayoutPromise = new Promise((res) => (this.changeLayoutPromiseResolve = res));
this.changeLayoutPromise = new ManualPromise();
}

// Cancel the previous syncLayout call, and then create a new token source for the new
Expand Down Expand Up @@ -437,9 +439,8 @@ export class BufferManager implements Disposable, NeovimRedrawProcessable, Neovi
this.logger.debug(`${LOG_PREFIX}: Cancellation requested in syncLayout, returning`);
return;
}
if (this.changeLayoutPromiseResolve) {
this.changeLayoutPromiseResolve();
}

this.changeLayoutPromise?.resolve();
this.changeLayoutPromise = undefined;
};

Expand Down

0 comments on commit 0b3ab8f

Please sign in to comment.