Repository navigation
Expand file tree
/
Copy pathrequestStatusBarEntry.ts
More file actions
94 lines (77 loc) · 3.18 KB
/
Copy pathrequestStatusBarEntry.ts
File metadata and controls
94 lines (77 loc) · 3.18 KB
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
import { EOL } from 'os';
import { StatusBarAlignment, StatusBarItem, window } from 'vscode';
import { HttpResponse } from '../models/httpResponse';
const filesize = require('filesize');
type NonReceivedRequestStatus = {
state: RequestState.Closed | RequestState.Cancelled | RequestState.Error | RequestState.Pending
};
type ReceivedRequestStatus = {
state: RequestState.Received,
response: HttpResponse
};
type RequestStatus = ReceivedRequestStatus | NonReceivedRequestStatus;
export enum RequestState {
Closed,
Pending,
Received,
Cancelled,
Error,
}
export class RequestStatusEntry {
private readonly durationEntry: StatusBarItem;
private readonly sizeEntry: StatusBarItem;
public constructor() {
this.durationEntry = window.createStatusBarItem('duration', StatusBarAlignment.Left);
this.durationEntry.name = 'Response Timing';
this.sizeEntry = window.createStatusBarItem('size', StatusBarAlignment.Left);
this.sizeEntry.name = 'Response Size';
}
public dispose() {
this.durationEntry.dispose();
this.sizeEntry.dispose();
}
public update(status: RequestStatus) {
this.sizeEntry.hide();
switch (status.state) {
case RequestState.Closed:
case RequestState.Error:
this.durationEntry.hide();
break;
case RequestState.Pending:
this.showDurationEntry('$(sync~spin) Waiting...', 'Click to cancel', 'rest-client.cancel-request');
break;
case RequestState.Cancelled:
this.showDurationEntry('$(circle-slash) Cancelled');
break;
case RequestState.Received:
const response = status.response;
const tooltip = [
'Breakdown of Duration:',
`Socket: ${response.timingPhases.wait?.toFixed(1) ?? 0}ms`,
`DNS: ${response.timingPhases.dns?.toFixed(1) ?? 0}ms`,
`TCP: ${response.timingPhases.tcp?.toFixed(1) ?? 0}ms`,
`Request: ${response.timingPhases.request?.toFixed(1) ?? 0}ms`,
`FirstByte: ${response.timingPhases.firstByte?.toFixed(1) ?? 0}ms`,
`Download: ${response.timingPhases.download?.toFixed(1) ?? 0}ms`
].join(EOL);
this.showDurationEntry(`$(clock) ${response.timingPhases.total ?? 0}ms`, tooltip);
this.showSizeEntry(response);
break;
}
}
private showSizeEntry(response: HttpResponse) {
this.sizeEntry.text = `$(database) ${filesize(response.bodySizeInBytes + response.headersSizeInBytes)}`;
this.sizeEntry.tooltip = [
'Breakdown of Response Size:',
`Headers: ${filesize(response.headersSizeInBytes)}`,
`Body: ${filesize(response.bodySizeInBytes)}`
].join(EOL);
this.sizeEntry.show();
}
private showDurationEntry(text: string, tooltip?: string, command?: string) {
this.durationEntry.text = text;
this.durationEntry.tooltip = tooltip;
this.durationEntry.command = command;
this.durationEntry.show();
}
}