UNPKG

3.87 kBJavaScriptView Raw
1!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t=t||self).PollingObserver={})}(this,function(t){"use strict";function n(t,n,e,i){return new(e||(e=Promise))(function(r,o){function u(t){try{c(i.next(t))}catch(n){o(n)}}function s(t){try{c(i.throw(t))}catch(n){o(n)}}function c(t){t.done?r(t.value):new e(function(n){n(t.value)}).then(u,s)}c((i=i.apply(t,n||[])).next())})}function e(t,n){var e,i,r,o,u={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return o={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(o){return function(s){return function(o){if(e)throw new TypeError("Generator is already executing.");for(;u;)try{if(e=1,i&&(r=2&o[0]?i.return:o[0]?i.throw||((r=i.return)&&r.call(i),0):i.next)&&!(r=r.call(i,o[1])).done)return r;switch(i=0,r&&(o=[2&o[0],r.value]),o[0]){case 0:case 1:r=o;break;case 4:return u.label++,{value:o[1],done:!1};case 5:u.label++,i=o[1],o=[0];continue;case 7:o=u.ops.pop(),u.trys.pop();continue;default:if(!(r=(r=u.trys).length>0&&r[r.length-1])&&(6===o[0]||2===o[0])){u=0;continue}if(3===o[0]&&(!r||o[1]>r[0]&&o[1]<r[3])){u.label=o[1];break}if(6===o[0]&&u.label<r[1]){u.label=r[1],r=o;break}if(r&&u.label<r[2]){u.label=r[2],u.ops.push(o);break}r[2]&&u.ops.pop(),u.trys.pop();continue}o=n.call(t,u)}catch(s){o=[6,s],i=0}finally{e=r=0}if(5&o[0])throw o[1];return{value:o[0]?o[1]:void 0,done:!0}}([o,s])}}}function i(t){return void 0===t&&(t=0),n(this,void 0,void 0,function(){return e(this,function(n){return[2,new Promise(function(n){("number"==typeof t?+t:0)<1?n():setTimeout(n,t)})]})})}function r(){return n(this,void 0,void 0,function(){return e(this,function(t){return[2,("undefined"!=typeof window?window:global).performance]})})}var PollingMeasure=function(){function PollingMeasure(t,n,e){this.name=t,this.duration=n,this.startTime=e,this.entryType="polling-measure"}return PollingMeasure.prototype.toJSON=function(){return{duration:this.duration,entryType:this.entryType,name:this.name,startTime:this.startTime}},PollingMeasure}();function o(t){return"function"==typeof t.then}var PollingObserver=function(){function PollingObserver(t){if(this.conditionCallback=t,this.t=!1,this.i=[],this.o=!1,"function"!=typeof t)throw new TypeError("'conditionCallback' is not defined")}return PollingObserver.prototype.disconnect=function(){this.t=!0,this.o||(this.i=[])},PollingObserver.prototype.observe=function(t,u){return n(this,void 0,void 0,function(){var n,s,c,f,a,h,d,l,y,p,v,w,b,m,k,x,T,g,j,E,P,S,_,C,G,M,O,q;return e(this,function(e){switch(e.label){case 0:return this.t=!1,s=(n=u||{}).interval,c=n.timeout,a="number"==typeof c?+c:-1,h=(f="number"==typeof s&&s>0)?+s:-1,[4,r()];case 1:d=e.sent(),l=a<1,y=this.i,p=this.onfinish,v=this.conditionCallback,w=0,b=void 0,m=0,k="finish",x={},e.label=2;case 2:e.trys.push([2,13,14,15]),e.label=3;case 3:return this.t?[3,12]:(this.o=!0,o(T=v(b,y,this))?[4,T]:[3,5]);case 4:return j=e.sent(),[3,6];case 5:j=T,e.label=6;case 6:return g=j,(E=!l&&w>=a)||g?(k=E?"timeout":k,[3,12]):(P=d.now(),o(S=t())?[4,S]:[3,8]);case 7:return _=e.sent(),[3,9];case 8:_=S,e.label=9;case 9:return b=_,C=d.now(),G=C-P,M=f?h-G:0,y.push(new PollingMeasure("polling:"+m,G,P)),w+=G>h?G:h,m+=1,M>0?[4,i(M)]:[3,11];case 10:e.sent(),e.label=11;case 11:return[3,3];case 12:return x={status:k,value:b},[3,15];case 13:return O=e.sent(),x={status:"error",reason:O},[3,15];case 14:return q=y.slice(),this.t&&(this.i=[]),this.o=this.t=!1,"function"==typeof p&&p(x,q,this),[7];case 15:return[2]}})})},PollingObserver.prototype.takeRecords=function(){return this.i},PollingObserver}();t.PollingObserver=PollingObserver,t.default=PollingObserver,Object.defineProperty(t,"u",{value:!0})});
2//# sourceMappingURL=polling-observer.umd.min.js.map