UNPKG

@altostra/core

Version:

Core library for shared types and logic

1 lines 3.95 kB
"use strict";var _BatchTelemetryAgent_events,_BatchTelemetryAgent_timer,_BatchTelemetryAgent_options,_BatchTelemetryAgent_reporter,__awaiter=this&&this.__awaiter||function(e,t,r,i){return new(r||(r=Promise))((function(s,a){function n(e){try{_(i.next(e))}catch(e){a(e)}}function o(e){try{_(i.throw(e))}catch(e){a(e)}}function _(e){var t;e.done?s(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(n,o)}_((i=i.apply(e,t||[])).next())}))},__classPrivateFieldSet=this&&this.__classPrivateFieldSet||function(e,t,r,i,s){if("m"===i)throw new TypeError("Private method is not writable");if("a"===i&&!s)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!s:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===i?s.call(e,r):s?s.value=r:t.set(e,r),r},__classPrivateFieldGet=this&&this.__classPrivateFieldGet||function(e,t,r,i){if("a"===r&&!i)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!i:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?i:"a"===r?i.call(e):i?i.value:t.get(e)};Object.defineProperty(exports,"__esModule",{value:!0}),exports.defaultOptions=exports.BatchTelemetryAgent=void 0;const timers_1=require("timers"),Logging_1=require("../../common/Logging"),Option_1=require("../../common/Option");class BatchTelemetryAgent{constructor(e,t){_BatchTelemetryAgent_events.set(this,[]),_BatchTelemetryAgent_timer.set(this,void 0),_BatchTelemetryAgent_options.set(this,void 0),_BatchTelemetryAgent_reporter.set(this,void 0),__classPrivateFieldSet(this,_BatchTelemetryAgent_options,Object.assign(Object.assign({},exports.defaultOptions),e),"f"),__classPrivateFieldSet(this,_BatchTelemetryAgent_reporter,t,"f")}get batchIsFull(){return __classPrivateFieldGet(this,_BatchTelemetryAgent_events,"f").length>=__classPrivateFieldGet(this,_BatchTelemetryAgent_options,"f").batchSize}get batchIsEmpty(){return 0===__classPrivateFieldGet(this,_BatchTelemetryAgent_events,"f").length}get batchIsNotEmpty(){return!this.batchIsEmpty}send(e){return __awaiter(this,void 0,void 0,(function*(){__classPrivateFieldGet(this,_BatchTelemetryAgent_events,"f").push(e),this.batchIsFull?this.reportEvents():void 0===__classPrivateFieldGet(this,_BatchTelemetryAgent_timer,"f")&&this.startTimer()}))}startTimer(){__classPrivateFieldGet(this,_BatchTelemetryAgent_options,"f").reportInterval.do(e=>{__classPrivateFieldSet(this,_BatchTelemetryAgent_timer,(0,timers_1.setTimeout)(()=>{this.reportEvents()},e),"f")})}stopTimer(){__classPrivateFieldGet(this,_BatchTelemetryAgent_timer,"f")&&(clearTimeout(__classPrivateFieldGet(this,_BatchTelemetryAgent_timer,"f")),__classPrivateFieldSet(this,_BatchTelemetryAgent_timer,void 0,"f"))}reportEvents(){return __awaiter(this,void 0,void 0,(function*(){if(this.batchIsNotEmpty){const e=__classPrivateFieldGet(this,_BatchTelemetryAgent_events,"f");__classPrivateFieldSet(this,_BatchTelemetryAgent_events,[],"f");try{this.stopTimer(),yield __classPrivateFieldGet(this,_BatchTelemetryAgent_reporter,"f").reportTelemetry(e)}catch(t){this.stopTimer(),this.startTimer(),__classPrivateFieldGet(this,_BatchTelemetryAgent_options,"f").debugLogger.warn("Telemetry reporter failed to report events.",{error:t,events:e})}}}))}dispose(){return __awaiter(this,void 0,void 0,(function*(){try{yield this.reportEvents()}catch(e){this.stopTimer(),__classPrivateFieldGet(this,_BatchTelemetryAgent_options,"f").debugLogger.warn("Failed to properly dispose of telemetry agent.",e)}}))}}exports.BatchTelemetryAgent=BatchTelemetryAgent,_BatchTelemetryAgent_events=new WeakMap,_BatchTelemetryAgent_timer=new WeakMap,_BatchTelemetryAgent_options=new WeakMap,_BatchTelemetryAgent_reporter=new WeakMap,exports.defaultOptions={batchSize:10,reportInterval:(0,Option_1.none)(),debugLogger:(0,Logging_1.nullLogger)()};