提交 2b4ce134 编写于 作者: A Alex Dima

Add RPCProtocol.getFastProxy (#36972)

上级 c1cf8ef3
......@@ -7,7 +7,8 @@
import {
createMainContextProxyIdentifier as createMainId,
createExtHostContextProxyIdentifier as createExtId,
ProxyIdentifier
ProxyIdentifier,
IRPCProtocol
} from 'vs/workbench/services/extensions/node/proxyIdentifier';
import * as vscode from 'vscode';
......@@ -90,28 +91,10 @@ export interface IWorkspaceConfigurationChangeEventData {
changedConfigurationByResource: { [folder: string]: IConfigurationModel };
}
export interface IExtHostContext {
/**
* Returns a proxy to an object addressable/named in the extension host process.
*/
get<T>(identifier: ProxyIdentifier<T>): T;
/**
* Register manually created instance.
*/
set<T, R extends T>(identifier: ProxyIdentifier<T>, instance: R): R;
/**
* Assert these identifiers are already registered via `.set`.
*/
assertRegistered(identifiers: ProxyIdentifier<any>[]): void;
export interface IExtHostContext extends IRPCProtocol {
}
export interface IMainContext {
/**
* Returns a proxy to an object addressable/named in the main/renderer process.
*/
get<T>(identifier: ProxyIdentifier<T>): T;
export interface IMainContext extends IRPCProtocol {
}
// --- main thread
......@@ -681,7 +664,7 @@ export interface ExtHostWindowShape {
// --- proxy identifiers
export const MainContext = {
MainThreadCommands: createMainId<MainThreadCommandsShape>('MainThreadCommands'),
MainThreadCommands: <ProxyIdentifier<MainThreadCommandsShape>>createMainId<MainThreadCommandsShape>('MainThreadCommands'),
MainThreadConfiguration: createMainId<MainThreadConfigurationShape>('MainThreadConfiguration'),
MainThreadDebugService: createMainId<MainThreadDebugServiceShape>('MainThreadDebugService'),
MainThreadDecorations: createMainId<MainThreadDecorationsShape>('MainThreadDecorations'),
......
......@@ -4,6 +4,33 @@
*--------------------------------------------------------------------------------------------*/
'use strict';
export interface IRPCProtocol {
/**
* Returns a proxy to an object addressable/named in the extension host process.
* > **Note:** Arguments or results of type `URI` or `RegExp` will be serialized/deserialized automatically,
* > but this has a performance cost, as each argument/result must be visited.
* >
* > Use `getFast` for a proxy where such arguments are not automatically serialized/deserialized.
*/
get<T>(identifier: ProxyIdentifier<T>): T;
/**
* Returns a proxy to an object addressable/named in the extension host process.
* > **Note:** Arguments or results of type `URI` or `RegExp` will **not** be serialized/deserialized automatically.
*/
getFastProxy<T>(identifier: ProxyIdentifier<T>): T;
/**
* Register manually created instance.
*/
set<T, R extends T>(identifier: ProxyIdentifier<T>, instance: R): R;
/**
* Assert these identifiers are already registered via `.set`.
*/
assertRegistered(identifiers: ProxyIdentifier<any>[]): void;
}
export class ProxyIdentifier<T> {
_proxyIdentifierBrand: void;
_suppressCompilerUnusedWarning: T;
......
......@@ -9,12 +9,12 @@ import * as marshalling from 'vs/base/common/marshalling';
import * as errors from 'vs/base/common/errors';
import { IMessagePassingProtocol } from 'vs/base/parts/ipc/common/ipc';
import { LazyPromise } from 'vs/workbench/services/extensions/node/lazyPromise';
import { ProxyIdentifier } from 'vs/workbench/services/extensions/node/proxyIdentifier';
import { ProxyIdentifier, IRPCProtocol } from 'vs/workbench/services/extensions/node/proxyIdentifier';
import { CharCode } from 'vs/base/common/charCode';
declare var Proxy: any; // TODO@TypeScript
export class RPCProtocol {
export class RPCProtocol implements IRPCProtocol {
private _isDisposed: boolean;
private readonly _locals: { [id: string]: any; };
......@@ -46,17 +46,28 @@ export class RPCProtocol {
public get<T>(identifier: ProxyIdentifier<T>): T {
if (!this._proxies[identifier.id]) {
this._proxies[identifier.id] = this._createProxy(identifier.id);
this._proxies[identifier.id] = this._createProxy(identifier.id, true);
}
return this._proxies[identifier.id];
}
private _createProxy<T>(proxyId: string): T {
public getFastProxy<T>(identifier: ProxyIdentifier<T>): T {
if (!this._proxies[identifier.id]) {
this._proxies[identifier.id] = this._createProxy(identifier.id, false);
}
return this._proxies[identifier.id];
}
private _createProxy<T>(proxyId: string, isFancy: boolean): T {
let handler = {
get: (target, name: string) => {
if (!target[name] && name.charCodeAt(0) === CharCode.DollarSign) {
target[name] = (...myArgs: any[]) => {
return this.fancyRemoteCall(proxyId, name, myArgs);
return (
isFancy
? this.fancyRemoteCall(proxyId, name, myArgs)
: this.remoteCall(proxyId, name, myArgs)
);
};
}
return target[name];
......@@ -186,11 +197,11 @@ export class RPCProtocol {
return method.apply(actor, args);
}
public remoteCall(proxyId: string, methodName: string, args: any[]): TPromise<any> {
private remoteCall(proxyId: string, methodName: string, args: any[]): TPromise<any> {
return this._remoteCall(proxyId, methodName, args, false);
}
public fancyRemoteCall(proxyId: string, methodName: string, args: any[]): TPromise<any> {
private fancyRemoteCall(proxyId: string, methodName: string, args: any[]): TPromise<any> {
return this._remoteCall(proxyId, methodName, args, true);
}
......@@ -290,7 +301,7 @@ class MessageFactory {
}
}
export const enum MessageType {
const enum MessageType {
Request = 1,
FancyRequest = 2,
Cancel = 3,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册