refactor: remove redundant searchId tracking
AbortController already handles cancellation of stale requests, making the searchId/currentSearchId pattern unnecessary.
This commit is contained in:
@@ -114,7 +114,6 @@
|
||||
let loading = $state(false)
|
||||
let searchResults: TrustedEvent[] = $state([])
|
||||
let searchInput: HTMLInputElement | undefined = $state()
|
||||
let currentSearchId = 0
|
||||
let controller: AbortController | undefined
|
||||
|
||||
let limit = $state(20)
|
||||
@@ -157,7 +156,7 @@
|
||||
controller = undefined
|
||||
}
|
||||
|
||||
const search = debounce(300, async (searchTerm: string, searchId: number) => {
|
||||
const search = debounce(300, async (searchTerm: string) => {
|
||||
controller?.abort()
|
||||
|
||||
if (!searchTerm.trim()) {
|
||||
@@ -177,27 +176,19 @@
|
||||
filters: [{kinds: [MESSAGE, ...CONTENT_KINDS], search: searchTerm.trim()}],
|
||||
})
|
||||
|
||||
if (searchId === currentSearchId) {
|
||||
searchResults = sortEventsDesc(uniqBy((e: TrustedEvent) => e.id, events))
|
||||
}
|
||||
searchResults = sortEventsDesc(uniqBy((e: TrustedEvent) => e.id, events))
|
||||
} catch (error) {
|
||||
if (
|
||||
!(error instanceof DOMException && error.name === "AbortError") &&
|
||||
searchId === currentSearchId
|
||||
) {
|
||||
if (!(error instanceof DOMException && error.name === "AbortError")) {
|
||||
searchResults = []
|
||||
}
|
||||
} finally {
|
||||
if (searchId === currentSearchId) {
|
||||
loading = false
|
||||
}
|
||||
loading = false
|
||||
}
|
||||
})
|
||||
|
||||
const onInput = () => {
|
||||
showSearch = true
|
||||
currentSearchId += 1
|
||||
void search(term, currentSearchId)
|
||||
void search(term)
|
||||
}
|
||||
|
||||
const onResultClick = (event: TrustedEvent) => {
|
||||
|
||||
Reference in New Issue
Block a user