Skip to content

Commit d9e7901

Browse files
committed
Clean up rpc tracing and use safer types
1 parent 39c75f6 commit d9e7901

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

src/vs/platform/terminal/node/ptyService.ts

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,54 +30,51 @@ import { formatMessageForTerminal } from 'vs/platform/terminal/common/terminalSt
3030
import { IPtyHostProcessReplayEvent } from 'vs/platform/terminal/common/capabilities/capabilities';
3131
import { IProductService } from 'vs/platform/product/common/productService';
3232
import { join } from 'path';
33+
import { memoize } from 'vs/base/common/decorators';
3334

34-
type WorkspaceId = string;
35+
export function traceRpc(): Function {
36+
function createDecorator(mapFn: (fn: Function, key: string) => Function): Function {
37+
return (target: any, key: string, descriptor: any) => {
38+
let fnKey: string | null = null;
39+
let fn: Function | null = null;
40+
41+
if (typeof descriptor.value === 'function') {
42+
fnKey = 'value';
43+
fn = descriptor.value;
44+
} else if (typeof descriptor.get === 'function') {
45+
fnKey = 'get';
46+
fn = descriptor.get;
47+
}
3548

36-
let SerializeAddon: typeof XtermSerializeAddon;
37-
let Unicode11Addon: typeof XtermUnicode11Addon;
49+
if (!fn) {
50+
throw new Error('not supported');
51+
}
3852

39-
let traceLogService: ILogService | undefined;
40-
let simulatedLatency: number = 0;
41-
export function traceRpc(): Function {
53+
descriptor[fnKey!] = mapFn(fn, key);
54+
};
55+
}
4256
return createDecorator((fn, key) => {
43-
return async function (this: any, ...args: any[]) {
44-
45-
if (traceLogService?.getLevel() === LogLevel.Trace) {
46-
traceLogService?.trace(`[RPC Request] PtyService#${fn.name}(${args.map(e => JSON.stringify(e)).join(', ')})`);
57+
// The PtyService type is unsafe, this decorator should only be used on PtyService
58+
return async function (this: PtyService, ...args: any[]) {
59+
if (this.traceRpcArgs.logService.getLevel() === LogLevel.Trace) {
60+
this.traceRpcArgs.logService.trace(`[RPC Request] PtyService#${fn.name}(${args.map(e => JSON.stringify(e)).join(', ')})`);
4761
}
48-
// TODO: Use PtyService as this?
49-
if (simulatedLatency) {
50-
await timeout(simulatedLatency);
62+
if (this.traceRpcArgs.simulatedLatency) {
63+
await timeout(this.traceRpcArgs.simulatedLatency);
5164
}
5265
const result = await fn.apply(this, args);
53-
if (traceLogService?.getLevel() === LogLevel.Trace) {
54-
traceLogService?.trace(`[RPC Response] PtyService#${fn.name}`, result);
66+
if (this.traceRpcArgs.logService.getLevel() === LogLevel.Trace) {
67+
this.traceRpcArgs.logService.trace(`[RPC Response] PtyService#${fn.name}`, result);
5568
}
5669
return result;
5770
};
5871
});
5972
}
6073

61-
function createDecorator(mapFn: (fn: Function, key: string) => Function): Function {
62-
return (target: any, key: string, descriptor: any) => {
63-
let fnKey: string | null = null;
64-
let fn: Function | null = null;
65-
66-
if (typeof descriptor.value === 'function') {
67-
fnKey = 'value';
68-
fn = descriptor.value;
69-
} else if (typeof descriptor.get === 'function') {
70-
fnKey = 'get';
71-
fn = descriptor.get;
72-
}
73-
74-
if (!fn) {
75-
throw new Error('not supported');
76-
}
74+
type WorkspaceId = string;
7775

78-
descriptor[fnKey!] = mapFn(fn, key);
79-
};
80-
}
76+
let SerializeAddon: typeof XtermSerializeAddon;
77+
let Unicode11Addon: typeof XtermUnicode11Addon;
8178

8279
export class PtyService extends Disposable implements IPtyService {
8380
declare readonly _serviceBrand: undefined;
@@ -106,6 +103,14 @@ export class PtyService extends Disposable implements IPtyService {
106103
private readonly _onDidChangeProperty = this._register(new Emitter<{ id: number; property: IProcessProperty<any> }>());
107104
readonly onDidChangeProperty = this._onDidChangeProperty.event;
108105

106+
@memoize
107+
get traceRpcArgs(): { logService: ILogService; simulatedLatency: number } {
108+
return {
109+
logService: this._logService,
110+
simulatedLatency: this._simulatedLatency
111+
};
112+
}
113+
109114
constructor(
110115
private _lastPtyId: number,
111116
private readonly _logService: ILogService,
@@ -115,9 +120,6 @@ export class PtyService extends Disposable implements IPtyService {
115120
) {
116121
super();
117122

118-
traceLogService = this._logService;
119-
simulatedLatency = this._simulatedLatency;
120-
121123
this._register(toDisposable(() => {
122124
for (const pty of this._ptys.values()) {
123125
pty.shutdown(true);

0 commit comments

Comments
 (0)