Skip to content

Commit 1ce8510

Browse files
committed
feat: wrap committedSearchQuery update in a debounce
1 parent 5d8fcf5 commit 1ce8510

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

app/composables/useGlobalSearch.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { debounce } from 'perfect-debounce'
44
// Pages that have their own local filter using ?q
55
const pagesWithLocalFilter = new Set(['~username', 'org'])
66

7+
const SEARCH_DEBOUNCE_MS = 250
8+
79
export function useGlobalSearch(place: 'header' | 'content' = 'content') {
810
const { settings } = useSettings()
911
const { searchProvider } = useSearchProvider()
@@ -27,10 +29,14 @@ export function useGlobalSearch(place: 'header' | 'content' = 'content') {
2729
// Syncs instantly when instantSearch is on, but only on Enter press when off
2830
const committedSearchQuery = useState<string>('committed-search-query', () => searchQuery.value)
2931

32+
const commitSearchQuery = debounce((val: string) => {
33+
committedSearchQuery.value = val
34+
}, SEARCH_DEBOUNCE_MS)
35+
3036
// This is basically doing instant search as user types
3137
watch(searchQuery, val => {
3238
if (settings.value.instantSearch) {
33-
committedSearchQuery.value = val
39+
commitSearchQuery(val)
3440
}
3541
})
3642

@@ -71,10 +77,11 @@ export function useGlobalSearch(place: 'header' | 'content' = 'content') {
7177
})
7278
}
7379

74-
const updateUrlQuery = debounce(updateUrlQueryImpl, 250)
80+
const updateUrlQuery = debounce(updateUrlQueryImpl, SEARCH_DEBOUNCE_MS)
7581

7682
function flushUpdateUrlQuery() {
7783
// Commit the current query when explicitly submitted (Enter pressed)
84+
commitSearchQuery.cancel()
7885
committedSearchQuery.value = searchQuery.value
7986
// When instant search is off the debounce queue is empty, so call directly
8087
if (!settings.value.instantSearch) {

0 commit comments

Comments
 (0)