-
Notifications
You must be signed in to change notification settings - Fork 1
/
DownloadInfoAtom.ts
45 lines (36 loc) · 1.33 KB
/
DownloadInfoAtom.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
import type { HttpResponse, HttpResponseType } from '@bitmovin/player-web-x/types/packages/network/Types';
import type { ContextWithState } from '@bitmovin/player-web-x/types/packages/Types';
export type DownloadInfo = {
size: number;
downloadDuration: number;
timeToFirstByte: number;
};
function createDefaultDownloadInfo(): DownloadInfo {
return {
size: -1,
downloadDuration: -1,
timeToFirstByte: -1,
};
}
function trackDownload(downloadInfo: DownloadInfo, response: HttpResponse<HttpResponseType>) {
const { openedTimestamp, doneTimestamp, headersReceivedTimestamp } = response.timingInformation;
if (
response.status < 200 ||
response.status >= 300 ||
openedTimestamp <= 0 ||
doneTimestamp <= 0 ||
headersReceivedTimestamp <= 0
) {
return false;
}
const downloadDuration = (doneTimestamp - headersReceivedTimestamp) / 1000;
const timeToFirstByte = (headersReceivedTimestamp - openedTimestamp) / 1000;
downloadInfo.size = response.length;
downloadInfo.downloadDuration = downloadDuration;
downloadInfo.timeToFirstByte = timeToFirstByte;
return true;
}
export type DownloadInfoAtom = ReturnType<typeof createDownloadInfoAtom>;
export const createDownloadInfoAtom = (context: ContextWithState) => {
return context.effects.state.create(createDefaultDownloadInfo(), { trackDownload });
};