From 8fab6cc1a10b7c1c8c18f09df196240e93939881 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Wed, 18 Apr 2018 16:17:43 -0700 Subject: [PATCH] Move dispose methods onto webview itself Also better hide a few 'internal' methods / properties on the panel / webview --- .../src/features/preview.ts | 4 +- src/vs/vscode.d.ts | 38 ++++++------ src/vs/workbench/api/node/extHostWebview.ts | 58 +++++++++---------- 3 files changed, 49 insertions(+), 51 deletions(-) diff --git a/extensions/markdown-language-features/src/features/preview.ts b/extensions/markdown-language-features/src/features/preview.ts index 4e3b3c487ddd0..8f91144439018 100644 --- a/extensions/markdown-language-features/src/features/preview.ts +++ b/extensions/markdown-language-features/src/features/preview.ts @@ -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); @@ -192,7 +192,7 @@ export class MarkdownPreview { this._onDisposeEmitter.dispose(); this._onDidChangeViewStateEmitter.dispose(); - this.editor.dispose(); + this.editor.webview.dispose(); disposeAll(this.disposables); } diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index 5bcb0473c3a04..ede448d9a84b6 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -4874,6 +4874,16 @@ declare module 'vscode' { */ readonly onDidReceiveMessage: Event; + /** + * 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; + /** * Post a message to the webview content. * @@ -4882,6 +4892,15 @@ declare module 'vscode' { * @param message Body of the message. */ postMessage(message: any): Thenable; + + /** + * 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; } /** @@ -4949,16 +4968,6 @@ declare module 'vscode' { */ readonly onDidChangeViewState: Event; - /** - * 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; - /** * Show the webview panel in a given column. * @@ -4966,15 +4975,6 @@ declare module 'vscode' { * 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; } /** diff --git a/src/vs/workbench/api/node/extHostWebview.ts b/src/vs/workbench/api/node/extHostWebview.ts index 25c43afa21bbb..176cb479c86fb 100644 --- a/src/vs/workbench/api/node/extHostWebview.ts +++ b/src/vs/workbench/api/node/extHostWebview.ts @@ -19,12 +19,12 @@ export class ExtHostWebview implements vscode.Webview { private _options: vscode.WebviewOptions; private _isDisposed: boolean = false; + public readonly onDisposeEmitter = new Emitter(); + public readonly onDidDispose: Event = this.onDisposeEmitter.event; + public readonly onMessageEmitter = new Emitter(); public readonly onDidReceiveMessage: Event = this.onMessageEmitter.event; - public readonly onDidChangeViewStateEmitter = new Emitter(); - public readonly onDidChangeViewState: Event = this.onDidChangeViewStateEmitter.event; - constructor( handle: WebviewPanelHandle, proxy: MainThreadWebviewsShape, @@ -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 { @@ -100,13 +109,9 @@ export class ExtHostWebviewPanel implements vscode.WebviewPanel { private _viewColumn: vscode.ViewColumn; private _visible: boolean = true; - public readonly onDisposeEmitter = new Emitter(); - public readonly onDidDispose: Event = this.onDisposeEmitter.event; - public readonly onDidChangeViewStateEmitter = new Emitter(); public readonly onDidChangeViewState: Event = this.onDidChangeViewStateEmitter.event; - constructor( handle: WebviewPanelHandle, proxy: MainThreadWebviewsShape, @@ -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() { @@ -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; } @@ -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 }); } } @@ -257,7 +255,7 @@ export class ExtHostWebviews implements ExtHostWebviewsShape { $onDidDisposeWebviewPanel(handle: WebviewPanelHandle): Thenable { const panel = this.getWebviewPanel(handle); if (panel) { - panel.dispose(); + panel.webview.dispose(); this._webviewPanels.delete(handle); } return TPromise.as(void 0);