Skip to content

Commit e8fe7d2

Browse files
committed
Rename JsonRpcProxyFactory and related types
- Rename `JsonRpcProxyFactory` and related types to `RpcProxyFactory` (Rpc*). The naming scheme was a remainder of the old vscode jsonr-rpc based protocol. By simply using the `Rpc` suffix the class names are less misleading and protocol agnostic. - Keep deprecated declarations of the old `JsonRpc*` namespace. The components are heavily used by adopters so we should maintain this deprecated symbols for a while to enable graceful migration without hard API breaks. Naturally this is open for discussion, but judging from the files I had to touch in the core framework alone I think it's definitely a good idea to give adopters a grace period. Complementary website PR: eclipse-theia/theia-website#422 Fixes #12581 Contributed on behalf of STMicroelectronics
1 parent 53b1987 commit e8fe7d2

File tree

42 files changed

+184
-150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+184
-150
lines changed

examples/api-samples/src/common/updater/sample-updater.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//
1414
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
// *****************************************************************************
16-
import { JsonRpcServer } from '@theia/core/lib/common/messaging/proxy-factory';
16+
import { RpcServer } from '@theia/core/lib/common/messaging/proxy-factory';
1717

1818
export enum UpdateStatus {
1919
InProgress = 'in-progress',
@@ -23,7 +23,7 @@ export enum UpdateStatus {
2323

2424
export const SampleUpdaterPath = '/services/sample-updater';
2525
export const SampleUpdater = Symbol('SampleUpdater');
26-
export interface SampleUpdater extends JsonRpcServer<SampleUpdaterClient> {
26+
export interface SampleUpdater extends RpcServer<SampleUpdaterClient> {
2727
checkForUpdates(): Promise<{ status: UpdateStatus }>;
2828
onRestartToUpdateRequested(): void;
2929
disconnectClient(client: SampleUpdaterClient): void;

examples/api-samples/src/electron-main/update/sample-updater-main-module.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// *****************************************************************************
1616

1717
import { ContainerModule } from '@theia/core/shared/inversify';
18-
import { JsonRpcConnectionHandler } from '@theia/core/lib/common/messaging/proxy-factory';
18+
import { RpcConnectionHandler } from '@theia/core/lib/common/messaging/proxy-factory';
1919
import { ElectronMainApplicationContribution } from '@theia/core/lib/electron-main/electron-main-application';
2020
import { ElectronConnectionHandler } from '@theia/core/lib/electron-common/messaging/electron-connection-handler';
2121
import { SampleUpdaterPath, SampleUpdater, SampleUpdaterClient } from '../../common/updater/sample-updater';
@@ -26,7 +26,7 @@ export default new ContainerModule(bind => {
2626
bind(SampleUpdater).toService(SampleUpdaterImpl);
2727
bind(ElectronMainApplicationContribution).toService(SampleUpdater);
2828
bind(ElectronConnectionHandler).toDynamicValue(context =>
29-
new JsonRpcConnectionHandler<SampleUpdaterClient>(SampleUpdaterPath, client => {
29+
new RpcConnectionHandler<SampleUpdaterClient>(SampleUpdaterPath, client => {
3030
const server = context.container.get<SampleUpdater>(SampleUpdater);
3131
server.setClient(client);
3232
client.onDidCloseConnection(() => server.disconnectClient(client));

packages/core/src/browser/messaging/ws-connection-provider.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
// *****************************************************************************
1616

1717
import { injectable, interfaces, decorate, unmanaged } from 'inversify';
18-
import { JsonRpcProxyFactory, JsonRpcProxy, Emitter, Event, Channel } from '../../common';
18+
import { RpcProxyFactory, RpcProxy, Emitter, Event, Channel } from '../../common';
1919
import { Endpoint } from '../endpoint';
2020
import { AbstractConnectionProvider } from '../../common/messaging/abstract-connection-provider';
2121
import { io, Socket } from 'socket.io-client';
2222
import { IWebSocket, WebSocketChannel } from '../../common/messaging/web-socket-channel';
2323

24-
decorate(injectable(), JsonRpcProxyFactory);
25-
decorate(unmanaged(), JsonRpcProxyFactory, 0);
24+
decorate(injectable(), RpcProxyFactory);
25+
decorate(unmanaged(), RpcProxyFactory, 0);
2626

2727
export interface WebSocketOptions {
2828
/**
@@ -44,7 +44,7 @@ export class WebSocketConnectionProvider extends AbstractConnectionProvider<WebS
4444
return this.onSocketDidCloseEmitter.event;
4545
}
4646

47-
static override createProxy<T extends object>(container: interfaces.Container, path: string, arg?: object): JsonRpcProxy<T> {
47+
static override createProxy<T extends object>(container: interfaces.Container, path: string, arg?: object): RpcProxy<T> {
4848
return container.get(WebSocketConnectionProvider).createProxy<T>(path, arg);
4949
}
5050

packages/core/src/common/logger-protocol.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
// *****************************************************************************
1616

1717
import { injectable } from 'inversify';
18-
import { JsonRpcServer } from './messaging/proxy-factory';
18+
import { RpcServer } from './messaging/proxy-factory';
1919

2020
export const ILoggerServer = Symbol('ILoggerServer');
2121

2222
export const loggerPath = '/services/logger';
2323

24-
export interface ILoggerServer extends JsonRpcServer<ILoggerClient> {
24+
export interface ILoggerServer extends RpcServer<ILoggerClient> {
2525
setLogLevel(name: string, logLevel: number): Promise<void>;
2626
getLogLevel(name: string): Promise<number>;
2727
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages/core/src/common/messaging/abstract-connection-provider.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { injectable, interfaces } from 'inversify';
1818
import { Emitter, Event } from '../event';
1919
import { ConnectionHandler } from './handler';
20-
import { JsonRpcProxy, JsonRpcProxyFactory } from './proxy-factory';
20+
import { RpcProxy, RpcProxyFactory } from './proxy-factory';
2121
import { Channel, ChannelMultiplexer } from '../message-rpc/channel';
2222

2323
/**
@@ -32,15 +32,15 @@ export abstract class AbstractConnectionProvider<AbstractOptions extends object>
3232
* Create a proxy object to remote interface of T type
3333
* over an electron ipc connection for the given path and proxy factory.
3434
*/
35-
static createProxy<T extends object>(container: interfaces.Container, path: string, factory: JsonRpcProxyFactory<T>): JsonRpcProxy<T>;
35+
static createProxy<T extends object>(container: interfaces.Container, path: string, factory: RpcProxyFactory<T>): RpcProxy<T>;
3636
/**
3737
* Create a proxy object to remote interface of T type
3838
* over an electron ipc connection for the given path.
3939
*
4040
* An optional target can be provided to handle
4141
* notifications and requests from a remote side.
4242
*/
43-
static createProxy<T extends object>(container: interfaces.Container, path: string, target?: object): JsonRpcProxy<T> {
43+
static createProxy<T extends object>(container: interfaces.Container, path: string, target?: object): RpcProxy<T> {
4444
throw new Error('abstract');
4545
}
4646

@@ -53,17 +53,17 @@ export abstract class AbstractConnectionProvider<AbstractOptions extends object>
5353
* Create a proxy object to remote interface of T type
5454
* over a web socket connection for the given path and proxy factory.
5555
*/
56-
createProxy<T extends object>(path: string, factory: JsonRpcProxyFactory<T>): JsonRpcProxy<T>;
56+
createProxy<T extends object>(path: string, factory: RpcProxyFactory<T>): RpcProxy<T>;
5757
/**
5858
* Create a proxy object to remote interface of T type
5959
* over a web socket connection for the given path.
6060
*
6161
* An optional target can be provided to handle
6262
* notifications and requests from a remote side.
6363
*/
64-
createProxy<T extends object>(path: string, target?: object): JsonRpcProxy<T>;
65-
createProxy<T extends object>(path: string, arg?: object): JsonRpcProxy<T> {
66-
const factory = arg instanceof JsonRpcProxyFactory ? arg : new JsonRpcProxyFactory<T>(arg);
64+
createProxy<T extends object>(path: string, target?: object): RpcProxy<T>;
65+
createProxy<T extends object>(path: string, arg?: object): RpcProxy<T> {
66+
const factory = arg instanceof RpcProxyFactory ? arg : new RpcProxyFactory<T>(arg);
6767
this.listen({
6868
path,
6969
onConnection: c => factory.listen(c)

packages/core/src/common/messaging/proxy-factory.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// *****************************************************************************
1616

1717
import * as chai from 'chai';
18-
import { JsonRpcProxyFactory, JsonRpcProxy } from './proxy-factory';
18+
import { RpcProxyFactory, RpcProxy } from './proxy-factory';
1919
import { ChannelPipe } from '../message-rpc/channel.spec';
2020

2121
const expect = chai.expect;
@@ -84,19 +84,19 @@ describe('Proxy-Factory', () => {
8484

8585
function getSetup(): {
8686
client: TestClient;
87-
clientProxy: JsonRpcProxy<TestClient>;
87+
clientProxy: RpcProxy<TestClient>;
8888
server: TestServer;
89-
serverProxy: JsonRpcProxy<TestServer>;
89+
serverProxy: RpcProxy<TestServer>;
9090
} {
9191
const client = new TestClient();
9292
const server = new TestServer();
9393

94-
const serverProxyFactory = new JsonRpcProxyFactory<TestServer>(client);
94+
const serverProxyFactory = new RpcProxyFactory<TestServer>(client);
9595
const pipe = new ChannelPipe();
9696
serverProxyFactory.listen(pipe.right);
9797
const serverProxy = serverProxyFactory.createProxy();
9898

99-
const clientProxyFactory = new JsonRpcProxyFactory<TestClient>(server);
99+
const clientProxyFactory = new RpcProxyFactory<TestClient>(server);
100100
clientProxyFactory.listen(pipe.left);
101101
const clientProxy = clientProxyFactory.createProxy();
102102
return {

packages/core/src/common/messaging/proxy-factory.ts

+73-38
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,29 @@ import { Channel } from '../message-rpc/channel';
2424
import { RequestHandler, RpcProtocol } from '../message-rpc/rpc-protocol';
2525
import { ConnectionHandler } from './handler';
2626
import { Deferred } from '../promise-util';
27+
import { decorate, injectable, unmanaged } from '../../../shared/inversify';
2728

28-
export type JsonRpcServer<Client> = Disposable & {
29+
export type RpcServer<Client> = Disposable & {
2930
/**
3031
* If this server is a proxy to a remote server then
3132
* a client is used as a local object
32-
* to handle JSON-RPC messages from the remote server.
33+
* to handle RPC messages from the remote server.
3334
*/
3435
setClient(client: Client | undefined): void;
3536
getClient?(): Client | undefined;
3637
};
3738

38-
export interface JsonRpcConnectionEventEmitter {
39+
export interface RpcConnectionEventEmitter {
3940
readonly onDidOpenConnection: Event<void>;
4041
readonly onDidCloseConnection: Event<void>;
4142
}
42-
export type JsonRpcProxy<T> = T & JsonRpcConnectionEventEmitter;
43+
export type RpcProxy<T> = T & RpcConnectionEventEmitter;
4344

44-
export class JsonRpcConnectionHandler<T extends object> implements ConnectionHandler {
45+
export class RpcConnectionHandler<T extends object> implements ConnectionHandler {
4546
constructor(
4647
readonly path: string,
47-
readonly targetFactory: (proxy: JsonRpcProxy<T>) => any,
48-
readonly factoryConstructor: new () => JsonRpcProxyFactory<T> = JsonRpcProxyFactory
48+
readonly targetFactory: (proxy: RpcProxy<T>) => any,
49+
readonly factoryConstructor: new () => RpcProxyFactory<T> = RpcProxyFactory
4950
) { }
5051

5152
onConnection(connection: Channel): void {
@@ -63,12 +64,12 @@ export type RpcProtocolFactory = (channel: Channel, requestHandler: RequestHandl
6364
const defaultRpcProtocolFactory: RpcProtocolFactory = (channel, requestHandler) => new RpcProtocol(channel, requestHandler);
6465

6566
/**
66-
* Factory for JSON-RPC proxy objects.
67+
* Factory for RPC proxy objects.
6768
*
68-
* A JSON-RPC proxy exposes the programmatic interface of an object through
69-
* JSON-RPC. This allows remote programs to call methods of this objects by
70-
* sending JSON-RPC requests. This takes place over a bi-directional stream,
71-
* where both ends can expose an object and both can call methods each other's
69+
* A RPC proxy exposes the programmatic interface of an object through
70+
* Theia's RPC protocol. This allows remote programs to call methods of this objects by
71+
* sending RPC requests. This takes place over a bi-directional stream,
72+
* where both ends can expose an object and both can call methods on each other'
7273
* exposed object.
7374
*
7475
* For example, assuming we have an object of the following type on one end:
@@ -77,45 +78,45 @@ const defaultRpcProtocolFactory: RpcProtocolFactory = (channel, requestHandler)
7778
* bar(baz: number): number { return baz + 1 }
7879
* }
7980
*
80-
* which we want to expose through a JSON-RPC interface. We would do:
81+
* which we want to expose through a RPC interface. We would do:
8182
*
8283
* let target = new Foo()
83-
* let factory = new JsonRpcProxyFactory<Foo>('/foo', target)
84+
* let factory = new RpcProxyFactory<Foo>('/foo', target)
8485
* factory.onConnection(connection)
8586
*
8687
* The party at the other end of the `connection`, in order to remotely call
8788
* methods on this object would do:
8889
*
89-
* let factory = new JsonRpcProxyFactory<Foo>('/foo')
90+
* let factory = new RpcProxyFactory<Foo>('/foo')
9091
* factory.onConnection(connection)
9192
* let proxy = factory.createProxy();
9293
* let result = proxy.bar(42)
9394
* // result is equal to 43
9495
*
9596
* One the wire, it would look like this:
9697
*
97-
* --> {"jsonrpc": "2.0", "id": 0, "method": "bar", "params": {"baz": 42}}
98-
* <-- {"jsonrpc": "2.0", "id": 0, "result": 43}
99-
*
98+
* --> { "type":"1", "id": 1, "method": "bar", "args": [42]}
99+
* <-- { "type":"3", "id": 1, "res": 43}
100+
*
100101
* Note that in the code of the caller, we didn't pass a target object to
101-
* JsonRpcProxyFactory, because we don't want/need to expose an object.
102+
* RpcProxyFactory, because we don't want/need to expose an object.
102103
* If we had passed a target object, the other side could've called methods on
103104
* it.
104105
*
105-
* @param <T> - The type of the object to expose to JSON-RPC.
106+
* @param <T> - The type of the object to expose to RPC.
106107
*/
107108

108-
export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
109+
export class RpcProxyFactory<T extends object> implements ProxyHandler<T> {
109110

110111
protected readonly onDidOpenConnectionEmitter = new Emitter<void>();
111112
protected readonly onDidCloseConnectionEmitter = new Emitter<void>();
112113

113114
protected rpcDeferred: Deferred<RpcProtocol>;
114115

115116
/**
116-
* Build a new JsonRpcProxyFactory.
117+
* Build a new RpcProxyFactory.
117118
*
118-
* @param target - The object to expose to JSON-RPC methods calls. If this
119+
* @param target - The object to expose to RPC methods calls. If this
119120
* is omitted, the proxy won't be able to handle requests, only send them.
120121
*/
121122
constructor(public target?: any, protected rpcProtocolFactory = defaultRpcProtocolFactory) {
@@ -135,10 +136,10 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
135136
}
136137

137138
/**
138-
* Connect a MessageConnection to the factory.
139+
* Connect a {@link Channel} to the factory by creating an {@link RpcProtocol} on top of it.
139140
*
140-
* This connection will be used to send/receive JSON-RPC requests and
141-
* response.
141+
* This protocol will be used to send/receive RPC requests and
142+
* responses.
142143
*/
143144
listen(channel: Channel): void {
144145
const protocol = this.rpcProtocolFactory(channel, (meth, args) => this.onRequest(meth, ...args));
@@ -148,9 +149,9 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
148149
}
149150

150151
/**
151-
* Process an incoming JSON-RPC method call.
152+
* Process an incoming RPC method call.
152153
*
153-
* onRequest is called when the JSON-RPC connection received a method call
154+
* onRequest is called when the RPC connection received a method call
154155
* request. It calls the corresponding method on [[target]].
155156
*
156157
* The return value is a Promise object that is resolved with the return
@@ -179,7 +180,7 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
179180
}
180181

181182
/**
182-
* Process an incoming JSON-RPC notification.
183+
* Process an incoming RPC notification.
183184
*
184185
* Same as [[onRequest]], but called on incoming notifications rather than
185186
* methods calls.
@@ -192,37 +193,37 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
192193

193194
/**
194195
* Create a Proxy exposing the interface of an object of type T. This Proxy
195-
* can be used to do JSON-RPC method calls on the remote target object as
196+
* can be used to do RPC method calls on the remote target object as
196197
* if it was local.
197198
*
198-
* If `T` implements `JsonRpcServer` then a client is used as a target object for a remote target object.
199+
* If `T` implements `RpcServer` then a client is used as a target object for a remote target object.
199200
*/
200-
createProxy(): JsonRpcProxy<T> {
201+
createProxy(): RpcProxy<T> {
201202
const result = new Proxy<T>(this as any, this);
202203
return result as any;
203204
}
204205

205206
/**
206-
* Get a callable object that executes a JSON-RPC method call.
207+
* Get a callable object that executes a RPC method call.
207208
*
208209
* Getting a property on the Proxy object returns a callable that, when
209-
* called, executes a JSON-RPC call. The name of the property defines the
210+
* called, executes a RPC call. The name of the property defines the
210211
* method to be called. The callable takes a variable number of arguments,
211-
* which are passed in the JSON-RPC method call.
212+
* which are passed in the RPC method call.
212213
*
213214
* For example, if you have a Proxy object:
214215
*
215-
* let fooProxyFactory = JsonRpcProxyFactory<Foo>('/foo')
216+
* let fooProxyFactory = RpcProxyFactory<Foo>('/foo')
216217
* let fooProxy = fooProxyFactory.createProxy()
217218
*
218219
* accessing `fooProxy.bar` will return a callable that, when called,
219-
* executes a JSON-RPC method call to method `bar`. Therefore, doing
220+
* executes a RPC method call to method `bar`. Therefore, doing
220221
* `fooProxy.bar()` will call the `bar` method on the remote Foo object.
221222
*
222223
* @param target - unused.
223224
* @param p - The property accessed on the Proxy object.
224225
* @param receiver - unused.
225-
* @returns A callable that executes the JSON-RPC call.
226+
* @returns A callable that executes the RPC call.
226227
*/
227228
get(target: T, p: PropertyKey, receiver: any): any {
228229
if (p === 'setClient') {
@@ -306,3 +307,37 @@ export class JsonRpcProxyFactory<T extends object> implements ProxyHandler<T> {
306307

307308
}
308309

310+
/**
311+
* @deprecated since 1.39.0 use `RpcConnectionEventEmitter` instead
312+
*/
313+
export type JsonRpcConnectionEventEmitter = RpcConnectionEventEmitter;
314+
315+
/**
316+
* @deprecated since 1.39.0 use `RpcServer` instead
317+
*/
318+
export type JsonRpcServer<Client> = RpcServer<Client>;
319+
320+
/**
321+
* @deprecated since 1.39.0 use `RpcProxy` instead
322+
*/
323+
export type JsonRpcProxy<T> = RpcProxy<T>;
324+
325+
/**
326+
* @deprecated since 1.39.0 use `RpcConnectionHandler` instead
327+
*/
328+
export class JsonRpcConnectionHandler<T extends object> extends RpcConnectionHandler<T> {
329+
330+
}
331+
332+
/**
333+
* @deprecated since 1.39.0 use `RpcProxyFactory` instead
334+
*/
335+
export class JsonRpcProxyFactory<T extends object> extends RpcProxyFactory<T> {
336+
337+
}
338+
339+
// eslint-disable-next-line deprecation/deprecation
340+
decorate(injectable(), JsonRpcProxyFactory);
341+
// eslint-disable-next-line deprecation/deprecation
342+
decorate(unmanaged(), JsonRpcProxyFactory, 0);
343+

0 commit comments

Comments
 (0)