@dxfeed/dxlink-indichart
Version:
dxLink INDICHART for receiving candle and indicator data
3 lines (2 loc) • 4.97 kB
JavaScript
import{DXLinkChannelState,Logger,DXLinkLogLevel}from"@dxfeed/dxlink-core";function _arrayLikeToArray(r,a){(null==a||a>r.length)&&(a=r.length);for(var e=0,n=Array(a);e<a;e++)n[e]=r[e];return n}function _createForOfIteratorHelperLoose(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,a){if(r){if("string"==typeof r)return _arrayLikeToArray(r,a);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(r,a):void 0}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var o=0;return function(){return o>=r.length?{done:!0}:{done:!1,value:r[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _extends(){return _extends=Object.assign?Object.assign.bind():function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)({}).hasOwnProperty.call(t,r)&&(n[r]=t[r])}return n},_extends.apply(null,arguments)}var DXLinkIndiChart=/*#__PURE__*/function(){function DXLinkIndiChart(client,_indicators){var _this=this;this.id=void 0,this.dataListeners=new Set,this.indicatorsStateListeners=new Set,this.logger=void 0,this.channel=void 0,this.lastSubscription=null,this.lastSetup=null,this.lastConfig=null,this.indicators=null,this.getChannel=function(){return _this.channel},this.getState=function(){return _this.channel.getState()},this.addStateChangeListener=function(listener){return _this.channel.addStateChangeListener(listener)},this.removeStateChangeListener=function(listener){return _this.channel.removeStateChangeListener(listener)},this.addIndicatorsStateChangeListener=function(listener){return _this.indicatorsStateListeners.add(listener)},this.removeIndicatorsStateChangeListener=function(listener){return _this.indicatorsStateListeners.delete(listener)},this.setup=function(setup){_this.lastSetup=setup,_this.channel.getState()===DXLinkChannelState.OPENED&&_this.channel.send(_extends({type:"INDICHART_SETUP"},setup))},this.getConfig=function(){return _this.lastConfig},this.getIndicators=function(){return _this.indicators},this.close=function(){_this.lastSetup=null,_this.dataListeners.clear(),_this.channel.close()},this.setSubscription=function(subscription,indicatorsParameters){_this.lastSubscription={subscription:subscription,indicatorsParameters:indicatorsParameters},_this.channel.getState()===DXLinkChannelState.OPENED&&_this.channel.send(_extends({type:"INDICHART_SUBSCRIPTION"},_this.lastSubscription))},this.removeIndicators=function(indicators){_this.channel.send({type:"INDICHART_REMOVE_INDICATORS",indicators:indicators})},this.addDataListener=function(listener){_this.dataListeners.add(listener)},this.removeDataListener=function(listener){_this.dataListeners.delete(listener)},this.processMessage=function(message){if(function(message){return"INDICHART_DATA"===message.type||"INDICHART_INDICATORS"===message.type||"INDICHART_CONFIG"===message.type}(message))switch(message.type){case"INDICHART_DATA":return void _this.processData(message);case"INDICHART_CONFIG":return void(_this.lastConfig=message);case"INDICHART_INDICATORS":var indicators=message.indicators;if(_this.indicators!==indicators){_this.indicators=indicators;for(var _step,_iterator=_createForOfIteratorHelperLoose(_this.indicatorsStateListeners);!(_step=_iterator()).done;)(0,_step.value)(_this.indicators)}return}_this.logger.warn("Unknown message",message)},this.processData=function(message){for(var _step2,_iterator2=_createForOfIteratorHelperLoose(_this.dataListeners);!(_step2=_iterator2()).done;){var listener=_step2.value;try{var pending=message.pending;listener(message.candles,message.indicators,Boolean(message.reset),Boolean(pending))}catch(error){_this.logger.error("Error in data listener",error)}}},this.processStatus=function(processStatus){switch(processStatus){case DXLinkChannelState.OPENED:return _this.reconfigure();case DXLinkChannelState.REQUESTED:return;case DXLinkChannelState.CLOSED:return _this.close()}},this.processError=function(processError){_this.logger.error("Error in channel",processError)},this.channel=client.openChannel("INDICHART",{indicators:_indicators}),this.id=this.channel.id,this.channel.addMessageListener(this.processMessage),this.channel.addStateChangeListener(this.processStatus),this.channel.addErrorListener(this.processError),this.logger=new Logger(DXLinkIndiChart.name+"#"+this.id,DXLinkLogLevel.WARN)}return DXLinkIndiChart.prototype.reconfigure=function(){this.lastSubscription&&this.channel.send(_extends({type:"INDICHART_SUBSCRIPTION"},this.lastSubscription)),this.lastSetup&&this.channel.send(_extends({type:"INDICHART_SETUP"},this.lastSetup))},DXLinkIndiChart}();export{DXLinkIndiChart};
//# sourceMappingURL=index.module.js.map