UNPKG

@building-block/track-progress

Version:

Track progress, bitrate (speed), remaining time from XHR upload & download events

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