Skip to content

Commit d241987

Browse files
committed
Use bi-direcitonal map for mapping webviews to handles
1 parent b1dd95c commit d241987

1 file changed

Lines changed: 41 additions & 13 deletions

File tree

src/vs/workbench/api/browser/mainThreadWebview.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,62 @@
55

66
import { onUnexpectedError } from 'vs/base/common/errors';
77
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
8-
import * as map from 'vs/base/common/map';
8+
import { startsWith } from 'vs/base/common/strings';
99
import { URI, UriComponents } from 'vs/base/common/uri';
1010
import * as modes from 'vs/editor/common/modes';
1111
import { localize } from 'vs/nls';
1212
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
1313
import { IOpenerService } from 'vs/platform/opener/common/opener';
14+
import { IProductService } from 'vs/platform/product/common/product';
1415
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
1516
import { ExtHostContext, ExtHostWebviewsShape, IExtHostContext, MainContext, MainThreadWebviewsShape, WebviewPanelHandle, WebviewPanelShowOptions, WebviewPanelViewStateData } from 'vs/workbench/api/common/extHost.protocol';
1617
import { editorGroupToViewColumn, EditorViewColumn, viewColumnToEditorGroup } from 'vs/workbench/api/common/shared/editor';
18+
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
1719
import { WebviewEditorInput } from 'vs/workbench/contrib/webview/browser/webviewEditorInput';
1820
import { ICreateWebViewShowOptions, IWebviewEditorService, WebviewInputOptions } from 'vs/workbench/contrib/webview/browser/webviewEditorService';
1921
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
2022
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2123
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
2224
import { extHostNamedCustomer } from '../common/extHostCustomers';
23-
import { IProductService } from 'vs/platform/product/common/product';
24-
import { startsWith } from 'vs/base/common/strings';
25-
import { Webview } from 'vs/workbench/contrib/webview/browser/webview';
26-
import { find } from 'vs/base/common/arrays';
2725

2826
interface OldMainThreadWebviewState {
2927
readonly viewType: string;
3028
state: any;
3129
}
3230

31+
/**
32+
* Bi-directional map between webview handles and inputs.
33+
*/
34+
class WebviewHandleStore {
35+
private readonly _handlesToInputs = new Map<string, WebviewEditorInput>();
36+
private readonly _inputsToHandles = new Map<WebviewEditorInput, string>();
37+
38+
public add(handle: string, input: WebviewEditorInput): void {
39+
this._handlesToInputs.set(handle, input);
40+
this._inputsToHandles.set(input, handle);
41+
}
42+
43+
public getHandleForInput(input: WebviewEditorInput): string | undefined {
44+
return this._inputsToHandles.get(input);
45+
}
46+
47+
public getInputForHandle(handle: string): WebviewEditorInput | undefined {
48+
return this._handlesToInputs.get(handle);
49+
}
50+
51+
public delete(handle: string): void {
52+
const input = this.getInputForHandle(handle);
53+
this._handlesToInputs.delete(handle);
54+
if (input) {
55+
this._inputsToHandles.delete(input);
56+
}
57+
}
58+
59+
public get size(): number {
60+
return this._handlesToInputs.size;
61+
}
62+
}
63+
3364
@extHostNamedCustomer(MainContext.MainThreadWebviews)
3465
export class MainThreadWebviews extends Disposable implements MainThreadWebviewsShape {
3566

@@ -44,7 +75,7 @@ export class MainThreadWebviews extends Disposable implements MainThreadWebviews
4475
private static revivalPool = 0;
4576

4677
private readonly _proxy: ExtHostWebviewsShape;
47-
private readonly _webviewEditorInputs = new Map<string, WebviewEditorInput>();
78+
private readonly _webviewEditorInputs = new WebviewHandleStore();
4879
private readonly _revivers = new Map<string, IDisposable>();
4980

5081
constructor(
@@ -102,7 +133,7 @@ export class MainThreadWebviews extends Disposable implements MainThreadWebviews
102133
});
103134
this.hookupWebviewEventDelegate(handle, webview);
104135

105-
this._webviewEditorInputs.set(handle, webview);
136+
this._webviewEditorInputs.add(handle, webview);
106137

107138
/* __GDPR__
108139
"webviews:createWebviewPanel" : {
@@ -172,7 +203,7 @@ export class MainThreadWebviews extends Disposable implements MainThreadWebviews
172203
}
173204

174205
const handle = `revival-${MainThreadWebviews.revivalPool++}`;
175-
this._webviewEditorInputs.set(handle, webviewEditorInput);
206+
this._webviewEditorInputs.add(handle, webviewEditorInput);
176207
this.hookupWebviewEventDelegate(handle, webviewEditorInput);
177208

178209
let state = undefined;
@@ -254,10 +285,7 @@ export class MainThreadWebviews extends Disposable implements MainThreadWebviews
254285
continue;
255286
}
256287

257-
const handle = find(
258-
map.keys(this._webviewEditorInputs),
259-
handle => input === this._webviewEditorInputs.get(handle));
260-
288+
const handle = this._webviewEditorInputs.getHandleForInput(input);
261289
if (handle) {
262290
viewStates[handle] = {
263291
visible: input === group.activeEditor,
@@ -303,7 +331,7 @@ export class MainThreadWebviews extends Disposable implements MainThreadWebviews
303331
}
304332

305333
private tryGetWebviewEditorInput(handle: WebviewPanelHandle): WebviewEditorInput | undefined {
306-
return this._webviewEditorInputs.get(handle);
334+
return this._webviewEditorInputs.getInputForHandle(handle);
307335
}
308336

309337
private getWebview(handle: WebviewPanelHandle): Webview {

0 commit comments

Comments
 (0)