Skip to content

Commit d9ec15c

Browse files
author
Benjamin Pasero
committed
debt - introduce simple IPC helpers for basic communication forwarding using proxys
1 parent 6d7917c commit d9ec15c

7 files changed

Lines changed: 53 additions & 98 deletions

File tree

src/vs/base/parts/ipc/common/ipc.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export interface IServerChannel<TContext = string> {
3232
listen<T>(ctx: TContext, event: string, arg?: any): Event<T>;
3333
}
3434

35-
3635
export const enum RequestType {
3736
Promise = 100,
3837
PromiseCancel = 101,
@@ -833,3 +832,29 @@ export class StaticRouter<TContext = string> implements IClientRouter<TContext>
833832
return await this.route(hub);
834833
}
835834
}
835+
836+
export class SimpleServiceProxyChannel implements IServerChannel {
837+
838+
private service: { [key: string]: unknown };
839+
840+
constructor(service: unknown) {
841+
this.service = service as { [key: string]: unknown };
842+
}
843+
844+
listen<T>(_: unknown, event: string): Event<T> {
845+
throw new Error(`Events are currently unsupported by SimpleServiceProxyChannel: ${event}`);
846+
}
847+
848+
call(_: unknown, command: string, arg?: any): Promise<any> {
849+
const target = this.service[command];
850+
if (typeof target === 'function') {
851+
if (Array.isArray(arg)) {
852+
return target.apply(this.service, arg);
853+
}
854+
855+
return target.call(this.service, arg);
856+
}
857+
858+
throw new Error(`Call Not Found: ${command}`);
859+
}
860+
}

src/vs/code/electron-main/app.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { NullTelemetryService, combinedAppender, LogAppender } from 'vs/platform
3232
import { TelemetryAppenderClient } from 'vs/platform/telemetry/node/telemetryIpc';
3333
import { TelemetryService, ITelemetryServiceConfig } from 'vs/platform/telemetry/common/telemetryService';
3434
import { resolveCommonProperties } from 'vs/platform/telemetry/node/commonProperties';
35-
import { getDelayedChannel, StaticRouter } from 'vs/base/parts/ipc/common/ipc';
35+
import { getDelayedChannel, StaticRouter, SimpleServiceProxyChannel } from 'vs/base/parts/ipc/common/ipc';
3636
import product from 'vs/platform/product/common/product';
3737
import { ProxyAuthHandler } from 'vs/code/electron-main/auth';
3838
import { Disposable } from 'vs/base/common/lifecycle';
@@ -45,7 +45,6 @@ import { Win32UpdateService } from 'vs/platform/update/electron-main/updateServi
4545
import { LinuxUpdateService } from 'vs/platform/update/electron-main/updateService.linux';
4646
import { DarwinUpdateService } from 'vs/platform/update/electron-main/updateService.darwin';
4747
import { IIssueService } from 'vs/platform/issue/node/issue';
48-
import { IssueChannel } from 'vs/platform/issue/electron-main/issueIpc';
4948
import { IssueMainService } from 'vs/platform/issue/electron-main/issueMainService';
5049
import { LoggerChannel } from 'vs/platform/log/common/logIpc';
5150
import { setUnexpectedErrorHandler, onUnexpectedError } from 'vs/base/common/errors';
@@ -77,7 +76,7 @@ import { IFileService } from 'vs/platform/files/common/files';
7776
import { DiskFileSystemProvider } from 'vs/platform/files/node/diskFileSystemProvider';
7877
import { ExtensionHostDebugBroadcastChannel } from 'vs/platform/debug/common/extensionHostDebugIpc';
7978
import { IElectronService } from 'vs/platform/electron/node/electron';
80-
import { ElectronMainService, ElectronChannel } from 'vs/platform/electron/electron-main/electronMainService';
79+
import { ElectronMainService } from 'vs/platform/electron/electron-main/electronMainService';
8180

8281
export class CodeApplication extends Disposable {
8382

@@ -539,11 +538,11 @@ export class CodeApplication extends Disposable {
539538
electronIpcServer.registerChannel('update', updateChannel);
540539

541540
const issueService = accessor.get(IIssueService);
542-
const issueChannel = new IssueChannel(issueService);
541+
const issueChannel = new SimpleServiceProxyChannel(issueService);
543542
electronIpcServer.registerChannel('issue', issueChannel);
544543

545544
const electronService = accessor.get(IElectronService);
546-
const electronChannel = new ElectronChannel(electronService);
545+
const electronChannel = new SimpleServiceProxyChannel(electronService);
547546
electronIpcServer.registerChannel('electron', electronChannel);
548547

549548
const workspacesMainService = accessor.get(IWorkspacesMainService);

src/vs/platform/electron/electron-browser/electronService.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService';
6+
import { IElectronService } from 'vs/platform/electron/node/electron';
7+
import { IMainProcessService, createSimpleMainChannelProxy } from 'vs/platform/ipc/electron-browser/mainProcessService';
78

89
export class ElectronService {
910

1011
_serviceBrand: undefined;
1112

1213
constructor(@IMainProcessService mainProcessService: IMainProcessService) {
13-
const channel = mainProcessService.getChannel('electron');
14-
15-
// Proxy: forward any property access to the channel
16-
return new Proxy({}, {
17-
get(_target, propKey, _receiver) {
18-
if (typeof propKey === 'string') {
19-
return function (...args: any[]) {
20-
return channel.call(propKey, ...args);
21-
};
22-
}
23-
24-
throw new Error(`Not Implemented in ElectronService: ${String(propKey)}`);
25-
}
26-
}) as ElectronService;
14+
return createSimpleMainChannelProxy<IElectronService>('electron', mainProcessService);
2715
}
2816
}

src/vs/platform/electron/electron-main/electronMainService.ts

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import { IElectronService } from 'vs/platform/electron/node/electron';
77
import { IWindowsMainService, ICodeWindow } from 'vs/platform/windows/electron-main/windows';
88
import { MessageBoxOptions, MessageBoxReturnValue, shell } from 'electron';
9-
import { IServerChannel } from 'vs/base/parts/ipc/common/ipc';
10-
import { Event } from 'vs/base/common/event';
119

1210
export class ElectronMainService implements IElectronService {
1311

@@ -35,29 +33,3 @@ export class ElectronMainService implements IElectronService {
3533
shell.showItemInFolder(path);
3634
}
3735
}
38-
39-
export class ElectronChannel implements IServerChannel {
40-
41-
private service: { [key: string]: unknown };
42-
43-
constructor(service: IElectronService) {
44-
this.service = service as unknown as { [key: string]: unknown };
45-
}
46-
47-
listen<T>(_: unknown, event: string): Event<T> {
48-
throw new Error(`Event not found: ${event}`);
49-
}
50-
51-
call(_: unknown, command: string, arg?: any): Promise<any> {
52-
const target = this.service[command];
53-
if (typeof target === 'function') {
54-
if (Array.isArray(arg)) {
55-
return target.apply(this.service, arg);
56-
}
57-
58-
return target.call(this.service, arg);
59-
}
60-
61-
throw new Error(`Call Not Found in ElectronService: ${command}`);
62-
}
63-
}

src/vs/platform/ipc/electron-browser/mainProcessService.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ export class MainProcessService extends Disposable implements IMainProcessServic
4141
this.mainProcessConnection.registerChannel(channelName, channel);
4242
}
4343
}
44+
45+
export function createSimpleMainChannelProxy<T>(channelName: string, mainProcessService: IMainProcessService): T {
46+
const channel = mainProcessService.getChannel(channelName);
47+
48+
return new Proxy({}, {
49+
get(_target, propKey, _receiver) {
50+
if (typeof propKey === 'string') {
51+
return function (...args: any[]) {
52+
return channel.call(propKey, ...args);
53+
};
54+
}
55+
56+
throw new Error(`Unable to provide main channel proxy implementation for: ${String(propKey)} in ${channelName}`);
57+
}
58+
}) as T;
59+
}

src/vs/platform/issue/electron-browser/issueService.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,14 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
7-
import { IIssueService, IssueReporterData, ProcessExplorerData } from 'vs/platform/issue/node/issue';
8-
import { IMainProcessService } from 'vs/platform/ipc/electron-browser/mainProcessService';
6+
import { IIssueService } from 'vs/platform/issue/node/issue';
7+
import { IMainProcessService, createSimpleMainChannelProxy } from 'vs/platform/ipc/electron-browser/mainProcessService';
98

10-
export class IssueService implements IIssueService {
9+
export class IssueService {
1110

1211
_serviceBrand: undefined;
1312

14-
private channel: IChannel;
15-
1613
constructor(@IMainProcessService mainProcessService: IMainProcessService) {
17-
this.channel = mainProcessService.getChannel('issue');
18-
}
19-
20-
openReporter(data: IssueReporterData): Promise<void> {
21-
return this.channel.call('openIssueReporter', data);
22-
}
23-
24-
openProcessExplorer(data: ProcessExplorerData): Promise<void> {
25-
return this.channel.call('openProcessExplorer', data);
26-
}
27-
28-
getSystemStatus(): Promise<string> {
29-
return this.channel.call('getSystemStatus');
14+
return createSimpleMainChannelProxy<IIssueService>('issue', mainProcessService);
3015
}
3116
}

src/vs/platform/issue/electron-main/issueIpc.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)