From 44c8d09342d3995854b53b1360645a660dcebddb Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sat, 9 Mar 2024 17:53:04 +0530 Subject: [PATCH] Only call search API when pause in typing search query on web, desktop apps Wait for 300ms since stop typing before calling search API. This smooths out UI jitter when rendering search results, especially now that we're reranking for every search query on GPU enabled devices Emacs already has 300ms debounce time. More convoluted to add debounce time to Obsidian search modal, so not updating that yet --- src/interface/desktop/search.html | 19 ++++++++++--------- src/khoj/interface/web/search.html | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/interface/desktop/search.html b/src/interface/desktop/search.html index 4ca248c5..ffc1bf64 100644 --- a/src/interface/desktop/search.html +++ b/src/interface/desktop/search.html @@ -192,16 +192,17 @@ }); } + let debounceTimeout; function incrementalSearch(event) { - type = 'all'; - // Search with reranking on 'Enter' - if (event.key === 'Enter') { - search(rerank=true); - } - // Limit incremental search to text types - else if (type !== "image") { - search(rerank=false); - } + // Run incremental search only after waitTime passed since the last key press + let waitTime = 300; + clearTimeout(debounceTimeout); + debounceTimeout = setTimeout(() => { + type = 'all'; + // Search with reranking on 'Enter' + let should_rerank = event.key === 'Enter'; + search(rerank=should_rerank); + }, waitTime); } async function populate_type_dropdown() { diff --git a/src/khoj/interface/web/search.html b/src/khoj/interface/web/search.html index 98e37cb8..8bbd9f32 100644 --- a/src/khoj/interface/web/search.html +++ b/src/khoj/interface/web/search.html @@ -193,16 +193,17 @@ }); } + let debounceTimeout; function incrementalSearch(event) { - type = document.getElementById("type").value; - // Search with reranking on 'Enter' - if (event.key === 'Enter') { - search(rerank=true); - } - // Limit incremental search to text types - else if (type !== "image") { - search(rerank=false); - } + // Run incremental search only after waitTime passed since the last key press + let waitTime = 300; + clearTimeout(debounceTimeout); + debounceTimeout = setTimeout(() => { + type = document.getElementById("type").value; + // Search with reranking on 'Enter' + let should_rerank = event.key === 'Enter'; + search(rerank=should_rerank); + }, waitTime); } function populate_type_dropdown() {