source: imaps-frontend/node_modules/axios/lib/helpers/progressEventReducer.js@ d565449

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import speedometer from "./speedometer.js";
2import throttle from "./throttle.js";
3import utils from "../utils.js";
4
5export const progressEventReducer = (listener, isDownloadStream, freq = 3) => {
6 let bytesNotified = 0;
7 const _speedometer = speedometer(50, 250);
8
9 return throttle(e => {
10 const loaded = e.loaded;
11 const total = e.lengthComputable ? e.total : undefined;
12 const progressBytes = loaded - bytesNotified;
13 const rate = _speedometer(progressBytes);
14 const inRange = loaded <= total;
15
16 bytesNotified = loaded;
17
18 const data = {
19 loaded,
20 total,
21 progress: total ? (loaded / total) : undefined,
22 bytes: progressBytes,
23 rate: rate ? rate : undefined,
24 estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
25 event: e,
26 lengthComputable: total != null,
27 [isDownloadStream ? 'download' : 'upload']: true
28 };
29
30 listener(data);
31 }, freq);
32}
33
34export const progressEventDecorator = (total, throttled) => {
35 const lengthComputable = total != null;
36
37 return [(loaded) => throttled[0]({
38 lengthComputable,
39 total,
40 loaded
41 }), throttled[1]];
42}
43
44export const asyncDecorator = (fn) => (...args) => utils.asap(() => fn(...args));
Note: See TracBrowser for help on using the repository browser.