forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmountedWebViewFactory.ts
More file actions
44 lines (38 loc) · 1.52 KB
/
mountedWebViewFactory.ts
File metadata and controls
44 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { ReactWrapper } from 'enzyme';
import { inject, injectable } from 'inversify';
import { IDisposable, IDisposableRegistry } from '../../client/common/types';
import { IMountedWebView, MountedWebView } from './mountedWebView';
export const IMountedWebViewFactory = Symbol('IMountedWebViewFactory');
export interface IMountedWebViewFactory {
get(id: string): IMountedWebView;
// tslint:disable-next-line: no-any
create(id: string, mount: () => ReactWrapper<any, Readonly<{}>, React.Component>): IMountedWebView;
}
@injectable()
export class MountedWebViewFactory implements IMountedWebViewFactory, IDisposable {
private map = new Map<string, MountedWebView>();
constructor(@inject(IDisposableRegistry) readonly disposables: IDisposableRegistry) {
disposables.push(this);
}
public dispose() {
this.map.forEach((v) => v.dispose());
this.map.clear();
}
public get(id: string): IMountedWebView {
const obj = this.map.get(id);
if (!obj) {
throw new Error(`No mounted web view found for id ${id}`);
}
return obj;
}
// tslint:disable-next-line: no-any
public create(id: string, mount: () => ReactWrapper<any, Readonly<{}>, React.Component>): IMountedWebView {
if (this.map.has(id)) {
throw new Error(`Mounted web view already exists for id ${id}`);
}
const obj = new MountedWebView(mount, id);
obj.onDisposed(() => this.map.delete(id));
this.map.set(id, obj);
return obj;
}
}