From 2a217a42b17e78814eecbe84028cf38b3f9327b7 Mon Sep 17 00:00:00 2001
From: Terry Truong <terry06890@gmail.com>
Date: Sat, 28 Jan 2023 22:31:38 +1100
Subject: Adjust onEventDisplay() to be more reponsive

---
 src/App.vue | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

(limited to 'src/App.vue')

diff --git a/src/App.vue b/src/App.vue
index bfd2730..c0e68a3 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -359,18 +359,31 @@ async function handleOnEventDisplay(
 	}
 }
 
+let lastEventDisplayHdlrTime = 0;
 const timelineTimeouts: Map<number, number> = new Map();
 	// Maps timeline IDs to setTimeout() timeouts (used to throttle handleEventDisplay() per-timeline)
+const EVENT_DISPLAY_HDLR_DELAY = 150;
 
 function onEventDisplay(
 		timelineId: number, eventIds: number[], firstDate: HistDate, lastDate: HistDate, scaleIdx: number){
 	if (timelineTimeouts.has(timelineId)){
 		clearTimeout(timelineTimeouts.get(timelineId));
-	}
-	timelineTimeouts.set(timelineId, window.setTimeout(async () => {
 		timelineTimeouts.delete(timelineId);
-		handleOnEventDisplay(timelineId, eventIds, firstDate, lastDate, scaleIdx);
-	}, 150));
+	}
+	const currentTime = new Date().getTime();
+	if (currentTime - lastEventDisplayHdlrTime > EVENT_DISPLAY_HDLR_DELAY){
+		lastEventDisplayHdlrTime = currentTime;
+		window.setTimeout(async () => {
+			await handleOnEventDisplay(timelineId, eventIds, firstDate, lastDate, scaleIdx);
+			lastEventDisplayHdlrTime = new Date().getTime();
+		}, EVENT_DISPLAY_HDLR_DELAY);
+	} else {
+		timelineTimeouts.set(timelineId, window.setTimeout(async () => {
+			timelineTimeouts.delete(timelineId);
+			await handleOnEventDisplay(timelineId, eventIds, firstDate, lastDate, scaleIdx);
+			lastEventDisplayHdlrTime = new Date().getTime();
+		}, EVENT_DISPLAY_HDLR_DELAY));
+	}
 }
 
 // ========== For info modal ==========
-- 
cgit v1.2.3

