progressEventReducer.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import speedometer from "./speedometer.js";
  2. import throttle from "./throttle.js";
  3. import utils from "../utils.js";
  4. export const progressEventReducer = (listener, isDownloadStream, freq = 3) => {
  5. let bytesNotified = 0;
  6. const _speedometer = speedometer(50, 250);
  7. return throttle(e => {
  8. const loaded = e.loaded;
  9. const total = e.lengthComputable ? e.total : undefined;
  10. const progressBytes = loaded - bytesNotified;
  11. const rate = _speedometer(progressBytes);
  12. const inRange = loaded <= total;
  13. bytesNotified = loaded;
  14. const data = {
  15. loaded,
  16. total,
  17. progress: total ? (loaded / total) : undefined,
  18. bytes: progressBytes,
  19. rate: rate ? rate : undefined,
  20. estimated: rate && total && inRange ? (total - loaded) / rate : undefined,
  21. event: e,
  22. lengthComputable: total != null,
  23. [isDownloadStream ? 'download' : 'upload']: true
  24. };
  25. listener(data);
  26. }, freq);
  27. }
  28. export const progressEventDecorator = (total, throttled) => {
  29. const lengthComputable = total != null;
  30. return [(loaded) => throttled[0]({
  31. lengthComputable,
  32. total,
  33. loaded
  34. }), throttled[1]];
  35. }
  36. export const asyncDecorator = (fn) => (...args) => utils.asap(() => fn(...args));