Skip to content

Commit

Permalink
refactor: 只在common/proxy.ts注射capturer逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
tyn1998 committed Jul 9, 2022
1 parent c6ff77a commit 5a04ac4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 92 deletions.
41 changes: 1 addition & 40 deletions packages/connection/src/common/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,50 +83,11 @@ export class WebSocketMessageWriter extends AbstractMessageWriter implements Mes
public end(): void {}
}

declare global {
interface Window {
__opensumi_devtools: any;
}
}

/**
* 给服务端的 WebSocket 及 Browser 端的 WebSocket 实例共用的方法
* @param socket
* @returns
*/
export function createWebSocketConnection(socket: any) {
// return createMessageConnection(new WebSocketMessageReader(socket), new WebSocketMessageWriter(socket));

const messageConnection = createMessageConnection(
new WebSocketMessageReader(socket),
new WebSocketMessageWriter(socket),
);

// sendRequest是有回复的,在proxy.ts中处理requestResult
const messageConnectionProxy = new Proxy(messageConnection, {
get(target, prop) {
if (prop === 'sendRequest' || prop === 'sendNotification') {
return function (...args: any) {
// 注意这是common/xxx,所以要同时考虑在browser和在node的情况,node是没有window的
if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
window.__opensumi_devtools.capture([prop, ...args]);
}
return target[prop].apply(target, [...args]);
};
}

// onNotification很多的,onRequest我都试不出来
// if (prop === 'onRequest' || prop === 'onNotification') {
// return function (...args: any) {
// if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
// window.__opensumi_devtools.capture([prop, ...args]);
// }
// return target[prop].apply(target, [...args]);
// };
// }
return target[prop];
},
});

return messageConnectionProxy;
return createMessageConnection(new WebSocketMessageReader(socket), new WebSocketMessageWriter(socket));
}
34 changes: 11 additions & 23 deletions packages/connection/src/common/proxy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ApplicationError } from '@opensumi/ide-core-common';
import type { MessageConnection } from '@opensumi/vscode-jsonrpc/lib/common/connection';

import { getCapturer } from './utils';

export abstract class RPCService<T = any> {
rpcClient?: T[];
rpcRegistered?: boolean;
Expand Down Expand Up @@ -98,17 +100,21 @@ export class RPCProxy {
if (prop.startsWith('on')) {
if (isSingleArray) {
connection.sendNotification(prop, [...args]);
getCapturer() && getCapturer()(['sendNotification', prop, [...args]]);
} else {
connection.sendNotification(prop, ...args);
getCapturer() && getCapturer()(['sendNotification', prop, ...args]);
}

resolve(null);
} else {
let requestResult: Promise<any>;
if (isSingleArray) {
requestResult = connection.sendRequest(prop, [...args]) as Promise<any>;
getCapturer() && getCapturer()(['sendRequest', prop, [...args]]);
} else {
requestResult = connection.sendRequest(prop, ...args) as Promise<any>;
getCapturer() && getCapturer()(['sendRequest', prop, ...args]);
}

requestResult
Expand All @@ -126,22 +132,10 @@ export class RPCProxy {
const applicationError = ApplicationError.fromJson(result.error.code, result.error.data);
error.cause = applicationError;
}
if (
typeof window !== 'undefined' &&
window.__opensumi_devtools &&
window.__opensumi_devtools.capture
) {
window.__opensumi_devtools.capture(['错误', error]);
}
getCapturer() && getCapturer()(['错误', error]);
reject(error);
} else {
if (
typeof window !== 'undefined' &&
window.__opensumi_devtools &&
window.__opensumi_devtools.capture
) {
window.__opensumi_devtools.capture(['requestResult', result.data]);
}
getCapturer() && getCapturer()(['requestResult', result.data]);
resolve(result.data);
}
});
Expand Down Expand Up @@ -178,17 +172,13 @@ export class RPCProxy {
if (method.startsWith('on')) {
connection.onNotification(method, (...args) => this.onNotification(method, ...args));
connection.onNotification(method, (...args) => {
if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
window.__opensumi_devtools.capture(['onNotification', method, ...args]);
}
getCapturer() && getCapturer()(['onNotification', method, ...args]);
this.onNotification(method, ...args);
});
} else {
// connection.onRequest(method, (...args) => this.onRequest(method, ...args));
connection.onRequest(method, (...args) => {
if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
window.__opensumi_devtools.capture(['onRequest', method, ...args]);
}
getCapturer() && getCapturer()(['onRequest', method, ...args]);
return this.onRequest(method, ...args);
});
}
Expand All @@ -200,9 +190,7 @@ export class RPCProxy {

connection.onRequest((method) => {
if (!this.proxyService[method]) {
if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
window.__opensumi_devtools.capture(`onRequest: ${method} is not registered!`);
}
getCapturer() && getCapturer()(`onRequest: ${method} is not registered!`);
return {
data: NOTREGISTERMETHOD,
};
Expand Down
13 changes: 13 additions & 0 deletions packages/connection/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ export function stringify(obj: any): string {
export function parse(input: string, reviver?: (this: any, key: string, value: any) => any): any {
return JSON.parse(input, reviver);
}

declare global {
interface Window {
__opensumi_devtools: any;
}
}

export function getCapturer() {
if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
return window.__opensumi_devtools.capture;
}
return;
}
30 changes: 1 addition & 29 deletions packages/connection/src/node/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,5 @@ import {
} from '@opensumi/vscode-jsonrpc/lib/node/main';

export function createSocketConnection(socket: net.Socket) {
// return createMessageConnection(new SocketMessageReader(socket), new SocketMessageWriter(socket));

const messageConnection = createMessageConnection(new SocketMessageReader(socket), new SocketMessageWriter(socket));

const messageConnectionProxy = new Proxy(messageConnection, {
get(target, prop) {
if (prop === 'sendRequest' || prop === 'sendNotification') {
return function (...args: any) {
// 注意这是common/xxx,所以要同时考虑在browser和在node的情况,node是没有window的
if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
window.__opensumi_devtools.capture([prop, ...args]);
}
return target[prop].apply(target, [...args]);
};
}

// if (prop === 'onRequest' || prop === 'onNotification') {
// return function (...args: any) {
// if (typeof window !== 'undefined' && window.__opensumi_devtools && window.__opensumi_devtools.capture) {
// window.__opensumi_devtools.capture([prop, ...args]);
// }
// return target[prop].apply(target, [...args]);
// };
// }
return target[prop];
},
});

return messageConnectionProxy;
return createMessageConnection(new SocketMessageReader(socket), new SocketMessageWriter(socket));
}

0 comments on commit 5a04ac4

Please sign in to comment.