@opensumi/ide-connection 2.27.3-next-1706686746.0 → 2.27.3-rc-1706687185.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/lib/browser/ws-channel-handler.d.ts.map +1 -1
  2. package/lib/browser/ws-channel-handler.js +4 -0
  3. package/lib/browser/ws-channel-handler.js.map +1 -1
  4. package/lib/common/connection/drivers/base.d.ts +1 -0
  5. package/lib/common/connection/drivers/base.d.ts.map +1 -1
  6. package/lib/common/connection/drivers/base.js +4 -0
  7. package/lib/common/connection/drivers/base.js.map +1 -1
  8. package/lib/common/ext-rpc-protocol.d.ts.map +1 -1
  9. package/lib/common/ext-rpc-protocol.js +2 -3
  10. package/lib/common/ext-rpc-protocol.js.map +1 -1
  11. package/lib/common/proxy/base.d.ts +8 -16
  12. package/lib/common/proxy/base.d.ts.map +1 -1
  13. package/lib/common/proxy/base.js +21 -62
  14. package/lib/common/proxy/base.js.map +1 -1
  15. package/lib/common/proxy/legacy.d.ts +7 -3
  16. package/lib/common/proxy/legacy.d.ts.map +1 -1
  17. package/lib/common/proxy/legacy.js +135 -88
  18. package/lib/common/proxy/legacy.js.map +1 -1
  19. package/lib/common/rpc-service/center.d.ts +1 -5
  20. package/lib/common/rpc-service/center.d.ts.map +1 -1
  21. package/lib/common/rpc-service/center.js +16 -36
  22. package/lib/common/rpc-service/center.js.map +1 -1
  23. package/lib/common/rpc-service/stub.d.ts.map +1 -1
  24. package/lib/common/rpc-service/stub.js +5 -1
  25. package/lib/common/rpc-service/stub.js.map +1 -1
  26. package/lib/common/types.d.ts +1 -1
  27. package/lib/common/types.d.ts.map +1 -1
  28. package/lib/common/ws-channel.d.ts +18 -86
  29. package/lib/common/ws-channel.d.ts.map +1 -1
  30. package/lib/common/ws-channel.js +33 -80
  31. package/lib/common/ws-channel.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/browser/ws-channel-handler.ts +4 -0
  34. package/src/common/connection/drivers/base.ts +6 -0
  35. package/src/common/ext-rpc-protocol.ts +2 -4
  36. package/src/common/proxy/base.ts +28 -80
  37. package/src/common/proxy/legacy.ts +147 -97
  38. package/src/common/rpc-service/center.ts +16 -46
  39. package/src/common/rpc-service/stub.ts +5 -1
  40. package/src/common/types.ts +1 -1
  41. package/src/common/ws-channel.ts +35 -103
  42. package/lib/common/fury-extends/one-of.d.ts +0 -14
  43. package/lib/common/fury-extends/one-of.d.ts.map +0 -1
  44. package/lib/common/fury-extends/one-of.js +0 -53
  45. package/lib/common/fury-extends/one-of.js.map +0 -1
  46. package/lib/common/protocols/common-server.d.ts +0 -3
  47. package/lib/common/protocols/common-server.d.ts.map +0 -1
  48. package/lib/common/protocols/common-server.js +0 -18
  49. package/lib/common/protocols/common-server.js.map +0 -1
  50. package/lib/common/protocols/common.d.ts +0 -24
  51. package/lib/common/protocols/common.d.ts.map +0 -1
  52. package/lib/common/protocols/common.js +0 -12
  53. package/lib/common/protocols/common.js.map +0 -1
  54. package/lib/common/proxy/runner.d.ts +0 -20
  55. package/lib/common/proxy/runner.d.ts.map +0 -1
  56. package/lib/common/proxy/runner.js +0 -46
  57. package/lib/common/proxy/runner.js.map +0 -1
  58. package/lib/common/proxy/sumi.d.ts +0 -9
  59. package/lib/common/proxy/sumi.d.ts.map +0 -1
  60. package/lib/common/proxy/sumi.js +0 -86
  61. package/lib/common/proxy/sumi.js.map +0 -1
  62. package/lib/common/rpc/connection.d.ts +0 -29
  63. package/lib/common/rpc/connection.d.ts.map +0 -1
  64. package/lib/common/rpc/connection.js +0 -195
  65. package/lib/common/rpc/connection.js.map +0 -1
  66. package/lib/common/rpc/index.d.ts +0 -2
  67. package/lib/common/rpc/index.d.ts.map +0 -1
  68. package/lib/common/rpc/index.js +0 -5
  69. package/lib/common/rpc/index.js.map +0 -1
  70. package/lib/common/rpc/packet.d.ts +0 -49
  71. package/lib/common/rpc/packet.d.ts.map +0 -1
  72. package/lib/common/rpc/packet.js +0 -69
  73. package/lib/common/rpc/packet.js.map +0 -1
  74. package/lib/common/rpc/protocol-repository.d.ts +0 -32
  75. package/lib/common/rpc/protocol-repository.d.ts.map +0 -1
  76. package/lib/common/rpc/protocol-repository.js +0 -94
  77. package/lib/common/rpc/protocol-repository.js.map +0 -1
  78. package/lib/common/rpc/types.d.ts +0 -13
  79. package/lib/common/rpc/types.d.ts.map +0 -1
  80. package/lib/common/rpc/types.js +0 -3
  81. package/lib/common/rpc/types.js.map +0 -1
  82. package/src/common/fury-extends/one-of.ts +0 -63
  83. package/src/common/protocols/common-server.ts +0 -18
  84. package/src/common/protocols/common.ts +0 -9
  85. package/src/common/proxy/runner.ts +0 -58
  86. package/src/common/proxy/sumi.ts +0 -81
  87. package/src/common/rpc/connection.ts +0 -247
  88. package/src/common/rpc/index.ts +0 -1
  89. package/src/common/rpc/packet.ts +0 -85
  90. package/src/common/rpc/protocol-repository.ts +0 -148
  91. package/src/common/rpc/types.ts +0 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opensumi/ide-connection",
3
- "version": "2.27.3-next-1706686746.0",
3
+ "version": "2.27.3-rc-1706687185.0",
4
4
  "files": [
5
5
  "lib",
6
6
  "src"
@@ -19,16 +19,16 @@
19
19
  "dependencies": {
20
20
  "@furyjs/fury": "0.5.6-beta",
21
21
  "@opensumi/events": "^0.1.0",
22
- "@opensumi/ide-core-common": "2.27.3-next-1706686746.0",
22
+ "@opensumi/ide-core-common": "2.27.3-rc-1706687185.0",
23
23
  "@opensumi/vscode-jsonrpc": "^8.0.0-next.2",
24
24
  "path-match": "^1.2.4",
25
25
  "reconnecting-websocket": "^4.4.0",
26
26
  "ws": "^8.15.1"
27
27
  },
28
28
  "devDependencies": {
29
- "@opensumi/ide-components": "2.27.3-next-1706686746.0",
29
+ "@opensumi/ide-components": "2.27.3-rc-1706687185.0",
30
30
  "@opensumi/ide-dev-tool": "1.3.1",
31
31
  "@opensumi/mock-socket": "^9.3.1"
32
32
  },
33
- "gitHead": "2a072e19dab9fea8c3925eba3d0a7386415ae485"
33
+ "gitHead": "922ed9010fe3658f626401b559c5d1cae6afa760"
34
34
  }
@@ -72,6 +72,10 @@ export class WSChannelHandler {
72
72
 
73
73
  const channel = this.channelMap.get(msg.id);
74
74
  if (channel) {
75
+ if (!channel.hasMessageListener()) {
76
+ // 要求前端发送初始化消息,但后端最先发送消息时,前端并未准备好
77
+ this.logger.error(this.LOG_TAG, 'channel not ready!', msg);
78
+ }
75
79
  channel.handleMessage(msg);
76
80
  } else {
77
81
  this.logger.warn(this.LOG_TAG, `channel ${msg.id} not found`);
@@ -2,8 +2,14 @@ import { IDisposable } from '@opensumi/ide-core-common';
2
2
 
3
3
  import { IConnectionShape } from '../types';
4
4
 
5
+ import { createQueue } from './utils';
6
+
5
7
  export abstract class BaseConnection<T> implements IConnectionShape<T> {
6
8
  abstract send(data: T): void;
7
9
  abstract onMessage(cb: (data: T) => void): IDisposable;
8
10
  abstract onceClose(cb: () => void): IDisposable;
11
+
12
+ createQueue(): IConnectionShape<T> {
13
+ return createQueue(this);
14
+ }
9
15
  }
@@ -25,8 +25,6 @@ export interface IProxyIdentifier {
25
25
  countId: number;
26
26
  }
27
27
 
28
- const CancellationTokenStr = 'add.cancellation.token';
29
-
30
28
  export class ProxyIdentifier<T = any> {
31
29
  public static count = 0;
32
30
 
@@ -263,7 +261,7 @@ export class RPCProtocol implements IRPCProtocol {
263
261
  const result = new Deferred();
264
262
 
265
263
  if (cancellationToken) {
266
- args.push(CancellationTokenStr);
264
+ args.push('add.cancellation.token');
267
265
  cancellationToken.onCancellationRequested(() => this._protocol.send(MessageIO.cancel(callId)));
268
266
  }
269
267
  this._pendingRPCReplies.set(callId, result);
@@ -344,7 +342,7 @@ export class RPCProtocol implements IRPCProtocol {
344
342
  const method = msg.method;
345
343
  const args = msg.args.map((arg) => (arg === null ? undefined : arg));
346
344
 
347
- const addToken = args.length && args[args.length - 1] === CancellationTokenStr ? args.pop() : false;
345
+ const addToken = args.length && args[args.length - 1] === 'add.cancellation.token' ? args.pop() : false;
348
346
  if (addToken) {
349
347
  const tokenSource = new CancellationTokenSource();
350
348
  this._cancellationTokenSources.set(callId, tokenSource);
@@ -1,36 +1,28 @@
1
1
  import { Deferred, isDefined } from '@opensumi/ide-core-common';
2
2
 
3
3
  import { ILogger, IRPCServiceMap } from '../types';
4
- import { ICapturedMessage, MessageType, ResponseStatus, getCapturer } from '../utils';
5
-
6
- import type { ServiceRunner } from './runner';
4
+ import { ICapturedMessage, getCapturer, getServiceMethods } from '../utils';
7
5
 
8
6
  interface IBaseConnection {
9
7
  listen(): void;
10
8
  }
11
9
 
12
- let requestId = 0;
13
-
14
10
  export abstract class ProxyBase<T extends IBaseConnection> {
11
+ protected proxyService: any = {};
12
+
15
13
  protected logger: ILogger;
16
14
  protected connection: T;
17
15
 
18
16
  protected connectionPromise: Deferred<void> = new Deferred<void>();
19
17
 
20
- protected abstract engine: 'legacy' | 'sumi';
18
+ protected abstract engine: 'legacy';
21
19
 
22
- constructor(public runner: ServiceRunner, logger?: ILogger) {
20
+ constructor(public target?: IRPCServiceMap, logger?: ILogger) {
23
21
  this.logger = logger || console;
24
-
25
- this.runner.onServicesUpdate((services) => {
26
- if (this.connection) {
27
- this.bindMethods(services);
28
- }
29
- });
30
22
  }
31
23
 
32
24
  // capture messages for opensumi devtools
33
- private capture(message: ICapturedMessage): void {
25
+ protected capture(message: ICapturedMessage): void {
34
26
  const capturer = getCapturer();
35
27
  if (isDefined(capturer)) {
36
28
  capturer({
@@ -40,80 +32,36 @@ export abstract class ProxyBase<T extends IBaseConnection> {
40
32
  }
41
33
  }
42
34
 
43
- protected nextRequestId() {
44
- return String(requestId++);
45
- }
46
-
47
- protected captureOnRequest(requestId: string, serviceMethod: string, args: any[]): void {
48
- this.capture({ type: MessageType.OnRequest, requestId, serviceMethod, arguments: args });
49
- }
50
-
51
- protected captureOnRequestResult(requestId: string, serviceMethod: string, data: any): void {
52
- this.capture({
53
- type: MessageType.OnRequestResult,
54
- status: ResponseStatus.Success,
55
- requestId,
56
- serviceMethod,
57
- data,
58
- });
59
- }
60
-
61
- protected captureOnRequestFail(requestId: string, serviceMethod: string, error: any): void {
62
- this.logger.warn(`request exec ${serviceMethod} error`, error);
63
-
64
- this.capture({
65
- type: MessageType.OnRequestResult,
66
- status: ResponseStatus.Fail,
67
- requestId,
68
- serviceMethod,
69
- error,
70
- });
71
- }
72
-
73
- protected captureOnNotification(serviceMethod: string, args: any[]): void {
74
- this.capture({ type: MessageType.OnNotification, serviceMethod, arguments: args });
75
- }
76
-
77
- protected captureSendRequest(requestId: string, serviceMethod: string, args: any[]): void {
78
- this.capture({ type: MessageType.SendRequest, requestId, serviceMethod, arguments: args });
79
- }
80
-
81
- protected captureSendRequestResult(requestId: string, serviceMethod: string, data: any): void {
82
- this.capture({
83
- type: MessageType.RequestResult,
84
- status: ResponseStatus.Success,
85
- requestId,
86
- serviceMethod,
87
- data,
88
- });
89
- }
90
-
91
- protected captureSendRequestFail(requestId: string, serviceMethod: string, error: any): void {
92
- this.capture({
93
- type: MessageType.RequestResult,
94
- status: ResponseStatus.Fail,
95
- requestId,
96
- serviceMethod,
97
- error,
98
- });
99
- }
100
-
101
- protected captureSendNotification(serviceMethod: string, args: any[]): void {
102
- this.capture({ type: MessageType.SendNotification, serviceMethod, arguments: args });
103
- }
104
-
105
35
  listen(connection: T): void {
106
36
  this.connection = connection;
107
- this.bindMethods(this.runner.getServices());
37
+
38
+ if (this.target) {
39
+ this.listenService(this.target);
40
+ }
108
41
 
109
42
  connection.listen();
110
43
  this.connectionPromise.resolve();
111
44
  }
112
45
 
113
46
  public listenService(service: IRPCServiceMap) {
114
- this.runner.registerService(service);
47
+ if (this.connection) {
48
+ this.bindOnRequest(service, (service, prop) => {
49
+ this.proxyService[prop] = service[prop].bind(service);
50
+ });
51
+ } else {
52
+ if (!this.target) {
53
+ this.target = {} as any;
54
+ }
55
+
56
+ const methods = getServiceMethods(service);
57
+ for (const method of methods) {
58
+ // `getServiceMethods` ensure that method is a function
59
+ (this.target as any)[method] = service[method]!.bind(service);
60
+ }
61
+ }
115
62
  }
116
63
 
117
- abstract getInvokeProxy<T = any>(): T;
118
- protected abstract bindMethods(services: string[]): void;
64
+ abstract getInvokeProxy(): any;
65
+
66
+ protected abstract bindOnRequest(service: IRPCServiceMap, cb: (service: IRPCServiceMap, prop: string) => void): void;
119
67
  }
@@ -1,6 +1,9 @@
1
+ import { uuid } from '@opensumi/ide-core-common';
1
2
  import { MessageConnection } from '@opensumi/vscode-jsonrpc';
2
3
 
3
4
  import { METHOD_NOT_REGISTERED } from '../constants';
5
+ import { IRPCServiceMap } from '../types';
6
+ import { MessageType, ResponseStatus, getServiceMethods } from '../utils';
4
7
 
5
8
  import { ProxyBase } from './base';
6
9
 
@@ -10,104 +13,119 @@ interface IRPCResult {
10
13
  }
11
14
 
12
15
  export class ProxyLegacy extends ProxyBase<MessageConnection> {
13
- protected engine = 'legacy' as const;
14
-
15
- protected bindMethods(methods: string[]): void {
16
- for (const method of methods) {
17
- if (method.startsWith('on')) {
18
- this.connection.onNotification(method, async (...args: any[]) => {
19
- this.captureOnNotification(method, args);
20
- try {
21
- await this.runner.run(method, ...this.serializeArguments(args));
22
- } catch (e) {
23
- this.logger.warn(`notification exec ${method} error`, e);
24
- }
25
- });
16
+ engine = 'legacy' as const;
17
+
18
+ public getInvokeProxy(): any {
19
+ return new Proxy(this, this);
20
+ }
21
+
22
+ public get(target: any, p: PropertyKey) {
23
+ const prop = p.toString();
24
+ return async (...args: any[]) => {
25
+ await this.connectionPromise.promise;
26
+
27
+ let isSingleArray = false;
28
+ if (args.length === 1 && Array.isArray(args[0])) {
29
+ isSingleArray = true;
30
+ }
31
+
32
+ // 调用方法为 on 开头时,作为单项通知
33
+ if (prop.startsWith('on')) {
34
+ if (isSingleArray) {
35
+ this.connection.sendNotification(prop, [...args]);
36
+ } else {
37
+ this.connection.sendNotification(prop, ...args);
38
+ }
39
+ this.capture({ type: MessageType.SendNotification, serviceMethod: prop, arguments: args });
26
40
  } else {
27
- this.connection.onRequest(method, async (...args: any[]) => {
28
- const requestId = this.nextRequestId();
29
- this.captureOnRequest(requestId, method, args);
30
-
31
- try {
32
- const result = await this.runner.run(method, ...this.serializeArguments(args));
33
-
34
- this.captureOnRequestResult(requestId, method, result);
35
-
36
- return {
37
- error: false,
38
- data: result,
39
- };
40
- } catch (e) {
41
- this.captureOnRequestFail(requestId, method, e);
42
-
43
- return {
44
- error: true,
45
- data: {
46
- message: e.message,
47
- stack: e.stack,
48
- },
49
- };
41
+ let requestResult: Promise<any>;
42
+ // generate a unique requestId to associate request and requestResult
43
+ const requestId = uuid();
44
+
45
+ if (isSingleArray) {
46
+ requestResult = this.connection.sendRequest(prop, [...args]) as Promise<any>;
47
+ } else {
48
+ requestResult = this.connection.sendRequest(prop, ...args) as Promise<any>;
49
+ }
50
+
51
+ this.capture({ type: MessageType.SendRequest, requestId, serviceMethod: prop, arguments: args });
52
+
53
+ const result: IRPCResult = await requestResult;
54
+
55
+ if (result.error) {
56
+ const error = new Error(result.data.message);
57
+ if (result.data.stack) {
58
+ error.stack = result.data.stack;
50
59
  }
51
- });
60
+ this.capture({
61
+ type: MessageType.RequestResult,
62
+ status: ResponseStatus.Fail,
63
+ requestId,
64
+ serviceMethod: prop,
65
+ error: result.data,
66
+ });
67
+ throw error;
68
+ } else {
69
+ this.capture({
70
+ type: MessageType.RequestResult,
71
+ status: ResponseStatus.Success,
72
+ requestId,
73
+ serviceMethod: prop,
74
+ data: result.data,
75
+ });
76
+ return result.data;
77
+ }
52
78
  }
53
- }
79
+ };
54
80
  }
55
81
 
56
- public getInvokeProxy<T = any>(): T {
57
- return new Proxy(Object.create(null), {
58
- get: (target: any, p: PropertyKey) => {
59
- const prop = p.toString();
60
- if (!target[prop]) {
61
- target[prop] = async (...args: any[]) => {
62
- await this.connectionPromise.promise;
63
-
64
- let isSingleArray = false;
65
- if (args.length === 1 && Array.isArray(args[0])) {
66
- isSingleArray = true;
67
- }
68
-
69
- // 调用方法为 on 开头时,作为单项通知
70
- if (prop.startsWith('on')) {
71
- this.captureSendNotification(prop, args);
72
- if (isSingleArray) {
73
- this.connection.sendNotification(prop, [...args]);
74
- } else {
75
- this.connection.sendNotification(prop, ...args);
76
- }
77
- } else {
78
- // generate a unique requestId to associate request and requestResult
79
- const requestId = this.nextRequestId();
80
-
81
- let requestResult: Promise<any>;
82
-
83
- if (isSingleArray) {
84
- requestResult = this.connection.sendRequest(prop, [...args]) as Promise<any>;
85
- } else {
86
- requestResult = this.connection.sendRequest(prop, ...args) as Promise<any>;
87
- }
88
-
89
- this.captureSendRequest(requestId, prop, args);
90
-
91
- const result: IRPCResult = await requestResult;
92
-
93
- if (result.error) {
94
- const error = new Error(result.data.message);
95
- if (result.data.stack) {
96
- error.stack = result.data.stack;
97
- }
98
-
99
- this.captureSendRequestFail(requestId, prop, result.data);
100
- throw error;
101
- } else {
102
- this.captureSendRequestResult(requestId, prop, result.data);
103
- return result.data;
104
- }
105
- }
106
- };
82
+ protected bindOnRequest(service: IRPCServiceMap, cb?: ((service: IRPCServiceMap, prop: string) => void) | undefined) {
83
+ if (this.connection) {
84
+ const connection = this.connection;
85
+
86
+ const methods = getServiceMethods(service);
87
+
88
+ methods.forEach((method) => {
89
+ if (method.startsWith('on')) {
90
+ connection.onNotification(method, (...args) => {
91
+ this.onNotification(method, ...args);
92
+ this.capture({ type: MessageType.OnNotification, serviceMethod: method, arguments: args });
93
+ });
94
+ } else {
95
+ connection.onRequest(method, (...args) => {
96
+ const requestId = uuid();
97
+ const result = this.onRequest(method, ...args);
98
+ this.capture({ type: MessageType.OnRequest, requestId, serviceMethod: method, arguments: args });
99
+
100
+ result
101
+ .then((result) => {
102
+ this.capture({
103
+ type: MessageType.OnRequestResult,
104
+ status: ResponseStatus.Success,
105
+ requestId,
106
+ serviceMethod: method,
107
+ data: result.data,
108
+ });
109
+ })
110
+ .catch((err) => {
111
+ this.capture({
112
+ type: MessageType.OnRequestResult,
113
+ status: ResponseStatus.Fail,
114
+ requestId,
115
+ serviceMethod: method,
116
+ error: err.data,
117
+ });
118
+ });
119
+
120
+ return result;
121
+ });
107
122
  }
108
- return target[prop];
109
- },
110
- });
123
+
124
+ if (cb) {
125
+ cb(service, method);
126
+ }
127
+ });
128
+ }
111
129
  }
112
130
 
113
131
  /**
@@ -132,17 +150,49 @@ export class ProxyLegacy extends ProxyBase<MessageConnection> {
132
150
  return args;
133
151
  }
134
152
 
153
+ private async onRequest(prop: PropertyKey, ...args: any[]) {
154
+ try {
155
+ const result = await this.proxyService[prop](...this.serializeArguments(args));
156
+
157
+ return {
158
+ error: false,
159
+ data: result,
160
+ };
161
+ } catch (e) {
162
+ return {
163
+ error: true,
164
+ data: {
165
+ message: e.message,
166
+ stack: e.stack,
167
+ },
168
+ };
169
+ }
170
+ }
171
+
172
+ private onNotification(prop: PropertyKey, ...args: any[]) {
173
+ try {
174
+ this.proxyService[prop](...this.serializeArguments(args));
175
+ } catch (e) {
176
+ this.logger.warn('notification', e);
177
+ }
178
+ }
179
+
135
180
  listen(connection: MessageConnection): void {
136
181
  super.listen(connection);
137
-
138
182
  connection.onRequest((method) => {
139
- if (!this.runner.has(method)) {
140
- const requestId = this.nextRequestId();
141
- this.captureOnRequest(requestId, method, []);
183
+ if (!this.proxyService[method]) {
184
+ const requestId = uuid();
185
+ this.capture({ type: MessageType.OnRequest, requestId, serviceMethod: method });
142
186
  const result = {
143
187
  data: METHOD_NOT_REGISTERED,
144
188
  };
145
- this.captureOnRequestFail(requestId, method, result.data);
189
+ this.capture({
190
+ type: MessageType.OnRequestResult,
191
+ status: ResponseStatus.Fail,
192
+ requestId,
193
+ serviceMethod: method,
194
+ error: result.data,
195
+ });
146
196
  return result;
147
197
  }
148
198
  });
@@ -2,11 +2,7 @@ import { Deferred } from '@opensumi/ide-core-common';
2
2
 
3
3
  import { METHOD_NOT_REGISTERED } from '../constants';
4
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';
9
- import { IBench, ILogger, RPCServiceMethod, ServiceType } from '../types';
5
+ import { IBench, ILogger, IRPCServiceMap, RPCServiceMethod, ServiceType } from '../types';
10
6
  import { getMethodName } from '../utils';
11
7
  import { WSChannel } from '../ws-channel';
12
8
 
@@ -16,32 +12,19 @@ const defaultReservedWordSet = new Set(['then']);
16
12
 
17
13
  class Invoker {
18
14
  legacyProxy: ProxyLegacy;
19
- sumiProxy: ProxySumi;
20
15
 
21
16
  private legacyInvokeProxy: any;
22
- private sumiInvokeProxy: any;
23
-
24
- constructor(protected repo: ProtocolRepository) {}
25
17
 
26
18
  setLegacyProxy(proxy: ProxyLegacy) {
27
19
  this.legacyProxy = proxy;
28
20
  this.legacyInvokeProxy = proxy.getInvokeProxy();
29
21
  }
30
22
 
31
- setSumiProxy(proxy: ProxySumi) {
32
- this.sumiProxy = proxy;
33
- this.sumiInvokeProxy = proxy.getInvokeProxy();
34
- }
35
-
36
23
  invoke(name: string, ...args: any[]) {
37
24
  if (defaultReservedWordSet.has(name) || typeof name === 'symbol') {
38
25
  return Promise.resolve();
39
26
  }
40
27
 
41
- if (this.repo.has(name)) {
42
- return this.sumiInvokeProxy[name](...args);
43
- }
44
-
45
28
  return this.legacyInvokeProxy[name](...args);
46
29
  }
47
30
  }
@@ -52,8 +35,7 @@ export class RPCServiceCenter {
52
35
  private invokers: Invoker[] = [];
53
36
  private connection: Array<WSChannel> = [];
54
37
 
55
- private protocolRepository = new ProtocolRepository();
56
- private serviceRunner = new ServiceRunner();
38
+ private serviceMethodMap = { client: undefined } as unknown as IRPCServiceMap;
57
39
 
58
40
  private connectionDeferred = new Deferred<void>();
59
41
  private logger: ILogger;
@@ -75,33 +57,20 @@ export class RPCServiceCenter {
75
57
  return this.connectionDeferred.promise;
76
58
  }
77
59
 
78
- loadProtocol(protocol: TSumiProtocol) {
79
- this.protocolRepository.loadProtocol(protocol, {
80
- nameConverter: (name) => getMethodName(protocol.name, name),
81
- });
82
- }
83
-
84
60
  setChannel(channel: WSChannel) {
85
- if (this.connection.length === 0) {
61
+ if (!this.connection.length) {
86
62
  this.connectionDeferred.resolve();
87
63
  }
88
-
89
64
  this.connection.push(channel);
90
65
  const index = this.connection.length - 1;
91
66
 
92
- const invoker = new Invoker(this.protocolRepository);
93
-
94
- const legacy = new ProxyLegacy(this.serviceRunner, this.logger);
95
- const messageConnection = channel.createMessageConnection();
96
- legacy.listen(messageConnection);
67
+ const rpcProxy = new ProxyLegacy(this.serviceMethodMap, this.logger);
68
+ const connection = channel.createMessageConnection();
69
+ rpcProxy.listen(connection);
97
70
 
98
- const sumi = new ProxySumi(this.serviceRunner, this.logger);
99
- const connection = channel.createConnection(this.protocolRepository);
71
+ const invoker = new Invoker();
100
72
 
101
- sumi.listen(connection);
102
-
103
- invoker.setLegacyProxy(legacy);
104
- invoker.setSumiProxy(sumi);
73
+ invoker.setLegacyProxy(rpcProxy);
105
74
 
106
75
  this.invokers.push(invoker);
107
76
 
@@ -115,13 +84,14 @@ export class RPCServiceCenter {
115
84
  }
116
85
 
117
86
  onRequest(serviceName: string, _name: string, method: RPCServiceMethod) {
118
- this.serviceRunner.register(getMethodName(serviceName, _name), method);
119
- }
120
-
121
- onRequestService(serviceName: string, service: any) {
122
- this.serviceRunner.registerService(service, {
123
- nameConverter: (name) => getMethodName(serviceName, name),
124
- });
87
+ const name = getMethodName(serviceName, _name);
88
+ if (!this.connection.length) {
89
+ this.serviceMethodMap[name] = method;
90
+ } else {
91
+ this.invokers.forEach((proxy) => {
92
+ proxy.legacyProxy.listenService({ [name]: method });
93
+ });
94
+ }
125
95
  }
126
96
 
127
97
  async broadcast(serviceName: string, _name: string, ...args: any[]): Promise<any> {
@@ -1,4 +1,5 @@
1
1
  import { RPCServiceMethod, ServiceType } from '../types';
2
+ import { getServiceMethods } from '../utils';
2
3
 
3
4
  import { RPCServiceCenter } from './center';
4
5
 
@@ -16,7 +17,10 @@ export class RPCServiceStub {
16
17
  }
17
18
 
18
19
  onRequestService(service: any) {
19
- this.center.onRequestService(this.serviceName, service);
20
+ const methods = getServiceMethods(service);
21
+ for (const method of methods) {
22
+ this.onRequest(method, service[method].bind(service));
23
+ }
20
24
  }
21
25
 
22
26
  onRequest(name: string, method: RPCServiceMethod) {
@@ -5,7 +5,7 @@ export interface ILogger {
5
5
  }
6
6
 
7
7
  export type RPCServiceMethod = (...args: any[]) => any;
8
- export type IRPCServiceMap = Record<PropertyKey, RPCServiceMethod>;
8
+ export type IRPCServiceMap = Record<string, RPCServiceMethod>;
9
9
 
10
10
  export enum ServiceType {
11
11
  Service,