MCPcopy
hub / github.com/axios/axios / progressEventReducer

Function progressEventReducer

lib/helpers/progressEventReducer.js:5–35  ·  view source on GitHub ↗
(listener, isDownloadStream, freq = 3)

Source from the content-addressed store, hash-verified

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 if (!e || typeof e.loaded !== 'number') {
11 return;
12 }
13 const rawLoaded = e.loaded;
14 const total = e.lengthComputable ? e.total : undefined;
15 const loaded = total != null ? Math.min(rawLoaded, total) : rawLoaded;
16 const progressBytes = Math.max(0, loaded - bytesNotified);
17 const rate = _speedometer(progressBytes);
18
19 bytesNotified = Math.max(bytesNotified, loaded);
20
21 const data = {
22 loaded,
23 total,
24 progress: total ? loaded / total : undefined,
25 bytes: progressBytes,
26 rate: rate ? rate : undefined,
27 estimated: rate && total ? (total - loaded) / rate : undefined,
28 event: e,
29 lengthComputable: total != null,
30 [isDownloadStream ? 'download' : 'upload']: true,
31 };
32
33 listener(data);
34 }, freq);
35};
36
37export const progressEventDecorator = (total, throttled) => {
38 const lengthComputable = total != null;

Callers 4

factoryFunction · 0.90
xhr.jsFile · 0.90
http.jsFile · 0.90

Calls 2

speedometerFunction · 0.85
throttleFunction · 0.85

Tested by

no test coverage detected