Skip to content

Commit

Permalink
Check pending version before updating markdown preview content
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed Apr 29, 2019
1 parent 98f383f commit 4470b86
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions extensions/markdown-language-features/src/features/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ interface PreviewStyleLoadErrorMessage extends WebviewMessage {
};
}

export class PreviewDocumentVersion {
public constructor(
public readonly resource: vscode.Uri,
public readonly version: number,
) { }

public equals(other: PreviewDocumentVersion): boolean {
return this.resource.fsPath === other.resource.fsPath
&& this.version === other.version;
}
}

export class MarkdownPreview extends Disposable {

public static viewType = 'markdown.preview';
Expand All @@ -71,7 +83,7 @@ export class MarkdownPreview extends Disposable {
private throttleTimer: any;
private line: number | undefined = undefined;
private firstUpdate = true;
private currentVersion?: { resource: vscode.Uri, version: number };
private currentVersion?: PreviewDocumentVersion;
private forceUpdate = false;
private isScrolling = false;
private _disposed: boolean = false;
Expand Down Expand Up @@ -389,18 +401,23 @@ export class MarkdownPreview extends Disposable {
return;
}

if (!this.forceUpdate && this.currentVersion && this.currentVersion.resource.fsPath === resource.fsPath && this.currentVersion.version === document.version) {
const pendingVersion = new PreviewDocumentVersion(resource, document.version);
if (!this.forceUpdate && this.currentVersion && this.currentVersion.equals(pendingVersion)) {
if (this.line) {
this.updateForView(resource, this.line);
}
return;
}
this.forceUpdate = false;

this.currentVersion = { resource, version: document.version };
this.currentVersion = pendingVersion;
if (this._resource === resource) {
const content = await this._contentProvider.provideTextDocumentContent(document, this._previewConfigurations, this.line, this.state);
this.setContent(content);
// Another call to `doUpdate` may have happened.
// Make sure we are still updating for the correct document
if (this.currentVersion && this.currentVersion.equals(pendingVersion)) {
this.setContent(content);
}
}
}

Expand Down

0 comments on commit 4470b86

Please sign in to comment.