perf(tracks): bound playback rendering work

This commit is contained in:
lingniu
2026-07-16 04:06:08 +08:00
parent 982acb7730
commit 8e8f3f9f5c
7 changed files with 143 additions and 25 deletions

View File

@@ -16,6 +16,30 @@ export function sampledEventIndex(event: TrackPlaybackEvent, sampledCount: numbe
return Math.max(0, Math.min(sampledCount - 1, Math.round(event.index * (sampledCount - 1) / (originalCount - 1))));
}
export function trackPlaybackInterval(speed: number) {
return Math.max(55, 300 / Math.max(0.5, speed));
}
export function buildTrackRailSelection(track?: TrackPlaybackResponse) {
const pointCount = track?.points.length ?? 0;
const stopIndexesByPoint: Array<number[] | undefined> = new Array(pointCount);
const eventIndexesByPoint: Array<number[] | undefined> = new Array(pointCount);
if (!track || pointCount === 0) return { stopIndexesByPoint, eventIndexesByPoint };
track.stops.forEach((stop, stopIndex) => {
for (let offset = -1; offset <= 1; offset += 1) {
const pointIndex = stop.sampledIndex + offset;
if (pointIndex < 0 || pointIndex >= pointCount) continue;
(stopIndexesByPoint[pointIndex] ??= []).push(stopIndex);
}
});
track.events.forEach((event, eventIndex) => {
const pointIndex = sampledEventIndex(event, pointCount, track.summary.pointCount);
(eventIndexesByPoint[pointIndex] ??= []).push(eventIndex);
});
return { stopIndexesByPoint, eventIndexesByPoint };
}
function csvCell(value: unknown) {
const text = String(value ?? '');
return /[",\n]/.test(text) ? `"${text.replace(/"/g, '""')}"` : text;