@iotize/tap
Version:
IoTize Device client for Javascript
16 lines (14 loc) • 5.45 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@iotize/tap/service/impl/interface"),require("rxjs"),require("rxjs/operators"),require("@iotize/common/debug")):"function"==typeof define&&define.amd?define("@iotize/tap/keep-alive",["exports","@iotize/tap/service/impl/interface","rxjs","rxjs/operators","@iotize/common/debug"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).iotize=e.iotize||{},e.iotize.tap=e.iotize.tap||{},e.iotize.tap["keep-alive"]={}),e.iotize.tap.service.impl.interface,e.rxjs,e.rxjs.operators,e.debug$1)}(this,(function(e,t,i,n,o){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function r(e,t){var i,n,o,r,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return r={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function u(r){return function(u){return function(r){if(i)throw new TypeError("Generator is already executing.");for(;s;)try{if(i=1,n&&(o=2&r[0]?n.return:r[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,r[1])).done)return o;switch(n=0,o&&(r=[2&r[0],o.value]),r[0]){case 0:case 1:o=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,n=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!o||r[1]>o[0]&&r[1]<o[3])){s.label=r[1];break}if(6===r[0]&&s.label<o[1]){s.label=o[1],o=r;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(r);break}o[2]&&s.ops.pop(),s.trys.pop();continue}r=t.call(e,s)}catch(e){r=[6,e],n=0}finally{i=o=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,u])}}}Object.create;Object.create;var s=o.createDebugger("@iotize/tap/keepalive"),u=this&&this.__awaiter||function(e,t,i,n){return new(i||(i=Promise))((function(o,r){function s(e){try{p(n.next(e))}catch(e){r(e)}}function u(e){try{p(n.throw(e))}catch(e){r(e)}}function p(e){var t;e.done?o(e.value):(t=e.value,t instanceof i?t:new i((function(e){e(t)}))).then(s,u)}p((n=n.apply(e,t||[])).next())}))},p="KeepAliveEngine",a=function(){function e(e,t){var o=this;this.tap=e,this._options=t,this._lastResponseTime=0,this._isStopped=!0,this._frameCounter=0,this._events=new i.Subject,s(p,"new KeepAliveEngine()"),this.tap.client.addInterceptor((function(e,t){return t.handle(e).pipe(n.tap({next:function(){s(p,"update last response time",o._lastResponseTime,"=>",(new Date).getTime()),o._lastResponseTime=(new Date).getTime()}}))}))}return Object.defineProperty(e.prototype,"counter",{get:function(){return this._frameCounter},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"events",{get:function(){return this._events.asObservable()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"period",{set:function(e){"number"!=typeof e&&(e=0),this._options.period=e,this._options.period<=0?this.clearTimeout():!this.timeoutId&&this.running&&this._setupNextTimeout(0)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"running",{get:function(){return!this._isStopped},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"options",{get:function(){return this._options},enumerable:!1,configurable:!0}),e.prototype.start=function(){this._isStopped=!1,!this.timeoutId&&this.options.period>0&&this._setupNextTimeout(0),this._events.next({type:"start"})},e.prototype.stop=function(){this._isStopped=!0,this.clearTimeout(),this._events.next({type:"stop"})},e.prototype.clearTimeout=function(){this.timeoutId&&(clearTimeout(this.timeoutId),this.timeoutId=void 0)},e.prototype._setupNextTimeout=function(e){var t=this;this._isStopped||(this._events.next({type:"rescheduleKeepAlive",timeout:e}),this.timeoutId=setTimeout((function(){return u(t,void 0,void 0,(function(){var e,t;return r(this,(function(i){switch(i.label){case 0:return this.tap.isConnected()?(e=this._options.period-(n=this._lastResponseTime,(new Date).getTime()-n))>0?(s(p,"Reschedule keep alive in ",e),this._setupNextTimeout(e),[3,6]):[3,1]:[3,6];case 1:s(p,"Running keep alive"),this._frameCounter++,i.label=2;case 2:return i.trys.push([2,4,,5]),this._events.next({type:"tick"}),[4,this.tap.service.interface.keepAlive()];case 3:return i.sent(),[3,5];case 4:return t=i.sent(),this._events.next({type:"error",error:t}),[3,5];case 5:this._options.period>0&&this._setupNextTimeout(this._options.period),i.label=6;case 6:return[2]}var n}))}))}),e))},e}();e.DEFAULT_KEEP_ALIVE_PERIOD_MS=5e3,e.KeepAliveEngine=a,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=iotize-tap-keep-alive.umd.min.js.map