Cancelable search query

This commit is contained in:
Simon Cambier
2023-05-13 22:04:37 +02:00
parent 8b7cd2d3e3
commit 0d6907abbd
3 changed files with 17 additions and 6 deletions

View File

@@ -12,7 +12,6 @@
const dispatch = createEventDispatcher()
export function setInputValue(v: string): void {
console.log('setinput')
value = v
}
@@ -42,7 +41,7 @@
// the next time we open the modal, the search field will be empty
cacheManager.addToSearchHistory('')
dispatch('input', value)
}, 250)
}, 300)
</script>
<div class="omnisearch-input-container">

View File

@@ -27,6 +27,7 @@
import * as NotesIndex from '../notes-index'
import { cacheManager } from '../cache-manager'
import { searchEngine } from 'src/search/omnisearch'
import { cancelable, CancelablePromise } from 'cancelable-promise'
export let modal: OmnisearchVaultModal
let previousQuery: string | undefined
@@ -112,9 +113,20 @@
refInput?.setInputValue(searchQuery)
}
let cancelableQuery: CancelablePromise<ResultNote[]> | null = null
async function updateResults() {
// If search is already in progress, cancel it and start a new one
if (cancelableQuery) {
cancelableQuery.cancel()
cancelableQuery = null
}
query = new Query(searchQuery)
resultNotes = await searchEngine.getSuggestions(query)
cancelableQuery = cancelable(
new Promise(resolve => {
resolve(searchEngine.getSuggestions(query))
})
)
resultNotes = await cancelableQuery
selectedIndex = 0
await scrollIntoView()
}