mirror of
https://github.com/Microsoft/vscode
synced 2024-10-30 13:10:43 +00:00
debt - introduce and use simpleIpcProxy
This commit is contained in:
parent
a0e65a532d
commit
c3d611c7ad
6 changed files with 56 additions and 47 deletions
|
@ -832,29 +832,3 @@ export class StaticRouter<TContext = string> implements IClientRouter<TContext>
|
|||
return await this.route(hub);
|
||||
}
|
||||
}
|
||||
|
||||
export class SimpleServiceProxyChannel implements IServerChannel {
|
||||
|
||||
private service: { [key: string]: unknown };
|
||||
|
||||
constructor(service: unknown) {
|
||||
this.service = service as { [key: string]: unknown };
|
||||
}
|
||||
|
||||
listen<T>(_: unknown, event: string): Event<T> {
|
||||
throw new Error(`Events are currently unsupported by SimpleServiceProxyChannel: ${event}`);
|
||||
}
|
||||
|
||||
call(_: unknown, command: string, arg?: any): Promise<any> {
|
||||
const target = this.service[command];
|
||||
if (typeof target === 'function') {
|
||||
if (Array.isArray(arg)) {
|
||||
return target.apply(this.service, arg);
|
||||
}
|
||||
|
||||
return target.call(this.service, arg);
|
||||
}
|
||||
|
||||
throw new Error(`Call Not Found: ${command}`);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,8 @@ import { NullTelemetryService, combinedAppender, LogAppender } from 'vs/platform
|
|||
import { TelemetryAppenderClient } from 'vs/platform/telemetry/node/telemetryIpc';
|
||||
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
|
||||
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
|
||||
import { getDelayedChannel, StaticRouter, SimpleServiceProxyChannel } from 'vs/base/parts/ipc/common/ipc';
|
||||
import { getDelayedChannel, StaticRouter } from 'vs/base/parts/ipc/common/ipc';
|
||||
import { SimpleServiceProxyChannel } from 'vs/platform/ipc/node/simpleIpcProxy';
|
||||
import product from 'vs/platform/product/common/product';
|
||||
import { ProxyAuthHandler } from 'vs/code/electron-main/auth';
|
||||
import { Disposable } from 'vs/base/common/lifecycle';
|
||||
|
|
|
@ -4,13 +4,14 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IElectronService } from 'vs/platform/electron/node/electron';
|
||||
import { IMainProcessService, createSimpleMainChannelProxy } from 'vs/platform/ipc/electron-browser/mainProcessService';
|
||||
import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService';
|
||||
import { createSimpleChannelProxy } from 'vs/platform/ipc/node/simpleIpcProxy';
|
||||
|
||||
export class ElectronService {
|
||||
|
||||
_serviceBrand: undefined;
|
||||
|
||||
constructor(@IMainProcessService mainProcessService: IMainProcessService) {
|
||||
return createSimpleMainChannelProxy<IElectronService>('electron', mainProcessService);
|
||||
return createSimpleChannelProxy<IElectronService>(mainProcessService.getChannel('electron'));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,19 +41,3 @@ export class MainProcessService extends Disposable implements IMainProcessServic
|
|||
this.mainProcessConnection.registerChannel(channelName, channel);
|
||||
}
|
||||
}
|
||||
|
||||
export function createSimpleMainChannelProxy<T>(channelName: string, mainProcessService: IMainProcessService): T {
|
||||
const channel = mainProcessService.getChannel(channelName);
|
||||
|
||||
return new Proxy({}, {
|
||||
get(_target, propKey, _receiver) {
|
||||
if (typeof propKey === 'string') {
|
||||
return function (...args: any[]) {
|
||||
return channel.call(propKey, ...args);
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Unable to provide main channel proxy implementation for: ${String(propKey)} in ${channelName}`);
|
||||
}
|
||||
}) as T;
|
||||
}
|
||||
|
|
48
src/vs/platform/ipc/node/simpleIpcProxy.ts
Normal file
48
src/vs/platform/ipc/node/simpleIpcProxy.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { Event } from 'vs/base/common/event';
|
||||
import { IChannel, IServerChannel } from 'vs/base/parts/ipc/common/ipc';
|
||||
|
||||
//
|
||||
// Use both `SimpleServiceProxyChannel` and `createSimpleChannelProxy`
|
||||
// for a very basic process <=> process communication over methods.
|
||||
//
|
||||
|
||||
export class SimpleServiceProxyChannel implements IServerChannel {
|
||||
|
||||
private service: { [key: string]: unknown };
|
||||
|
||||
constructor(service: unknown) {
|
||||
this.service = service as { [key: string]: unknown };
|
||||
}
|
||||
|
||||
listen<T>(_: unknown, event: string): Event<T> {
|
||||
throw new Error(`Events are currently unsupported by SimpleServiceProxyChannel: ${event}`);
|
||||
}
|
||||
|
||||
call(_: unknown, command: string, args: any[]): Promise<any> {
|
||||
const target = this.service[command];
|
||||
if (typeof target === 'function') {
|
||||
return target.apply(this.service, args);
|
||||
}
|
||||
|
||||
throw new Error(`Method not found: ${command}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function createSimpleChannelProxy<T>(channel: IChannel): T {
|
||||
return new Proxy({}, {
|
||||
get(_target, propKey, _receiver) {
|
||||
if (typeof propKey === 'string') {
|
||||
return function (...args: any[]) {
|
||||
return channel.call(propKey, args);
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`Unable to provide main channel proxy implementation for: ${String(propKey)}`);
|
||||
}
|
||||
}) as T;
|
||||
}
|
|
@ -4,13 +4,14 @@
|
|||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { IIssueService } from 'vs/platform/issue/node/issue';
|
||||
import { IMainProcessService, createSimpleMainChannelProxy } from 'vs/platform/ipc/electron-browser/mainProcessService';
|
||||
import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService';
|
||||
import { createSimpleChannelProxy } from 'vs/platform/ipc/node/simpleIpcProxy';
|
||||
|
||||
export class IssueService {
|
||||
|
||||
_serviceBrand: undefined;
|
||||
|
||||
constructor(@IMainProcessService mainProcessService: IMainProcessService) {
|
||||
return createSimpleMainChannelProxy<IIssueService>('issue', mainProcessService);
|
||||
return createSimpleChannelProxy<IIssueService>(mainProcessService.getChannel('issue'));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue