mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-13 11:38:36 -05:00
Merge from vscode 817eb6b0c720a4ecbc13c020afbbebfed667aa09 (#7356)
This commit is contained in:
@@ -11,6 +11,32 @@ import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc';
|
||||
// for a very basic process <=> process communication over methods.
|
||||
//
|
||||
|
||||
export type AddContextToFunctions<Target, Context> = {
|
||||
// For every property: IF property is a FUNCTION ADD context as first parameter and original parameters afterwards with same return type, otherwise preserve as is
|
||||
[K in keyof Target]: Target[K] extends (...args: any) => any ? (context: Context, ...args: Parameters<Target[K]>) => ReturnType<Target[K]> : Target[K]
|
||||
};
|
||||
|
||||
interface ISimpleChannelProxyContext {
|
||||
__$simpleIPCContextMarker: boolean;
|
||||
proxyContext: unknown;
|
||||
}
|
||||
|
||||
function serializeContext(proxyContext?: unknown): ISimpleChannelProxyContext | undefined {
|
||||
if (proxyContext) {
|
||||
return { __$simpleIPCContextMarker: true, proxyContext };
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function deserializeContext(candidate?: ISimpleChannelProxyContext | undefined): unknown | undefined {
|
||||
if (candidate && candidate.__$simpleIPCContextMarker === true) {
|
||||
return candidate.proxyContext;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export class SimpleServiceProxyChannel implements IServerChannel {
|
||||
|
||||
private service: { [key: string]: unknown };
|
||||
@@ -23,9 +49,16 @@ export class SimpleServiceProxyChannel implements IServerChannel {
|
||||
throw new Error(`Events are currently unsupported by SimpleServiceProxyChannel: ${event}`);
|
||||
}
|
||||
|
||||
call(_: unknown, command: string, args: any[]): Promise<any> {
|
||||
call(_: unknown, command: string, args?: any[]): Promise<any> {
|
||||
const target = this.service[command];
|
||||
if (typeof target === 'function') {
|
||||
if (Array.isArray(args)) {
|
||||
const context = deserializeContext(args[0]);
|
||||
if (context) {
|
||||
args[0] = context;
|
||||
}
|
||||
}
|
||||
|
||||
return target.apply(this.service, args);
|
||||
}
|
||||
|
||||
@@ -33,12 +66,21 @@ export class SimpleServiceProxyChannel implements IServerChannel {
|
||||
}
|
||||
}
|
||||
|
||||
export function createSimpleChannelProxy<T>(channel: IChannel): T {
|
||||
export function createSimpleChannelProxy<T>(channel: IChannel, context?: unknown): T {
|
||||
const serializedContext = serializeContext(context);
|
||||
|
||||
return new Proxy({}, {
|
||||
get(_target, propKey, _receiver) {
|
||||
if (typeof propKey === 'string') {
|
||||
return function (...args: any[]) {
|
||||
return channel.call(propKey, args);
|
||||
let methodArgs: any[];
|
||||
if (serializedContext) {
|
||||
methodArgs = [context, ...args];
|
||||
} else {
|
||||
methodArgs = args;
|
||||
}
|
||||
|
||||
return channel.call(propKey, methodArgs);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user