@building-block/track-progress
Version:
Track progress, bitrate (speed), remaining time from XHR upload & download events
3 lines (2 loc) • 2.52 kB
JavaScript
var t="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{},e=NaN,n="[object Symbol]",i=/^\s+|\s+$/g,o=/^[-+]0x[0-9a-f]+$/i,r=/^0b[01]+$/i,a=/^0o[0-7]+$/i,u=parseInt,f="object"==typeof t&&t&&t.Object===Object&&t,l="object"==typeof self&&self&&self.Object===Object&&self,c=f||l||Function("return this")(),d=Object.prototype.toString,s=Math.max,v=Math.min,p=function(){return c.Date.now()};function b(t){var e=typeof t;return!!t&&("object"==e||"function"==e)}function g(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||function(t){return!!t&&"object"==typeof t}(t)&&d.call(t)==n}(t))return e;if(b(t)){var f="function"==typeof t.valueOf?t.valueOf():t;t=b(f)?f+"":f}if("string"!=typeof t)return 0===t?t:+t;t=t.replace(i,"");var l=r.test(t);return l||a.test(t)?u(t.slice(2),l?2:8):o.test(t)?e:+t}var m=function(t){return{computable:t.lengthComputable,finished:t.loaded,total:t.total}},y=function(t){void 0===t&&(t={throttleWait:250});var e,n,i,o,r=t.throttleWait,a=Date.now();return e=function(t){var e,n,i,o,r,u,f=t.total,l=t.finished,c=t.computable;return void 0===c&&(c=f>0&&l>=0),e=(Date.now()-a)/1e3,c&&(n=Math.round(100*l/f),e>=1&&(i=l/e),f&&(o=Math.min(l,f)/f,i&&(u=i/8,r=0,n<100&&(r=f/i-e,r=Number(r.toFixed(4)))))),{total:f,finished:l,percent:n,progress:o,elapsed:e,remaining:r,speed:i,bitrate:u}},n=r,i=!0,o=!0,b(void 0)&&(i="leading"in void 0?!!(void 0).leading:i,o="trailing"in void 0?!!(void 0).trailing:o),function(t,e,n){var i,o,r,a,u,f,l=0,c=!1,d=!1,m=!0;if("function"!=typeof t)throw new TypeError("Expected a function");function y(e){var n=i,r=o;return i=o=void 0,l=e,a=t.apply(r,n)}function h(t){var n=t-f;return void 0===f||n>=e||n<0||d&&t-l>=r}function j(){var t=p();if(h(t))return w(t);u=setTimeout(j,function(t){var n=e-(t-f);return d?v(n,r-(t-l)):n}(t))}function w(t){return u=void 0,m&&i?y(t):(i=o=void 0,a)}function x(){var t=p(),n=h(t);if(i=arguments,o=this,f=t,n){if(void 0===u)return function(t){return l=t,u=setTimeout(j,e),c?y(t):a}(f);if(d)return u=setTimeout(j,e),y(f)}return void 0===u&&(u=setTimeout(j,e)),a}return e=g(e)||0,b(n)&&(c=!!n.leading,r=(d="maxWait"in n)?s(g(n.maxWait)||0,e):r,m="trailing"in n?!!n.trailing:m),x.cancel=function(){void 0!==u&&clearTimeout(u),l=0,i=f=o=u=void 0},x.flush=function(){return void 0===u?a:w(p())},x}(e,n,{leading:i,maxWait:n,trailing:o})};export{m as fromXHREvent,y as createProgressTracker};
//# sourceMappingURL=trackProgress.mjs.map