UNPKG

@dxfeed/dxlink-indichart

Version:

dxLink INDICHART for receiving candle and indicator data

3 lines (2 loc) 5.25 kB
var dxlinkCore=require("@dxfeed/dxlink-core");class DXLinkIndiChart{constructor(client,_indicators){this.id=void 0,this.updateListeners=new Set,this.candleSnapshotListeners=new Set,this.indicatorsSnapshotListeners=new Set,this.indicatorsStateListeners=new Set,this.logger=void 0,this.channel=void 0,this.lastSubscriptionState=null,this.lastSetup=null,this.lastConfig=null,this.indicators=null,this.getChannel=()=>this.channel,this.getState=()=>this.channel.getState(),this.addStateChangeListener=listener=>this.channel.addStateChangeListener(listener),this.removeStateChangeListener=listener=>this.channel.removeStateChangeListener(listener),this.addIndicatorsStateChangeListener=listener=>this.indicatorsStateListeners.add(listener),this.removeIndicatorsStateChangeListener=listener=>this.indicatorsStateListeners.delete(listener),this.setup=setup=>{this.lastSetup=setup,this.channel.getState()===dxlinkCore.DXLinkChannelState.OPENED&&this.channel.send({type:"INDICHART_SETUP",...setup})},this.getConfig=()=>this.lastConfig,this.getIndicators=()=>this.indicators,this.close=()=>{this.lastSetup=null,this.updateListeners.clear(),this.candleSnapshotListeners.clear(),this.indicatorsSnapshotListeners.clear(),this.channel.close()},this.setSubscription=(subscription,indicatorsParameters)=>{this.lastSubscriptionState={subscription:subscription,indicatorsParameters:indicatorsParameters},this.channel.getState()===dxlinkCore.DXLinkChannelState.OPENED&&this.channel.send({type:"INDICHART_SUBSCRIPTION",...this.lastSubscriptionState})},this.getSubscription=()=>this.lastSubscriptionState,this.removeIndicators=indicators=>{this.channel.send({type:"INDICHART_REMOVE_INDICATORS",indicators:indicators})},this.addUpdateListener=listener=>{this.updateListeners.add(listener)},this.removeUpdateListener=listener=>{this.updateListeners.delete(listener)},this.addCandleSnapshotListener=listener=>{this.candleSnapshotListeners.add(listener)},this.removeCandleSnapshotListener=listener=>{this.candleSnapshotListeners.delete(listener)},this.addIndicatorsSnapshotListener=listener=>{this.indicatorsSnapshotListeners.add(listener)},this.removeIndicatorsSnapshotListener=listener=>{this.indicatorsSnapshotListeners.delete(listener)},this.processMessage=message=>{if((message=>"INDICHART_UPDATE"===message.type||"INDICHART_INDICATORS"===message.type||"INDICHART_CONFIG"===message.type||"INDICHART_CANDLE_SNAPSHOT"===message.type||"INDICHART_INDICATORS_SNAPSHOT"===message.type)(message))switch(message.type){case"INDICHART_UPDATE":return this.processUpdate(message);case"INDICHART_CANDLE_SNAPSHOT":return this.processCandleSnapshot(message);case"INDICHART_INDICATORS_SNAPSHOT":return this.processIndicatorsSnapshot(message);case"INDICHART_CONFIG":return void(this.lastConfig=message);case"INDICHART_INDICATORS":{const indicators=message.indicators;if(this.indicators!==indicators){this.indicators=indicators;for(const listener of this.indicatorsStateListeners)listener(this.indicators)}return}}this.logger.warn("Unknown message",message)},this.processUpdate=message=>{for(const listener of this.updateListeners)try{const{pending:pending,candles:candles,indicators:indicators}=message;listener(candles,indicators,Boolean(pending))}catch(error){this.logger.error("Error in update listener",error)}},this.processCandleSnapshot=message=>{for(const listener of this.candleSnapshotListeners)try{const{reset:reset,pending:pending,candles:candles}=message;listener(candles,Boolean(reset),Boolean(pending))}catch(error){this.logger.error("Error in candle snapshot listener",error)}},this.processIndicatorsSnapshot=message=>{for(const listener of this.indicatorsSnapshotListeners)try{const{pending:pending,indicators:indicators}=message;listener(indicators,Boolean(pending))}catch(error){this.logger.error("Error in indicators snapshot listener",error)}},this.processStatus=processStatus=>{switch(processStatus){case dxlinkCore.DXLinkChannelState.OPENED:return this.reconfigure();case dxlinkCore.DXLinkChannelState.REQUESTED:return;case dxlinkCore.DXLinkChannelState.CLOSED:return this.close()}},this.processError=processError=>{this.logger.error("Error in channel",processError)},this.updateIndicatorsParameters=indicatorsParameters=>{console.log("Updating indi params",indicatorsParameters),this.lastSubscriptionState?(this.lastSubscriptionState={...this.lastSubscriptionState,indicatorsParameters:indicatorsParameters},this.channel.getState()===dxlinkCore.DXLinkChannelState.OPENED&&this.channel.send({type:"INDICHART_SUBSCRIPTION",...this.lastSubscriptionState})):this.logger.warn("Cannot update indicators parameters, subscription is not set")},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 dxlinkCore.Logger(`${DXLinkIndiChart.name}#${this.id}`,dxlinkCore.DXLinkLogLevel.WARN)}reconfigure(){this.lastSubscriptionState&&this.channel.send({type:"INDICHART_SUBSCRIPTION",...this.lastSubscriptionState}),this.lastSetup&&this.channel.send({type:"INDICHART_SETUP",...this.lastSetup})}}exports.DXLinkIndiChart=DXLinkIndiChart; //# sourceMappingURL=index.js.map