Skip to content

Commit 511069b

Browse files
committed
refactor: update code
1 parent b133b91 commit 511069b

18 files changed

Lines changed: 173 additions & 143 deletions

File tree

packages/connection/__test__/node/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import http from 'http';
33
import WebSocket from 'ws';
44

55
import { WSWebSocketConnection } from '@opensumi/ide-connection/lib/common/connection';
6-
import { SimpleConnection } from '@opensumi/ide-connection/lib/common/connection/drivers/empty';
76
import { Connection } from '@opensumi/ide-connection/lib/common/rpc/connection';
87
import { Deferred, Emitter, Uri } from '@opensumi/ide-core-common';
98

109
import { createMockPairRPCProtocol } from '../../../extension/__mocks__/initRPCProtocol';
1110
import { RPCService } from '../../src';
1211
import { RPCServiceCenter, initRPCService } from '../../src/common';
12+
import { SimpleConnection } from '../../src/common/connection/drivers/simple';
1313
import { RPCProtocol, createMainContextProxyIdentifier } from '../../src/common/ext-rpc-protocol';
1414
import { WSChannel, parse } from '../../src/common/ws-channel';
1515
import { WebSocketServerRoute, CommonChannelHandler, commonChannelPathHandler } from '../../src/node';
@@ -68,7 +68,7 @@ describe('connection', () => {
6868
const msgObj = parse(msg);
6969
if (msgObj.kind === 'server-ready') {
7070
if (msgObj.id === 'TEST_CHANNEL_ID') {
71-
channel.handleMessage(msgObj);
71+
channel.dispatchChannelMessage(msgObj);
7272
}
7373
}
7474
});
@@ -270,7 +270,7 @@ describe('connection', () => {
270270
await expect(timeoutBProtocol.getProxy(testTimeoutIdentifier).$test()).resolves.toBe(undefined);
271271

272272
await expect(timeoutCProtocol.getProxy(testTimeoutIdentifier).$test()).rejects.toThrow(
273-
'method testTimeoutIdentifier timeout',
273+
'method testTimeoutIdentifier/$test timeout',
274274
);
275275
});
276276
});

packages/connection/src/browser/ws-channel-handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class WSChannelHandler {
7272

7373
const channel = this.channelMap.get(msg.id);
7474
if (channel) {
75-
channel.handleMessage(msg);
75+
channel.dispatchChannelMessage(msg);
7676
} else {
7777
this.logger.warn(this.LOG_TAG, `channel ${msg.id} not found`);
7878
}

packages/connection/src/common/connection/drivers/message-channel.ts renamed to packages/connection/src/common/connection/drivers/message-port.ts

File renamed without changes.

packages/connection/src/common/connection/drivers/empty.ts renamed to packages/connection/src/common/connection/drivers/simple.ts

File renamed without changes.

packages/connection/src/common/ext-rpc-protocol.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ export interface IRPCProtocol {
3939
get<T>(identifier: ProxyIdentifier<T>): T;
4040
}
4141

42+
const SEP = '/';
43+
44+
function getRPCName(serviceId: string, methodName: string) {
45+
return `${serviceId}${SEP}${methodName}`;
46+
}
47+
48+
function extractServiceAndMethod(rpcId: string): [string, string] {
49+
const idx = rpcId.indexOf(SEP);
50+
return [rpcId.substring(0, idx), rpcId.substring(idx + 1)];
51+
}
52+
4253
export class RPCProtocol implements IRPCProtocol {
4354
private readonly _locals: Map<string, any>;
4455
private readonly _proxies: Map<string, any>;
@@ -52,7 +63,10 @@ export class RPCProtocol implements IRPCProtocol {
5263

5364
connection.listen();
5465

55-
this.connection.onRequestNotFound((rpcId: string, args: any[]) => this._doInvokeHandler(rpcId, args[0], args[1]));
66+
this.connection.onRequestNotFound((rpcName: string, args: any[]) => {
67+
const [rpcId, methodName] = extractServiceAndMethod(rpcName);
68+
return this._doInvokeHandler(rpcId, methodName, args);
69+
});
5670
}
5771

5872
public set<T>(identifier: ProxyIdentifier<T>, instance: any) {
@@ -78,8 +92,10 @@ export class RPCProtocol implements IRPCProtocol {
7892
if (typeof name === 'symbol') {
7993
return null;
8094
}
95+
// charCodeAt(0) === 36 means starts with $
8196
if (!target[name] && name.charCodeAt(0) === 36) {
82-
target[name] = (...args: any[]) => this.connection.sendRequest(rpcId, name, args);
97+
const rpcName = getRPCName(rpcId, name);
98+
target[name] = (...args: any[]) => this.connection.sendRequest(rpcName, args);
8399
}
84100

85101
return target[name];
@@ -112,6 +128,5 @@ export function createRPCProtocol(channel: WSChannel, options: RPCProtocolCreate
112128
timeout: options.timeout,
113129
});
114130

115-
const mainThreadProtocol = new RPCProtocol(connection);
116-
return mainThreadProtocol;
131+
return new RPCProtocol(connection);
117132
}

packages/connection/src/common/proxy/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export * from './legacy';
2+
export * from './runner';
3+
export * from './sumi';
4+
export * from './invoker';
25

36
export abstract class RPCService<T = any> {
47
rpcClient?: T[];
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ProtocolRepository } from '../rpc/protocol-repository';
2+
import { ILogger } from '../types';
3+
import { WSChannel } from '../ws-channel';
4+
5+
import { ProxyLegacy } from './legacy';
6+
import { ServiceRunner } from './runner';
7+
import { ProxySumi } from './sumi';
8+
9+
const defaultReservedWordSet = new Set(['then']);
10+
11+
export class Invoker {
12+
legacyProxy: ProxyLegacy;
13+
sumiProxy: ProxySumi;
14+
15+
private legacyInvokeProxy: any;
16+
private sumiInvokeProxy: any;
17+
18+
forceUseSumi = true;
19+
20+
constructor(protected repo: ProtocolRepository, public runner: ServiceRunner, channel: WSChannel, logger?: ILogger) {
21+
this.legacyProxy = new ProxyLegacy(runner, logger);
22+
this.legacyInvokeProxy = this.legacyProxy.getInvokeProxy();
23+
24+
this.sumiProxy = new ProxySumi(runner, logger);
25+
this.sumiInvokeProxy = this.sumiProxy.getInvokeProxy();
26+
27+
this.listen(channel);
28+
}
29+
30+
listen(channel: WSChannel) {
31+
const messageConnection = channel.createMessageConnection();
32+
this.legacyProxy.listen(messageConnection);
33+
34+
const connection = channel.createConnection();
35+
connection.setProtocolRepository(this.repo);
36+
this.sumiProxy.listen(connection);
37+
}
38+
39+
invoke(name: string, ...args: any[]) {
40+
if (defaultReservedWordSet.has(name) || typeof name === 'symbol') {
41+
return Promise.resolve();
42+
}
43+
44+
if (this.forceUseSumi) {
45+
return this.sumiInvokeProxy[name](...args);
46+
}
47+
48+
if (this.repo.has(name)) {
49+
return this.sumiInvokeProxy[name](...args);
50+
}
51+
52+
return this.legacyInvokeProxy[name](...args);
53+
}
54+
55+
dispose() {
56+
this.legacyProxy.dispose();
57+
this.sumiProxy.dispose();
58+
}
59+
}

packages/connection/src/common/rpc-service/center.ts

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,14 @@
11
import { Deferred } from '@opensumi/ide-core-common';
22

33
import { METHOD_NOT_REGISTERED } from '../constants';
4-
import { ProxyLegacy } from '../proxy';
5-
import { ServiceRunner } from '../proxy/runner';
6-
import { ProxySumi } from '../proxy/sumi';
7-
import { TSumiProtocol } from '../rpc';
8-
import { ProtocolRepository } from '../rpc/protocol-repository';
4+
import { ServiceRunner, Invoker } from '../proxy';
5+
import { TSumiProtocol, ProtocolRepository } from '../rpc';
96
import { IBench, ILogger, RPCServiceMethod, ServiceType } from '../types';
107
import { getMethodName } from '../utils';
118
import { WSChannel } from '../ws-channel';
129

1310
const safeProcess: { pid: string } = typeof process === 'undefined' ? { pid: 'mock' } : (process as any);
1411

15-
const defaultReservedWordSet = new Set(['then']);
16-
17-
class Invoker {
18-
legacyProxy: ProxyLegacy;
19-
sumiProxy: ProxySumi;
20-
21-
private legacyInvokeProxy: any;
22-
private sumiInvokeProxy: any;
23-
24-
forceUseSumi = true;
25-
26-
constructor(protected repo: ProtocolRepository, public runner: ServiceRunner, channel: WSChannel, logger?: ILogger) {
27-
this.legacyProxy = new ProxyLegacy(runner, logger);
28-
this.legacyInvokeProxy = this.legacyProxy.getInvokeProxy();
29-
30-
this.sumiProxy = new ProxySumi(runner, logger);
31-
this.sumiInvokeProxy = this.sumiProxy.getInvokeProxy();
32-
33-
this.listen(channel);
34-
}
35-
36-
listen(channel: WSChannel) {
37-
const messageConnection = channel.createMessageConnection();
38-
this.legacyProxy.listen(messageConnection);
39-
40-
const connection = channel.createConnection();
41-
connection.setProtocolRepository(this.repo);
42-
this.sumiProxy.listen(connection);
43-
}
44-
45-
invoke(name: string, ...args: any[]) {
46-
if (defaultReservedWordSet.has(name) || typeof name === 'symbol') {
47-
return Promise.resolve();
48-
}
49-
50-
if (this.forceUseSumi) {
51-
return this.sumiInvokeProxy[name](...args);
52-
}
53-
54-
if (this.repo.has(name)) {
55-
return this.sumiInvokeProxy[name](...args);
56-
}
57-
58-
return this.legacyInvokeProxy[name](...args);
59-
}
60-
61-
dispose() {
62-
this.legacyProxy.dispose();
63-
this.sumiProxy.dispose();
64-
}
65-
}
66-
6712
export class RPCServiceCenter {
6813
public uid: string;
6914

@@ -151,8 +96,8 @@ export class RPCServiceCenter {
15196
}
15297

15398
// FIXME: this is an unreasonable design, if remote service only returned doubtful result, we will return an empty array.
154-
// but actually we should throw an error to tell user that no remote service can handle this call.
155-
// or just return `undefined`.
99+
// but actually we should throw an error to tell user that no remote service can handle this call.
100+
// or just return `undefined`.
156101
return result.length === 1 ? result[0] : result;
157102
}
158103
}

0 commit comments

Comments
 (0)