-
Notifications
You must be signed in to change notification settings - Fork 393
/
connection-rtt-contribution.ts
95 lines (79 loc) · 2.85 KB
/
connection-rtt-contribution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { Autowired, Injectable } from '@opensumi/di';
import { IStatusBarService, StatusBarAlignment, StatusBarEntryAccessor } from '@opensumi/ide-core-browser/lib/services';
import { Command, CommandContribution, CommandRegistry } from '@opensumi/ide-core-common/lib/command';
import { Domain } from '@opensumi/ide-core-common/lib/di-helper';
import { localize } from '@opensumi/ide-core-common/lib/localize';
import { ConnectionBackServicePath, IConnectionBackService } from '../common';
const START_CONNECTION_RTT_COMMAND: Command = {
id: 'connection.start.rtt',
label: localize('connection.start.rtt', 'Measure Connection RTT'),
category: localize('command.category.developerTools', 'Developer Tool'),
};
const STOP_CONNECTION_RTT_COMMAND = {
id: 'connection.stop.rtt',
label: localize('connection.stop.rtt', 'Stop Connection RTT'),
category: localize('command.category.developerTools', 'Developer Tool'),
};
const statusBarOption = {
alignment: StatusBarAlignment.LEFT,
priority: Infinity - 1,
};
export const ConnectionRTTBrowserServiceToken = Symbol('ConnectionRTTBrowserService');
@Injectable()
export class ConnectionRTTBrowserService {
@Autowired(ConnectionBackServicePath)
protected readonly connectionBackService: IConnectionBackService;
async measure() {
await this.connectionBackService.$measure();
}
}
@Domain(CommandContribution)
export class ConnectionRTTContribution implements CommandContribution {
@Autowired(IStatusBarService)
protected readonly statusBarService: IStatusBarService;
@Autowired(ConnectionRTTBrowserServiceToken)
protected readonly rttService: ConnectionRTTBrowserService;
private interval?: NodeJS.Timeout;
private statusBar?: StatusBarEntryAccessor;
static INTERVAL = 1000;
registerCommands(commands: CommandRegistry): void {
commands.registerCommand(START_CONNECTION_RTT_COMMAND, {
execute: () => {
if (!this.interval) {
this.startRTTInterval();
}
},
});
commands.registerCommand(STOP_CONNECTION_RTT_COMMAND, {
execute: () => {
if (!this.interval) {
return;
}
global.clearInterval(this.interval);
if (this.statusBar) {
this.statusBar.dispose();
this.statusBar = undefined;
}
},
});
}
private startRTTInterval() {
this.interval = global.setInterval(async () => {
const start = Date.now();
await this.rttService.measure();
const rtt = Date.now() - start;
const option = {
text: `${rtt}ms`,
};
if (!this.statusBar) {
const element = this.statusBarService.addElement('connection-rtt', {
...option,
...statusBarOption,
});
this.statusBar = element;
} else {
this.statusBar.update({ ...option, ...statusBarOption });
}
}, ConnectionRTTContribution.INTERVAL);
}
}