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


public async loadMoreHistory(): Promise<void> {
  await this.ready();

  if (this.isEndOfData) {
    return;
  }

  if (this.loadingPromise) {
    return this.loadingPromise;
  }

  const until = this.oldestCandle;

  if (!until) {
    return;
  }

  const seq = ++this.loadSeq;
  const tf = ensureDefined(this.getTimeframe());

  const task = (async () => {
    this.isLoadingSubject.next(true);

    try {
      this.saveRealtimeCache();

      if (seq !== this.loadSeq) {
        return;
      }

      const olderData = await this.getData(tf, this.symbol, until);

      if (seq !== this.loadSeq) {
        return;
      }

      if (olderData === null) {
        this.isEndOfData = true;
        return;
      }

      const older = this.normalizeList(tf, olderData);

      if (older.length === 0) {
        return;
      }

      const current = this.currentDataSubject.value;
      const combinedRaw = SymbolSource.mergeHistory(older, current);
      const combined = this.normalizeList(tf, combinedRaw);

      const nextOldest = combined[0] ?? null;

      if (!nextOldest || nextOldest.time >= until.time) {
        return;
      }

      this.oldestCandle = nextOldest;
      this.newestCandle = combined[combined.length - 1] ?? this.newestCandle;

      this.currentDataSubject.next(combined);
      this.lastCandleSubject.next(this.newestCandle);
    } catch (error) {
      if (seq === this.loadSeq) {
        console.error('[DataSource] Ошибка при догрузке истории:', error);
      }
    } finally {
      if (seq === this.loadSeq) {
        this.isLoadingSubject.next(false);
      }
    }
  })();

  this.loadingPromise = task;

  task.finally(() => {
    if (this.loadingPromise === task) {
      this.loadingPromise = null;
    }
  });

  await task;
}