UNPKG

@bcswaper/apis

Version:

bcswaper apis

1 lines 1.55 kB
import{conerr,isObjectEmpty}from"../utils";import{ClientSettings}from"../context";import{reverseResolution}from"./dydxContext";export class CandlesHandler{constructor(t,e){this.currentPrice=0,this.symbol=t,this.clientCallBackFuncs=e,this.candleMap=new Map}async initialize(t){const{id:e,contents:i}=t;if(await isObjectEmpty(i?.candles))return void conerr(`no initialized candles available for symbol ${this.symbol}`);const n=e?.split("/");if(n.length<2)return void conerr(`error id found: ${e}`);const s=n[1];let c=i.candles;this.candleMap.set(s,c),c.length>ClientSettings.ListLenths&&(c=c.slice(0,ClientSettings.ListLenths));const r=c[0].close;this.resetPrice(r)}async update(t){const{id:e,contents:i}=t;if(await isObjectEmpty(i))return void conerr(`no candle available to update for symbol ${this.symbol}`);const n=e?.split("/");if(n.length<2)return;const s=n[1],c=this.candleMap.get(s)||[];if(0==c?.length)return void conerr(`no initialized candles for symbol ${this.symbol}/${s}`);const r=i[0],l=r.close;if(await this.resetPrice(l),c[0].startedAt===r.startedAt?c[0]=r:(c.unshift(r),c.length>ClientSettings.ListLenths&&c.pop()),this.clientCallBackFuncs.candleFunc){const t=reverseResolution(s);if(!t)return void conerr("resolution converted error: ",s);await this.clientCallBackFuncs.candleFunc(r,t)}}async resetPrice(t){t!==this.currentPrice&&(this.currentPrice=t,this.clientCallBackFuncs.tickFunc&&await this.clientCallBackFuncs.tickFunc(this.currentPrice))}getPrice(){return this.currentPrice}getCandles(t){return this.candleMap.get(t)||[]}}