Загрузка данных


useEffect(() => {
  const nextSymbolInfo: SymbolInfoInput = {
    symbolId: symbolInfo.symbolId,
    symbol: symbolInfo.symbol,
    symbolName: symbolInfo.symbolName,
  };

  const currentSymbolInfo = currentSymbolInfoRef.current;

  const isSameSymbolInfo =
    currentSymbolInfo.symbolId === nextSymbolInfo.symbolId &&
    currentSymbolInfo.symbol === nextSymbolInfo.symbol &&
    currentSymbolInfo.symbolName === nextSymbolInfo.symbolName;

  if (isSameSymbolInfo) {
    return;
  }

  currentSymbolInfoRef.current = nextSymbolInfo;
  chartRef.current?.setSymbol(nextSymbolInfo);
}, [symbolInfo.symbolId, symbolInfo.symbol, symbolInfo.symbolName]);





function getCandleTimeInSeconds(time: number): number {
  return time > 1e10 ? Math.floor(time / 1000) : Math.floor(time);
}

public getDataSource =
  (indicativeData?: ChartIndicativeData, cb?: (timeframe: Timeframes) => void) =>
  async (timeframe: Timeframes, symbolId: string, until?: Candle): Promise<Candle[] | null> => {
    const symbol = getRequestSymbol(symbolId);

    if (!symbol) {
      return null;
    }

    cb?.(timeframe);

    const interval = moexChartTimeConverter(timeframe);
    const untilTime = until ? getCandleTimeInSeconds(until.time) : undefined;

    const date =
      untilTime === undefined
        ? Math.round(Date.now() / 1000)
        : untilTime - 1;

    const data = await requestBars({
      currencyPair: symbol.replaceAll(':', '.'),
      interval,
      periodParams: {
        firstDataRequest: true,
        to: date,
        from: Date.now(),
        countBack: 2000,
      },
      ticker: symbol,
      indicativeData,
    });

    const historyData =
      untilTime === undefined
        ? data
        : data.filter(({ time }) => {
            return getCandleTimeInSeconds(time) < untilTime;
          });

    if (historyData.length === 0) {
      return null;
    }

    const issTimeframe = moexChartToIssTimeframe(timeframe);

    if (issTimeframe === timeframe) {
      this.realtimeShouldBeConvoluted = false;

      return historyData;
    }

    this.realtimeShouldBeConvoluted = true;

    const convolutedData = timeframeConvolution(historyData, timeframe);

    return convolutedData.length === 0 ? null : convolutedData;
  };