UNPKG

14.4 kBJavaScriptView Raw
1"use strict";var __extends=this&&this.__extends||function(){var n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])};return function(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}}(),__decorate=this&&this.__decorate||function(t,e,r,n){var s,i=arguments.length,o=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)o=Reflect.decorate(t,e,r,n);else for(var a=t.length-1;0<=a;a--)(s=t[a])&&(o=(i<3?s(o):3<i?s(e,r,o):s(e,r))||o);return 3<i&&o&&Object.defineProperty(e,r,o),o},__metadata=this&&this.__metadata||function(t,e){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(t,e)},__param=this&&this.__param||function(r,n){return function(t,e){n(t,e,r)}};Object.defineProperty(exports,"__esModule",{value:!0});var dispatch_1=require("./dispatch"),codec_1=require("./codec"),core_1=require("./core"),delivery=require("@jayesol/jayeson.lib.delivery"),jayeson_lib_delivery_1=require("@jayesol/jayeson.lib.delivery"),Collections=require("typescript-collections"),T=require("ts-promise"),data_structure_1=require("./data_structure"),receive_1=require("./receive"),message_class_1=require("./message_class");require("reflect-metadata");var injection_js_1=require("injection-js"),InsertMatch=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.InsertMatch=InsertMatch;var UpdateMatch=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.UpdateMatch=UpdateMatch;var DeleteMatch=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.DeleteMatch=DeleteMatch;var InsertEvent=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.InsertEvent=InsertEvent;var UpdateEvent=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.UpdateEvent=UpdateEvent;var DeleteEvent=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.DeleteEvent=DeleteEvent;var InsertOdd=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.InsertOdd=InsertOdd;var UpdateOdd=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.UpdateOdd=UpdateOdd;var DeleteOdd=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.DeleteOdd=DeleteOdd;var Reset=function(){function t(t,e){this._after=t,this._get=e}return t.prototype.after=function(){return this._after},t.prototype.get=function(){return this._get},t}();exports.Reset=Reset;var FeedView=function(){function t(t,e,r){this._epgm=t,this._fsRepo=e,this._grp=r,this._handlers=[]}return t.prototype.fsRepo=function(){return this._fsRepo},t.prototype.epgm=function(){return this._epgm},t.prototype.grp=function(){return this._grp},t.prototype.snapshot=function(){return this._fsRepo.getSnapshot("")},t.prototype.register=function(t){if(this.hasHandler(t))console.warn("Handler already registered.");else{var e=new MemoryDispatcher(this._fsRepo,this._grp,t);this._epgm.registerEPD(e),this._handlers.push(new HandlerDispatcherTuple(t,e))}},t.prototype.unregister=function(t){var e=this.removeHandler(t);null!=e&&this._epgm.deregisterEPD(e)},t.prototype.hasHandler=function(e){return this._handlers.some(function(t){return t.handler===e})},t.prototype.removeHandler=function(e){var t=this._handlers.find(function(t){return t.handler===e});if(void 0===t)return null;var r=this._handlers.indexOf(t);return this._handlers.splice(r,1),t.dispatch},t.prototype.handlerSize=function(){return this._handlers.length},t.prototype.cleanUp=function(){for(var t=0,e=this._handlers.slice(0);t<e.length;t++){var r=e[t];this.unregister(r.handler)}},t=__decorate([injection_js_1.Injectable(),__param(0,injection_js_1.Inject(dispatch_1.EPGM_IMPL)),__param(1,injection_js_1.Inject(core_1.FSREPO_IMPL)),__metadata("design:paramtypes",[Object,Object,message_class_1.SportsFeedMessageGroup])],t)}();exports.FeedView=FeedView;var HandlerDispatcherTuple=function(t,e){this.handler=t,this.dispatch=e},AbstractComparableEPDispatcher=function(){function t(t,e){this._stream=t,this._grp=e}return t.prototype.id=function(){throw new Error("Method not implemented.")},t.prototype.submit=function(t){throw new Error("Method not implemented.")},t.prototype.onException=function(t){throw new Error("Method not implemented.")},t.prototype.stream=function(){return this._stream},t.prototype.grp=function(){return this._grp},t.prototype.isInitialized=function(){return null},t.prototype.readyToDispatch=function(){this._readyToDispatch=!0},t.prototype.isReadyToDispatch=function(){return this._readyToDispatch},t}(),MemoryDispatcher=function(s){function t(t,e,r){var n=s.call(this,i.MEMORY_STREAM,e)||this;return n._fsRepo=t,n.lastPushedSs=data_structure_1.IndexedSnapshotImpl.EMPTY_SNAPSHOT,n._grp=e,n._handler=r,n._id=delivery.generateUUID(),n}var i;return __extends(t,s),(i=t).prototype.grp=function(){return this._grp},t.prototype.handler=function(){return this._handler},t.prototype.id=function(){return this._id},t.prototype.submit=function(t){this.isInitialized().isFulfilled()&&this.isInitialized().value()&&this.handleMessage(t)},t.prototype.onException=function(t){console.error("Exception when dispatching outgoing messages to dispatchers:"+t)},t.prototype.handleMessage=function(t){if(this.grp().isIndicatorMessage(t.msgType()))if(t.msgType()==this._grp.SWITCH_FILTER_START()){var e=Number(t.msg());this.pushSwitchFilterStart(e)}else if(t.msgType()==this._grp.SWITCH_FILTER_END())this.pushSwitchFilterEnd();else if(t.msgType()==this._grp.FULLSNAPSHOT_START())this.pushFullSnapshotStart();else if(t.msgType()==this._grp.FULLSNAPSHOT_END())this.pushFullSnapshotEnd();else if(t.msgType()==this._grp.SWITCH_FILTER_FAIL()){e=Number(t.msg());this.pushSwitchFilterFail(e)}else t.msgType()==this._grp.TTL_RESTORE_START()?this.pushFullSnapshotStart():t.msgType()==this._grp.TTL_RESTORE_END()&&this.pushFullSnapshotEnd();else{var r=t;this.isSame(r.msgType(),this._grp.DATA_INSERT_MATCH())?this.pushInsertMatch(r):this.isSame(r.msgType(),this._grp.DATA_INSERT_EVENT())?this.pushInsertEvent(r):this.isSame(r.msgType(),this._grp.DATA_INSERT_ODD())?this.pushInsertOdd(r):this.isSame(r.msgType(),this._grp.DATA_UPDATE_MATCH())?this.pushUpdateMatch(r):this.isSame(r.msgType(),this._grp.DATA_UPDATE_EVENT())?this.pushUpdateEvent(r):this.isSame(r.msgType(),this._grp.DATA_UPDATE_ODD())?this.pushUpdateOdd(r):this.isSame(r.msgType(),this._grp.DATA_DELETE_MATCH())?this.pushDeleteMatch(r):this.isSame(r.msgType(),this._grp.DATA_DELETE_EVENT())?this.pushDeleteEvent(r):this.isSame(r.msgType(),this._grp.DATA_DELETE_ODD())?this.pushDeleteOdd(r):this.isSame(r.msgType(),this._grp.DATA_RESET())&&this.pushReset(r)}},t.prototype.pushSwitchFilterStart=function(t){this._handler.onSwitchFilterStart(t)},t.prototype.pushSwitchFilterEnd=function(){this._handler.onSwitchFilterEnd()},t.prototype.pushFullSnapshotStart=function(){this._handler.onFullSnapshotStart()},t.prototype.pushFullSnapshotEnd=function(){this._handler.onFullSnapshotEnd()},t.prototype.pushSwitchFilterFail=function(t){this._handler.onSwitchFilterFail(t)},t.prototype.pushInsertMatch=function(t){for(var e=[],r=[],n=0,s=t.delta().matches();n<s.length;n++){var i=s[n];t.before().hasMatch(i.id())?r.push(i):e.push(i)}if(0!=e.length){var o=new InsertMatch(t.after(),e);this._handler.onInsertMatch(o)}if(0!=r.length){var a=new UpdateMatch(t.after(),r);this._handler.onUpdateMatch(a)}},t.prototype.pushInsertEvent=function(t){for(var e=[],r=[],n=0,s=this.events(t.delta().matches());n<s.length;n++){var i=s[n],o=t.before().match(i.matchId());null!=o&&o.hasEvent(i.id())?r.push(i):e.push(i)}if(0!=e.length){var a=new InsertEvent(t.after(),e);this._handler.onInsertEvent(a)}if(0<r.length){var p=new UpdateEvent(t.after(),r);this._handler.onUpdateEvent(p)}},t.prototype.pushInsertOdd=function(t){var e=this.records(this.events(t.delta().matches()));if(0<e.length){var r=new InsertOdd(t.after(),e);this._handler.onInsertOdd(r)}},t.prototype.pushUpdateMatch=function(t){var e=t.delta().matches();if(0<e.length){var r=new UpdateMatch(t.after(),e);this._handler.onUpdateMatch(r)}},t.prototype.pushUpdateEvent=function(t){var e=this.events(t.delta().matches());if(0<e.length){var r=new UpdateEvent(t.after(),e);this._handler.onUpdateEvent(r)}},t.prototype.pushUpdateOdd=function(t){var e=this.records(this.events(t.delta().matches()));if(0<e.length){var r=new UpdateOdd(t.after(),e);this._handler.onUpdateOdd(r)}},t.prototype.pushDeleteMatch=function(t){for(var e=[],r=[],n=0,s=t.delta().matches();n<s.length;n++){var i=s[n];t.after().hasMatch(i.id())?e.push(i):r.push(i)}if(0!=r.length){var o=new DeleteMatch(t.after(),r);this._handler.onDeleteMatch(o)}if(0!=e.length){var a=new UpdateMatch(t.after(),e);this._handler.onUpdateMatch(a)}},t.prototype.pushDeleteEvent=function(t){for(var e=[],r=[],n=0,s=this.events(t.delta().matches());n<s.length;n++){var i=s[n],o=t.after().match(i.matchId());null!=o&&o.hasEvent(i.id())?e.push(i):r.push(i)}if(0!=r.length){var a=new DeleteEvent(t.after(),r);this._handler.onDeleteEvent(a)}if(0!=e.length){var p=new UpdateEvent(t.after(),e);this._handler.onUpdateEvent(p)}},t.prototype.pushDeleteOdd=function(t){var e=this.records(this.events(t.delta().matches()));if(0<e.length){var r=new DeleteOdd(t.after(),e);this._handler.onDeleteOdd(r)}},t.prototype.pushReset=function(t){var e=t.delta().getPartitions().toArray(),r=new Reset(t.after(),e);this._handler.onReset(r)},t.prototype.events=function(t){for(var e=[],r=0,n=t;r<n.length;r++)for(var s=0,i=n[r].events();s<i.length;s++){var o=i[s];e.push(o)}return e},t.prototype.records=function(t){for(var e=[],r=0,n=t;r<n.length;r++)for(var s=0,i=n[r].records();s<i.length;s++){var o=i[s];e.push(o)}return e},t.prototype.states=function(t){for(var e=[],r=0,n=t;r<n.length;r++)for(var s=0,i=n[r].eventStates();s<i.length;s++){var o=i[s];e.push(o)}return e},t.prototype.isSame=function(t,e){return t.id()==e.id()},t.prototype.isInitialized=function(){return this._fsRepo.isReady(this.stream())},t.MEMORY_STREAM="MEMORY_AGGREGATED_STREAM",t=i=__decorate([injection_js_1.Injectable(),__metadata("design:paramtypes",[Object,message_class_1.SportsFeedMessageGroup,Object])],t)}(exports.AbstractComparableEPDispatcher=AbstractComparableEPDispatcher);exports.MemoryDispatcher=MemoryDispatcher;var SportsFeedClient=function(){function t(t,e,r,n,s,i){this._subscriber=n,this._epgm=r,this._eventHandler=s,this.configureEventHandler(s),this._fsRepo=e,this._grp=t,this._views=new Set,this._viewTemplate=this.view(),this._viewTemplate.register(this);this.resolverMap=new Collections.Dictionary,this.filterRequestCounter=0,i.startTtlVerification()}return t.prototype.subscriber=function(){return this._subscriber},t.prototype.epgm=function(){return this._epgm},t.prototype.InEndPointEventHandler=function(){return this._eventHandler},t.prototype.fsRepo=function(){return this._fsRepo},t.prototype.grp=function(){return this._grp},t.prototype.eventHandler=function(){return this._eventHandler},t.prototype.eventListener=function(t){if(t instanceof jayeson_lib_delivery_1.Subscriber.Event.ConsumptionStart)for(var e=0,r=t.streams;e<r.length;e++){var n=r[e];console.log("ConsumptionsStartEvent for streams "+n+" from "+t.endPoint)}else if(t instanceof jayeson_lib_delivery_1.Subscriber.Event.ConsumptionError)for(var s=0,i=t.streams;s<i.length;s++){n=i[s];console.log("ConsumptionsStartEvent for streams "+n)}else console.log("Received event "+t+" from "+t.endPoint)},t.prototype.configureEventHandler=function(t){this._subscriber.attachListener(t)},t.prototype.setUpStreamFilter=function(t){var r=this;if(0===Object.keys(this._eventHandler.streamSources()).length){var e=new codec_1.FilterRequest(codec_1.FilterRequest.IGNORED_REQUEST_ID,t);this._eventHandler.updateFilter(e);var n=null,s=new T.Promise(function(t,e){n=t});return n(!0),s}var i=this.filterRequestCounter++,o=new codec_1.FilterRequest(i,t),a=new T.Promise(function(t,e){r.resolverMap.setValue(i,t)});return this._eventHandler.updateFilter(o),a},t.prototype.getSubscriber=function(){return this._subscriber},t.prototype.start=function(){this.getSubscriber().start()},t.prototype.view=function(){var t=new FeedView(this._epgm,this._fsRepo,this._grp);return this._views.add(t),t},t.prototype.removeView=function(t){this._views.has(t)&&(t.cleanUp(),this._views.delete(t))},t.prototype.onSwitchFilterFail=function(t){t!=codec_1.FilterRequest.IGNORED_REQUEST_ID&&this.resolverMap.remove(t)(!1)},t.prototype.onSwitchFilterStart=function(t){t!=codec_1.FilterRequest.IGNORED_REQUEST_ID&&this.resolverMap.remove(t)(!0)},t.prototype.onInsertMatch=function(t){},t.prototype.onUpdateMatch=function(t){},t.prototype.onDeleteMatch=function(t){},t.prototype.onInsertEvent=function(t){},t.prototype.onUpdateEvent=function(t){},t.prototype.onDeleteEvent=function(t){},t.prototype.onInsertOdd=function(t){},t.prototype.onUpdateOdd=function(t){},t.prototype.onDeleteOdd=function(t){},t.prototype.onReset=function(t){},t.prototype.onSwitchFilterEnd=function(){},t.prototype.onFullSnapshotStart=function(){},t.prototype.onFullSnapshotEnd=function(){},t=__decorate([injection_js_1.Injectable(),__param(1,injection_js_1.Inject(core_1.FSREPO_IMPL)),__param(2,injection_js_1.Inject(dispatch_1.EPGM_IMPL)),__metadata("design:paramtypes",[message_class_1.SportsFeedMessageGroup,Object,Object,jayeson_lib_delivery_1.Subscriber,receive_1.InEndPointEventHandler,receive_1.SportsFeedInProcessor])],t)}();exports.SportsFeedClient=SportsFeedClient;
\No newline at end of file