From 67cf7da46692f86120a5e066a2f696074413d3f6 Mon Sep 17 00:00:00 2001
From: Terry Truong <terry06890@gmail.com>
Date: Tue, 24 May 2022 22:11:31 +1000
Subject: Make search suggestions appear during long user-input runs

---
 src/components/SearchModal.vue | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)

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

diff --git a/src/components/SearchModal.vue b/src/components/SearchModal.vue
index 210c87c..35efb55 100644
--- a/src/components/SearchModal.vue
+++ b/src/components/SearchModal.vue
@@ -15,9 +15,8 @@ export default defineComponent({
 			searchSuggs: [] as SearchSugg[],
 			searchHasMoreSuggs: false,
 			focusedSuggIdx: null as null | number, // Denotes a search-suggestion selected using the arrow keys
-			pendingSearchSuggReq: 0, // Set via setTimeout() upon a search-suggestion request
-				// Used to avoid sending many requests when search input is quickly changed multiple times
-			lastSuggReqId: 0, // Used to prevent late search-suggestion server-responses from taking effect
+			pendingSuggReq: 0, // Set via setTimeout() upon a search-suggestion request
+			pendingSuggReqUrl: null, // Used by a pendingSuggReq callback to use the latest user input
 		};
 	},
 	props: {
@@ -96,25 +95,20 @@ export default defineComponent({
 			url.pathname = '/data/search';
 			url.search = '?name=' + encodeURIComponent(input.value);
 			url.search += (this.uiOpts.useReducedTree ? '&tree=reduced' : '');
-			this.lastSuggReqId += 1;
-			let suggsId = this.lastSuggReqId;
-			let reqDelay = 0;
-			if (this.pendingSearchSuggReq != 0){
-				clearTimeout(this.pendingSearchSuggReq);
-				reqDelay = 200;
+			this.pendingSuggReqUrl = url.toString();
+			if (this.pendingSuggReq != 0){
+				return;
 			}
-			this.pendingSearchSuggReq = setTimeout(() =>
-				fetch(url.toString())
+			this.pendingSuggReq = setTimeout(() =>
+				fetch(this.pendingSuggReqUrl)
 					.then(response => response.json())
 					.then((results: SearchSuggResponse) => {
-						if (this.lastSuggReqId == suggsId){
-							this.searchSuggs = results[0];
-							this.searchHasMoreSuggs = results[1];
-							this.focusedSuggIdx = null;
-							this.pendingSearchSuggReq = 0;
-						}
+						this.searchSuggs = results[0];
+						this.searchHasMoreSuggs = results[1];
+						this.focusedSuggIdx = null;
+						this.pendingSuggReq = 0;
 					}),
-				reqDelay
+				200
 			);
 		},
 		onDownKey(){
-- 
cgit v1.2.3

