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 loading = $state(false)
|
||||||
let searchResults: TrustedEvent[] = $state([])
|
let searchResults: TrustedEvent[] = $state([])
|
||||||
let searchInput: HTMLInputElement | undefined = $state()
|
let searchInput: HTMLInputElement | undefined = $state()
|
||||||
let currentSearchId = 0
|
|
||||||
let controller: AbortController | undefined
|
let controller: AbortController | undefined
|
||||||
|
|
||||||
let limit = $state(20)
|
let limit = $state(20)
|
||||||
@@ -157,7 +156,7 @@
|
|||||||
controller = undefined
|
controller = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
const search = debounce(300, async (searchTerm: string, searchId: number) => {
|
const search = debounce(300, async (searchTerm: string) => {
|
||||||
controller?.abort()
|
controller?.abort()
|
||||||
|
|
||||||
if (!searchTerm.trim()) {
|
if (!searchTerm.trim()) {
|
||||||
@@ -177,27 +176,19 @@
|
|||||||
filters: [{kinds: [MESSAGE, ...CONTENT_KINDS], search: searchTerm.trim()}],
|
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) {
|
} catch (error) {
|
||||||
if (
|
if (!(error instanceof DOMException && error.name === "AbortError")) {
|
||||||
!(error instanceof DOMException && error.name === "AbortError") &&
|
|
||||||
searchId === currentSearchId
|
|
||||||
) {
|
|
||||||
searchResults = []
|
searchResults = []
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (searchId === currentSearchId) {
|
loading = false
|
||||||
loading = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const onInput = () => {
|
const onInput = () => {
|
||||||
showSearch = true
|
showSearch = true
|
||||||
currentSearchId += 1
|
void search(term)
|
||||||
void search(term, currentSearchId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const onResultClick = (event: TrustedEvent) => {
|
const onResultClick = (event: TrustedEvent) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user