import { BaseSource } from './base-source.js'; function clamp(value, min, max) { return Math.min(max, Math.max(min, value)); } export class CsvReplaySource extends BaseSource { constructor(config = {}) { super({ replayRate: 1, dataset: [], ...config, }); this.sourceType = 'csv-replay'; this.nextPointIndex = 0; } start(startTimeMs = 0) { super.start(); this.reset(startTimeMs); } reset() { this.nextPointIndex = 0; } updateConfig(nextConfig) { const datasetChanged = nextConfig.dataset !== this.config.dataset; super.updateConfig(nextConfig); if (datasetChanged) { this.reset(); } } update(currentPlotTimeMs) { if (!this.running || !Array.isArray(this.config.dataset) || this.config.dataset.length === 0) { return []; } const replayRate = clamp(this.config.replayRate ?? 1, 0.1, 8); const targetDatasetTimeMs = currentPlotTimeMs * replayRate; const points = []; while (this.nextPointIndex < this.config.dataset.length) { const datasetPoint = this.config.dataset[this.nextPointIndex]; if (datasetPoint.timeMs > targetDatasetTimeMs) { break; } points.push({ timeMs: datasetPoint.timeMs / replayRate, value: datasetPoint.value, sourceId: this.config.id ?? 'csv-replay', }); this.nextPointIndex += 1; } return points; } }