mirror of
https://github.com/exchanges-lab/view.git
synced 2026-12-04 19:49:19 +08:00
2 lines
14 KiB
JavaScript
2 lines
14 KiB
JavaScript
!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?s(exports):"function"==typeof define&&define.amd?define(["exports"],s):s((e="undefined"!=typeof globalThis?globalThis:e||self).Datafeeds={})}(this,function(e){"use strict";function s(e){return void 0===e?"":"string"==typeof e?e:e.message}class t{constructor(e,s,t){this._datafeedUrl=e,this._requester=s,this._limitedServerResponse=t}getBars(e,t,r){const i={symbol:e.ticker||"",resolution:t,from:r.from,to:r.to};return void 0!==r.countBack&&(i.countback=r.countBack),void 0!==e.currency_code&&(i.currencyCode=e.currency_code),void 0!==e.unit_id&&(i.unitId=e.unit_id),new Promise(async(e,t)=>{try{const s=await this._requester.sendRequest(this._datafeedUrl,"history",i),t=this._processHistoryResponse(s);this._limitedServerResponse&&await this._processTruncatedResponse(t,i),e(t)}catch(e){if(e instanceof Error||"string"==typeof e){const r=s(e);console.warn(`HistoryProvider: getBars() failed, error=${r}`),t(r)}}})}async _processTruncatedResponse(e,t){let r=e.bars.length;try{for(;this._limitedServerResponse&&this._limitedServerResponse.maxResponseLength>0&&this._limitedServerResponse.maxResponseLength===r&&t.from<t.to;){t.countback&&(t.countback=t.countback-r),"earliestFirst"===this._limitedServerResponse.expectedOrder?t.from=Math.round(e.bars[e.bars.length-1].time/1e3):t.to=Math.round(e.bars[0].time/1e3);const s=await this._requester.sendRequest(this._datafeedUrl,"history",t),i=this._processHistoryResponse(s);r=i.bars.length,"earliestFirst"===this._limitedServerResponse.expectedOrder?(i.bars[0].time===e.bars[e.bars.length-1].time&&i.bars.shift(),e.bars.push(...i.bars)):(i.bars[i.bars.length-1].time===e.bars[0].time&&i.bars.pop(),e.bars.unshift(...i.bars))}}catch(e){if(e instanceof Error||"string"==typeof e){const t=s(e);console.warn(`HistoryProvider: getBars() warning during followup request, error=${t}`)}}}_processHistoryResponse(e){if("ok"!==e.s&&"no_data"!==e.s)throw new Error(e.errmsg);const s=[],t={noData:!1};if("no_data"===e.s)t.noData=!0,t.nextTime=e.nextTime;else{const t=void 0!==e.v,r=void 0!==e.o;for(let i=0;i<e.t.length;++i){const o={time:1e3*e.t[i],close:parseFloat(e.c[i]),open:parseFloat(e.c[i]),high:parseFloat(e.c[i]),low:parseFloat(e.c[i])};r&&(o.open=parseFloat(e.o[i]),o.high=parseFloat(e.h[i]),o.low=parseFloat(e.l[i])),t&&(o.volume=parseFloat(e.v[i])),s.push(o)}}return{bars:s,meta:t}}}class r{constructor(e,s){this._subscribers={},this._requestsPending=0,this._historyProvider=e,setInterval(this._updateData.bind(this),s)}subscribeBars(e,s,t,r){this._subscribers.hasOwnProperty(r)||(this._subscribers[r]={lastBarTime:null,listener:t,resolution:s,symbolInfo:e},e.name)}unsubscribeBars(e){delete this._subscribers[e]}_updateData(){if(!(this._requestsPending>0)){this._requestsPending=0;for(const e in this._subscribers)this._requestsPending+=1,this._updateDataForSubscriber(e).then(()=>{this._requestsPending-=1,this._requestsPending}).catch(e=>{this._requestsPending-=1,s(e),this._requestsPending})}}_updateDataForSubscriber(e){const s=this._subscribers[e],t=parseInt((Date.now()/1e3).toString()),r=t-function(e,s){let t=0;t="D"===e||"1D"===e?s:"M"===e||"1M"===e?31*s:"W"===e||"1W"===e?7*s:s*parseInt(e)/1440;return 24*t*60*60}(s.resolution,10);return this._historyProvider.getBars(s.symbolInfo,s.resolution,{from:r,to:t,countBack:2,firstDataRequest:!1}).then(s=>{this._onSubscriberDataReceived(e,s)})}_onSubscriberDataReceived(e,s){if(!this._subscribers.hasOwnProperty(e))return;const t=s.bars;if(0===t.length)return;const r=t[t.length-1],i=this._subscribers[e];if(null!==i.lastBarTime&&r.time<i.lastBarTime)return;if(null!==i.lastBarTime&&r.time>i.lastBarTime){if(t.length<2)throw new Error("Not enough bars in history for proper pulse update. Need at least 2.");const e=t[t.length-2];i.listener(e)}i.lastBarTime=r.time,i.listener(r)}}class i{constructor(e){this._subscribers={},this._requestsPending=0,this._timers=null,this._quotesProvider=e}subscribeQuotes(e,s,t,r){this._subscribers[r]={symbols:e,fastSymbols:s,listener:t},this._createTimersIfRequired()}unsubscribeQuotes(e){del
|