source: imaps-frontend/node_modules/axios/lib/helpers/speedometer.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.1 KB
RevLine 
[d565449]1'use strict';
2
3/**
4 * Calculate data maxRate
5 * @param {Number} [samplesCount= 10]
6 * @param {Number} [min= 1000]
7 * @returns {Function}
8 */
9function speedometer(samplesCount, min) {
10 samplesCount = samplesCount || 10;
11 const bytes = new Array(samplesCount);
12 const timestamps = new Array(samplesCount);
13 let head = 0;
14 let tail = 0;
15 let firstSampleTS;
16
17 min = min !== undefined ? min : 1000;
18
19 return function push(chunkLength) {
20 const now = Date.now();
21
22 const startedAt = timestamps[tail];
23
24 if (!firstSampleTS) {
25 firstSampleTS = now;
26 }
27
28 bytes[head] = chunkLength;
29 timestamps[head] = now;
30
31 let i = tail;
32 let bytesCount = 0;
33
34 while (i !== head) {
35 bytesCount += bytes[i++];
36 i = i % samplesCount;
37 }
38
39 head = (head + 1) % samplesCount;
40
41 if (head === tail) {
42 tail = (tail + 1) % samplesCount;
43 }
44
45 if (now - firstSampleTS < min) {
46 return;
47 }
48
49 const passed = startedAt && now - startedAt;
50
51 return passed ? Math.round(bytesCount * 1000 / passed) : undefined;
52 };
53}
54
55export default speedometer;
Note: See TracBrowser for help on using the repository browser.