Skip to content

Commit

Permalink
Move dispose methods onto webview itself
Browse files Browse the repository at this point in the history
Also better hide a few 'internal' methods / properties on the panel / webview
  • Loading branch information
mjbvz committed Apr 18, 2018
1 parent ee92f9a commit 8fab6cc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
4 changes: 2 additions & 2 deletions extensions/markdown-language-features/src/features/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class MarkdownPreview {
this._locked = locked;
this.editor = webview;

this.editor.onDidDispose(() => {
this.editor.webview.onDidDispose(() => {
this.dispose();
}, null, this.disposables);

Expand Down Expand Up @@ -192,7 +192,7 @@ export class MarkdownPreview {

this._onDisposeEmitter.dispose();
this._onDidChangeViewStateEmitter.dispose();
this.editor.dispose();
this.editor.webview.dispose();

disposeAll(this.disposables);
}
Expand Down
38 changes: 19 additions & 19 deletions src/vs/vscode.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4874,6 +4874,16 @@ declare module 'vscode' {
*/
readonly onDidReceiveMessage: Event<any>;

/**
* Fired when the webview is disposed.
*
* This may be because the user closed the panel that owns this webview
* or because `.dispose()` was called on the webview.
*
* Trying to use the webview after it has been disposed throws an exception.
*/
readonly onDidDispose: Event<void>;

/**
* Post a message to the webview content.
*
Expand All @@ -4882,6 +4892,15 @@ declare module 'vscode' {
* @param message Body of the message.
*/
postMessage(message: any): Thenable<boolean>;

/**
* Dispose of the webview panel.
*
* This closes the panel that owns the webview if it showing and disposes of the
* resources owned by the webview. Webview are also disposed when the user closes
* the webview panel that owns the webview. Both cases fire the `onDispose` event.
*/
dispose(): any;
}

/**
Expand Down Expand Up @@ -4949,32 +4968,13 @@ declare module 'vscode' {
*/
readonly onDidChangeViewState: Event<WebviewPanelOnDidChangeViewStateEvent>;

/**
* Fired when the panel is disposed.
*
* This may be because the user closed the panel or because `.dispose()` was
* called on it.
*
* Trying to use the panel after it has been disposed throws an exception.
*/
readonly onDidDispose: Event<void>;

/**
* Show the webview panel in a given column.
*
* A webview panel may only show in a single column at a time. If it is already showing, this
* method moves it to a new column.
*/
reveal(viewColumn: ViewColumn): void;

/**
* Dispose of the webview panel.
*
* This closes the panel if it showing and disposes of the resources owned by the webview.
* Webview panels are also disposed when the user closes the webview panel. Both cases
* fire the `onDispose` event.
*/
dispose(): any;
}

/**
Expand Down
58 changes: 28 additions & 30 deletions src/vs/workbench/api/node/extHostWebview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export class ExtHostWebview implements vscode.Webview {
private _options: vscode.WebviewOptions;
private _isDisposed: boolean = false;

public readonly onDisposeEmitter = new Emitter<void>();
public readonly onDidDispose: Event<void> = this.onDisposeEmitter.event;

public readonly onMessageEmitter = new Emitter<any>();
public readonly onDidReceiveMessage: Event<any> = this.onMessageEmitter.event;

public readonly onDidChangeViewStateEmitter = new Emitter<vscode.WebviewPanelOnDidChangeViewStateEvent>();
public readonly onDidChangeViewState: Event<vscode.WebviewPanelOnDidChangeViewStateEvent> = this.onDidChangeViewStateEmitter.event;

constructor(
handle: WebviewPanelHandle,
proxy: MainThreadWebviewsShape,
Expand All @@ -37,8 +37,17 @@ export class ExtHostWebview implements vscode.Webview {
this._options = options;
}

dispose() {
this.onDidChangeViewStateEmitter.dispose();
public dispose() {
if (this._isDisposed) {
return;
}

this._isDisposed = true;
this.onDisposeEmitter.fire();

this._proxy.$disposeWebview(this._handle);

this.onDisposeEmitter.dispose();
}

get title(): string {
Expand Down Expand Up @@ -100,13 +109,9 @@ export class ExtHostWebviewPanel implements vscode.WebviewPanel {
private _viewColumn: vscode.ViewColumn;
private _visible: boolean = true;

public readonly onDisposeEmitter = new Emitter<void>();
public readonly onDidDispose: Event<void> = this.onDisposeEmitter.event;

public readonly onDidChangeViewStateEmitter = new Emitter<vscode.WebviewPanelOnDidChangeViewStateEvent>();
public readonly onDidChangeViewState: Event<vscode.WebviewPanelOnDidChangeViewStateEvent> = this.onDidChangeViewStateEmitter.event;


constructor(
handle: WebviewPanelHandle,
proxy: MainThreadWebviewsShape,
Expand All @@ -121,6 +126,8 @@ export class ExtHostWebviewPanel implements vscode.WebviewPanel {
this._options = editorOptions;
this._viewColumn = viewColumn;
this._webview = webview;

webview.onDidDispose(this.dispose, this);
}

public dispose() {
Expand All @@ -129,44 +136,35 @@ export class ExtHostWebviewPanel implements vscode.WebviewPanel {
}

this._isDisposed = true;
this.onDisposeEmitter.fire();

this._proxy.$disposeWebview(this._handle);

this.onDisposeEmitter.dispose();
this.onDidChangeViewStateEmitter.dispose();
}

get webview() {
this.assertNotDisposed();
public get webview() {
return this._webview;
}

get viewType(): string {
this.assertNotDisposed();
public get viewType(): string {
return this._viewType;
}

get options() {
public get options() {
return this._options;
}

get viewColumn(): vscode.ViewColumn {
this.assertNotDisposed();
return this._viewColumn;
public get viewColumn(): vscode.ViewColumn | undefined {
return this._isDisposed ? undefined : this._viewColumn;
}

set viewColumn(value: vscode.ViewColumn) {
_setViewColumn(value: vscode.ViewColumn) {
this.assertNotDisposed();
this._viewColumn = value;
}

get visible(): boolean {
this.assertNotDisposed();
return this._visible;
public get visible(): boolean {
return !this._isDisposed && this._visible;
}

set visible(value: boolean) {
_setVisible(value: boolean) {
this.assertNotDisposed();
this._visible = value;
}
Expand Down Expand Up @@ -247,8 +245,8 @@ export class ExtHostWebviews implements ExtHostWebviewsShape {
if (panel) {
const viewColumn = typeConverters.toViewColumn(position);
if (panel.visible !== visible || panel.viewColumn !== viewColumn) {
panel.visible = visible;
panel.viewColumn = viewColumn;
panel._setVisible(visible);
panel._setViewColumn(viewColumn);
panel.onDidChangeViewStateEmitter.fire({ webviewPanel: panel });
}
}
Expand All @@ -257,7 +255,7 @@ export class ExtHostWebviews implements ExtHostWebviewsShape {
$onDidDisposeWebviewPanel(handle: WebviewPanelHandle): Thenable<void> {
const panel = this.getWebviewPanel(handle);
if (panel) {
panel.dispose();
panel.webview.dispose();
this._webviewPanels.delete(handle);
}
return TPromise.as(void 0);
Expand Down

0 comments on commit 8fab6cc

Please sign in to comment.