-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathParallelLatency.js
96 lines (85 loc) · 2.35 KB
/
ParallelLatency.js
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
96
import BandwidthEngine from './BandwidthEngine';
class BandwidthWithParallelLatencyEngine extends BandwidthEngine {
constructor(
measurements,
{
measureParallelLatency = false,
parallelLatencyThrottleMs = 100,
downloadApiUrl,
uploadApiUrl,
estimatedServerTime = 0,
...ptProps
} = {}
) {
super(measurements, {
downloadApiUrl,
uploadApiUrl,
estimatedServerTime,
...ptProps
});
if (measureParallelLatency) {
this.#latencyEngine = new BandwidthEngine(
[
{
dir: 'down',
bytes: 0,
count: Infinity,
bypassMinDuration: true
}
],
{
downloadApiUrl,
uploadApiUrl,
estimatedServerTime,
throttleMs: parallelLatencyThrottleMs
}
);
this.#latencyEngine.qsParams = {
during: `${measurements[0].dir}load`
};
super.onRunningChange = this.#setLatencyRunning;
super.onConnectionError = () => this.#latencyEngine.pause();
}
}
// Public attributes
get latencyResults() {
// read access to latency results
return this.#latencyEngine && this.#latencyEngine.results.down[0].timings;
}
// callback invoked when a new parallel latency result arrives
set onParallelLatencyResult(f) {
this.#latencyEngine &&
(this.#latencyEngine.onMeasurementResult = res => f(res));
}
// Overridden attributes
get fetchOptions() {
return super.fetchOptions;
}
set fetchOptions(fetchOptions) {
super.fetchOptions = fetchOptions;
this.#latencyEngine && (this.#latencyEngine.fetchOptions = fetchOptions);
}
set onRunningChange(onRunningChange) {
super.onRunningChange = running => {
this.#setLatencyRunning(running);
onRunningChange(running);
};
}
set onConnectionError(onConnectionError) {
super.onConnectionError = (...args) => {
this.#latencyEngine && this.#latencyEngine.pause();
onConnectionError(...args);
};
}
// Internal state
#latencyEngine;
// Internal methods
#setLatencyRunning(running) {
this.#latencyEngine &&
(!running
? this.#latencyEngine.pause()
: // slight delay in starting latency measurements
setTimeout(() => this.#latencyEngine.play(), 20));
}
}
export default BandwidthWithParallelLatencyEngine;