From 07e53140ef1138635d20d61097eb5f4b9a769c3d Mon Sep 17 00:00:00 2001
From: Terry Truong <terry06890@gmail.com>
Date: Tue, 3 Jan 2023 23:42:36 +1100
Subject: Throttle server queries

Store previous queried range to avoid repeated duplicate queries.
In onResize(), account for edge case of 'await' taking a long time.
---
 src/components/TimeLine.vue | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

(limited to 'src/components/TimeLine.vue')

diff --git a/src/components/TimeLine.vue b/src/components/TimeLine.vue
index 76d78d0..5a69f3f 100644
--- a/src/components/TimeLine.vue
+++ b/src/components/TimeLine.vue
@@ -664,23 +664,17 @@ const tickToCount = computed((): Map<number, number> => {
 	if (ticks.value.length == 0){
 		return tickToCount;
 	}
-	let unitToTickIdx: [number, number][] = []; // Holds tick units with their tick indexes in tickToCount
+	const map = props.unitCountMaps[minorScaleIdx.value];
 	for (let tickIdx = firstIdx.value; tickIdx < lastIdx.value; tickIdx++){
 		tickToCount.set(tickIdx, 0);
-		let unit = dateToUnit(ticks.value[tickIdx].date, minorScale.value);
-		unitToTickIdx.push([unit, tickIdx]);
-	}
-	// Accumulate counts for ticks
-	const firstUnit = dateToUnit(ticks.value[firstIdx.value].date, minorScale.value);
-	const lastUnit = dateToUnit(ticks.value[lastIdx.value].date, minorScale.value);
-	for (let [unit, count] of props.unitCountMaps[minorScaleIdx.value].entries()){
-		if (unit >= firstUnit && unit < lastUnit){
-			let i = 0;
-			while (i + 1 < unitToTickIdx.length && unitToTickIdx[i + 1][0] <= unit){
-				i += 1;
+		let date = ticks.value[tickIdx].date.clone();
+		let nextDate = ticks.value[tickIdx + 1].date;
+		while (date.isEarlier(nextDate)){
+			let unit = dateToUnit(date, minorScale.value);
+			if (map.has(unit)){
+				tickToCount.set(tickIdx, tickToCount.get(tickIdx)! + map.get(unit)!);
 			}
-			const tickIdx = unitToTickIdx[i][1];
-			tickToCount.set(tickIdx, tickToCount.get(tickIdx)! + count);
+			stepDate(date, minorScale.value, {inplace: true});
 		}
 	}
 	return tickToCount;
-- 
cgit v1.2.3

