@signumjs/monitor
Version:
Monitor transactions on Signum Network blockchain
17 lines (14 loc) • 7.89 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).sig$monitor={})}(this,(function(exports){"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}function _createClass(e,t,i){return t&&_defineProperties(e.prototype,t),i&&_defineProperties(e,i),e}
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */function __awaiter(e,t,i,n){return new(i||(i=Promise))((function(o,r){function s(e){try{u(n.next(e))}catch(e){r(e)}}function a(e){try{u(n.throw(e))}catch(e){r(e)}}function u(e){e.done?o(e.value):new i((function(t){t(e.value)})).then(s,a)}u((n=n.apply(e,t||[])).next())}))}var VoidLogger=function(){function e(){_classCallCheck(this,e)}return _createClass(e,[{key:"debug",value:function(e){}},{key:"error",value:function(e){}},{key:"log",value:function(e){}}]),e}(),voidLogger=new VoidLogger,isNode=function(){return"undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node},EventEmitter=function(){function e(){if(_classCallCheck(this,e),isNode()){var t=require("events");this._emitter=new t}else this._emitter={on:function(e,t){window.addEventListener(e,(function(e){return t(e.detail)}))},once:function(e,t){var i=function(e){t(e)};window.addEventListener(e,(function(t){i(t.detail),window.removeEventListener(e,i)}))},off:function(e,t){window.removeEventListener(e,t)},emit:function(e,t){window.dispatchEvent(new window.CustomEvent(e,{detail:t}))}}}return _createClass(e,[{key:"off",value:function(e,t){this._emitter.off(e,t)}},{key:"on",value:function(e,t){this._emitter.on(e,t)}},{key:"once",value:function(e,t){this._emitter.once(e,t)}},{key:"emit",value:function(e,t){this._emitter.emit(e,t)}}]),e}(),MonitorEvents={Fulfilled:"@signumjs/monitor/fulfilled",Timeout:"@signumjs/monitor/timeout"},Monitor=function(){function Monitor(e){_classCallCheck(this,Monitor),this._timeoutSecs=-1,this._emitter=new EventEmitter,this._intervalSecs=-1,this._startTime=-1,this._handle=void 0;var t=e.asyncFetcherFn,i=e.compareFn,n=e.intervalSecs,o=e.key,r=e.logger,s=e.timeoutSecs;if(n<1)throw new Error("interval must be greater than zero");this._key=o,this._intervalSecs=n,this._timeoutSecs=s,this._asyncFetcher=t,this._compareFn=i,this._logger=r||voidLogger}return _createClass(Monitor,[{key:"startTime",get:function(){return this._startTime}},{key:"intervalSecs",get:function(){return this._intervalSecs}},{key:"key",get:function(){return this._key}},{key:"timeoutSecs",get:function(){return this._timeoutSecs}},{key:"serialize",value:function(){return JSON.stringify({intervalSecs:this._intervalSecs,timeoutSecs:this._timeoutSecs,key:this._key,startTime:this._startTime,asyncFetcherFn:Monitor._serializeFunction(this._asyncFetcher),compareFn:Monitor._serializeFunction(this._compareFn)})}},{key:"_debug",value:function(e){this._logger.debug("[".concat(this._key,"] - ").concat(e))}},{key:"_resetStartTime",value:function(){this._startTime=-1}},{key:"hasStarted",value:function(){return-1!==this.startTime}},{key:"isExpired",value:function(){return!!this.hasStarted()&&(Date.now()-this._startTime)/1e3>=this._timeoutSecs}},{key:"start",value:function(){var e=this;return this._debug("Monitoring..."),this.isExpired()?(this._debug("Monitor expired"),this.stop(),this):(this._handle=setTimeout((function(){return __awaiter(e,void 0,void 0,(function*(){try{var e=yield this._asyncFetcher();this._debug("Fetched: ".concat(JSON.stringify(e,null,"\t"))),this._compareFn(e)?(this._debug("Monitor predicate fulfilled"),this._emitter.emit(MonitorEvents.Fulfilled,{key:this.key,data:e}),this._resetStartTime()):this.isExpired()?(this._debug("Monitor timed out"),this._emitter.emit(MonitorEvents.Timeout,{key:this.key}),this._resetStartTime()):this.start()}catch(e){this._debug("Monitor failed: ".concat(e))}}))}),1e3*this._intervalSecs),this.hasStarted()||(this._startTime=Date.now()),this)}},{key:"stop",value:function(){clearTimeout(this._handle),this._startTime=-1}},{key:"onTimeout",value:function(e){return this._emitter.on(MonitorEvents.Timeout,e),this}},{key:"onFulfilled",value:function(e){return this._emitter.on(MonitorEvents.Fulfilled,e),this}}],[{key:"deserialize",value:function(e){var t=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],i=JSON.parse(e),n=new Monitor(Object.assign(Object.assign({},i),{asyncFetcherFn:Monitor._deserializeFunction(i.asyncFetcherFn),compareFn:Monitor._deserializeFunction(i.compareFn)}));return n._startTime=i.startTime,t&&i.startTime>-1&&n.start(),n}},{key:"_serializeFunction",value:function(e){return e.toString().replace(/\s+/g," ")}},{key:"_deserializeFunction",value:function _deserializeFunction(serialized){return eval(serialized)}}]),Monitor}(),MemoryMonitorRepository=function(){function e(){_classCallCheck(this,e),this.monitors={}}return _createClass(e,[{key:"get",value:function(e){return Promise.resolve(this.monitors[e]||null)}},{key:"getAll",value:function(){var e=this,t=Object.keys(this.monitors).map((function(t){return e.monitors[t]}));return Promise.resolve(t)}},{key:"insert",value:function(e){return this.monitors[e.key]?Promise.reject("Monitor with key '[".concat(e.key,"]' already exists")):(this.monitors[e.key]=e,e.stop(),Promise.resolve())}},{key:"remove",value:function(e){return this.monitors[e]&&delete this.monitors[e],Promise.resolve()}}]),e}(),DefaultOptions={autoStart:!0},LocalStorageMonitorRepository=function(){function e(t){var i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:DefaultOptions;_classCallCheck(this,e),this.storage=t,this.options=i}return _createClass(e,[{key:"get",value:function(e){var t=this.storage.getItem(e),i=t&&Monitor.deserialize(t,this.options.autoStart);return Promise.resolve(i||null)}},{key:"getAll",value:function(){for(var e=[],t=0;t<this.storage.length;++t){var i=this.storage.key(t),n=this.storage.getItem(i);e.push(Monitor.deserialize(n,this.options.autoStart))}return Promise.resolve(e)}},{key:"insert",value:function(e){return this.storage.getItem(e.key)?Promise.reject("Model with key '[".concat(e.key,"]' already exists")):(this.storage.setItem(e.key,e.serialize()),Promise.resolve())}},{key:"remove",value:function(e){return this.storage.getItem(e)&&this.storage.removeItem(e),Promise.resolve()}}]),e}(),ConsoleLogger=function(){function e(){_classCallCheck(this,e)}return _createClass(e,[{key:"debug",value:function(e){console.log("[DEBUG]: ".concat(e))}},{key:"error",value:function(e){console.log("[ERROR]: ".concat(e))}},{key:"log",value:function(e){console.log("[LOG]: ".concat(e))}}]),e}(),consoleLogger=new ConsoleLogger;exports.ConsoleLogger=ConsoleLogger,exports.LocalStorageMonitorRepository=LocalStorageMonitorRepository,exports.MemoryMonitorRepository=MemoryMonitorRepository,exports.Monitor=Monitor,Object.defineProperty(exports,"__esModule",{value:!0})}));
//# sourceMappingURL=signumjs.monitor.min.js.map