/
abstractThreadService.ts
88 lines (76 loc) · 2.81 KB
/
abstractThreadService.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import { IDispatcher, RPCProtocol } from 'vs/workbench/services/extensions/node/rpcProtocol';
import { ProxyIdentifier } from 'vs/workbench/services/thread/common/threadService';
declare var Proxy: any; // TODO@TypeScript
export abstract class AbstractThreadService implements IDispatcher {
private readonly _rpcProtocol: RPCProtocol;
private readonly _isMain: boolean;
protected readonly _locals: { [id: string]: any; };
private readonly _proxies: { [id: string]: any; } = Object.create(null);
constructor(rpcProtocol: RPCProtocol, isMain: boolean) {
this._rpcProtocol = rpcProtocol;
this._isMain = isMain;
this._locals = Object.create(null);
this._proxies = Object.create(null);
this._rpcProtocol.setDispatcher(this);
}
public dispose(): void {
this._rpcProtocol.dispose();
}
public invoke(proxyId: string, methodName: string, args: any[]): any {
if (!this._locals[proxyId]) {
throw new Error('Unknown actor ' + proxyId);
}
let actor = this._locals[proxyId];
let method = actor[methodName];
if (typeof method !== 'function') {
throw new Error('Unknown method ' + methodName + ' on actor ' + proxyId);
}
return method.apply(actor, args);
}
get<T>(identifier: ProxyIdentifier<T>): T {
if (!this._proxies[identifier.id]) {
this._proxies[identifier.id] = this._createProxy(identifier.id);
}
return this._proxies[identifier.id];
}
private _createProxy<T>(proxyId: string): T {
let handler = {
get: (target, name) => {
if (name === 'toJSON') {
return undefined;
}
if (!target[name]) {
target[name] = (...myArgs: any[]) => {
return this._callOnRemote(proxyId, name, myArgs);
};
}
return target[name];
}
};
return new Proxy(Object.create(null), handler);
}
set<T, R extends T>(identifier: ProxyIdentifier<T>, value: R): R {
if (identifier.isMain !== this._isMain) {
throw new Error('Mismatch in object registration!');
}
this._locals[identifier.id] = value;
return value;
}
assertRegistered(identifiers: ProxyIdentifier<any>[]): void {
for (let i = 0, len = identifiers.length; i < len; i++) {
const identifier = identifiers[i];
if (!this._locals[identifier.id]) {
throw new Error(`Missing actor ${identifier.id} (isMain: ${identifier.isMain})`);
}
}
}
private _callOnRemote(proxyId: string, methodName: string, args: any[]): TPromise<any> {
return this._rpcProtocol.callOnRemote(proxyId, methodName, args);
}
}