From 9b05789d2addec93ace064ab5f482bfde58f1e64 Mon Sep 17 00:00:00 2001 From: Vitaliy Gulyy Date: Thu, 29 Oct 2020 17:21:31 +0200 Subject: [PATCH] Do not store webview if there is no corresponding serializer Signed-off-by: Vitaliy Gulyy --- .../browser/shell/shell-layout-restorer.ts | 20 +++++++++++-------- .../src/hosted/browser/hosted-plugin.ts | 11 ++++++---- .../main/browser/view/plugin-view-registry.ts | 10 ++++++++-- .../src/main/browser/webviews-main.ts | 10 +++++----- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/core/src/browser/shell/shell-layout-restorer.ts b/packages/core/src/browser/shell/shell-layout-restorer.ts index 2320166cf5233..38166cb992afc 100644 --- a/packages/core/src/browser/shell/shell-layout-restorer.ts +++ b/packages/core/src/browser/shell/shell-layout-restorer.ts @@ -32,9 +32,9 @@ import { ApplicationShell, applicationShellLayoutVersion, ApplicationShellLayout export interface StatefulWidget { /** - * Called on unload to store the inner state. + * Called on unload to store the inner state. Returns 'undefined' if the widget cannot be stored. */ - storeState(): object; + storeState(): object | undefined; /** * Called when the widget got created by the storage service @@ -205,14 +205,18 @@ export class ShellLayoutRestorer implements CommandContribution { private convertToDescription(widget: Widget): WidgetDescription | undefined { const desc = this.widgetManager.getDescription(widget); if (desc) { - let innerState = undefined; if (StatefulWidget.is(widget)) { - innerState = widget.storeState(); + const innerState = widget.storeState(); + return innerState ? { + constructionOptions: desc, + innerWidgetState: this.deflate(innerState) + } : undefined; + } else { + return { + constructionOptions: desc, + innerWidgetState: undefined + }; } - return { - constructionOptions: desc, - innerWidgetState: innerState && this.deflate(innerState) - }; } } diff --git a/packages/plugin-ext/src/hosted/browser/hosted-plugin.ts b/packages/plugin-ext/src/hosted/browser/hosted-plugin.ts index bc2b02446b527..20153fb49aa1f 100644 --- a/packages/plugin-ext/src/hosted/browser/hosted-plugin.ts +++ b/packages/plugin-ext/src/hosted/browser/hosted-plugin.ts @@ -197,19 +197,22 @@ export class HostedPluginSupport { this.taskProviderRegistry.onWillProvideTaskProvider(event => this.ensureTaskActivation(event)); this.taskResolverRegistry.onWillProvideTaskResolver(event => this.ensureTaskActivation(event)); this.fileService.onWillActivateFileSystemProvider(event => this.ensureFileSystemActivation(event)); + this.widgets.onDidCreateWidget(({ factoryId, widget }) => { if (factoryId === WebviewWidget.FACTORY_ID && widget instanceof WebviewWidget) { const storeState = widget.storeState.bind(widget); const restoreState = widget.restoreState.bind(widget); + widget.storeState = () => { if (this.webviewRevivers.has(widget.viewType)) { return storeState(); } - return {}; + return undefined; }; - widget.restoreState = oldState => { - if (oldState.viewType) { - restoreState(oldState); + + widget.restoreState = state => { + if (state.viewType) { + restoreState(state); this.preserveWebview(widget); } else { widget.dispose(); diff --git a/packages/plugin-ext/src/main/browser/view/plugin-view-registry.ts b/packages/plugin-ext/src/main/browser/view/plugin-view-registry.ts index 874ae204d25e7..52e60f973e337 100644 --- a/packages/plugin-ext/src/main/browser/view/plugin-view-registry.ts +++ b/packages/plugin-ext/src/main/browser/view/plugin-view-registry.ts @@ -505,7 +505,10 @@ export class PluginViewRegistry implements FrontendApplicationContribution { const widgets = this.widgetManager.getWidgets(PLUGIN_VIEW_DATA_FACTORY_ID); for (const widget of widgets) { if (StatefulWidget.is(widget)) { - this.viewDataState.set(widget.id, widget.storeState()); + const state = widget.storeState(); + if (state) { + this.viewDataState.set(widget.id, state); + } } widget.dispose(); } @@ -565,7 +568,10 @@ export class PluginViewRegistry implements FrontendApplicationContribution { protected storeViewDataStateOnDispose(viewId: string, widget: Widget & StatefulWidget): void { const dispose = widget.dispose.bind(widget); widget.dispose = () => { - this.viewDataState.set(viewId, widget.storeState()); + const state = widget.storeState(); + if (state) { + this.viewDataState.set(viewId, state); + } dispose(); }; } diff --git a/packages/plugin-ext/src/main/browser/webviews-main.ts b/packages/plugin-ext/src/main/browser/webviews-main.ts index fea4b303b0f52..9c2524d10b16d 100644 --- a/packages/plugin-ext/src/main/browser/webviews-main.ts +++ b/packages/plugin-ext/src/main/browser/webviews-main.ts @@ -34,7 +34,7 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable { private readonly proxy: WebviewsExt; protected readonly shell: ApplicationShell; - protected readonly widgets: WidgetManager; + protected readonly widgetManager: WidgetManager; protected readonly pluginService: HostedPluginSupport; protected readonly viewColumnService: ViewColumnService; private readonly toDispose = new DisposableCollection(); @@ -43,7 +43,7 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable { this.proxy = rpc.getProxy(MAIN_RPC_CONTEXT.WEBVIEWS_EXT); this.shell = container.get(ApplicationShell); this.viewColumnService = container.get(ViewColumnService); - this.widgets = container.get(WidgetManager); + this.widgetManager = container.get(WidgetManager); this.pluginService = container.get(HostedPluginSupport); this.toDispose.push(this.shell.onDidChangeActiveWidget(() => this.updateViewStates())); this.toDispose.push(this.shell.onDidChangeCurrentWidget(() => this.updateViewStates())); @@ -61,7 +61,7 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable { showOptions: WebviewPanelShowOptions, options: WebviewPanelOptions & WebviewOptions ): Promise { - const view = await this.widgets.getOrCreateWidget(WebviewWidget.FACTORY_ID, { id: panelId }); + const view = await this.widgetManager.getOrCreateWidget(WebviewWidget.FACTORY_ID, { id: panelId }); this.hookWebview(view); view.viewType = viewType; view.title.label = title; @@ -217,7 +217,7 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable { } protected readonly updateViewStates = debounce(() => { - for (const widget of this.widgets.getWidgets(WebviewWidget.FACTORY_ID)) { + for (const widget of this.widgetManager.getWidgets(WebviewWidget.FACTORY_ID)) { if (widget instanceof WebviewWidget) { this.updateViewState(widget); } @@ -251,7 +251,7 @@ export class WebviewsMainImpl implements WebviewsMain, Disposable { } private async tryGetWebview(id: string): Promise { - return this.widgets.getWidget(WebviewWidget.FACTORY_ID, { id }); + return this.widgetManager.getWidget(WebviewWidget.FACTORY_ID, { id }); } }