⏱️ debounce user picker search, fix stale abort check
Browse files at this commit379022431dfc70ca2fb46a490becc01abdb9ffedParent: 723ae1e- fix
- ⏱️ debounce user picker search, fix stale abort check
1 changed file +16 -13
src/components/users/user-search-select.tsxmodified+16 -13
| @@ -1,4 +1,4 @@ | |||
| 1 | -import { For, Show, createEffect, createMemo, createSignal } from "solid-js"; | 1 | +import { For, Show, createEffect, createMemo, createSignal, onCleanup } from "solid-js"; |
| 2 | import { getUserSearch } from "@/api/getUserSearch"; | 2 | import { getUserSearch } from "@/api/getUserSearch"; |
| 3 | import type { PersonRef, Role } from "@/api/types"; | 3 | import type { PersonRef, Role } from "@/api/types"; |
| 4 | import { Input } from "@/components/ui/input"; | 4 | import { Input } from "@/components/ui/input"; |
| @@ -46,23 +46,26 @@ export function UserSearchSelect(props: { | |||
| 46 | }); | 46 | }); |
| 47 | 47 | ||
| 48 | let abortController: AbortController | null = null; | 48 | let abortController: AbortController | null = null; |
| 49 | - createEffect(async () => { | 49 | + createEffect(() => { |
| 50 | const q = query().trim(); | 50 | const q = query().trim(); |
| 51 | - if (abortController) abortController.abort(); | 51 | + abortController?.abort(); |
| 52 | if (q.length === 0) { | 52 | if (q.length === 0) { |
| 53 | setUsers([]); | 53 | setUsers([]); |
| 54 | return; | 54 | return; |
| 55 | } | 55 | } |
| 56 | - abortController = new AbortController(); | 56 | + // Debounce keystrokes so a typed name is one request, not one per letter. |
| 57 | - setLoading(true); | 57 | + const timer = window.setTimeout(() => { |
| 58 | - try { | 58 | + const controller = new AbortController(); |
| 59 | - const data = await getUserSearch(q, abortController.signal, props.role); | 59 | + abortController = controller; |
| 60 | - setUsers(data.items); | 60 | + setLoading(true); |
| 61 | - } catch { | 61 | + void getUserSearch(q, controller.signal, props.role) |
| 62 | - if (!abortController?.signal.aborted) setUsers([]); | 62 | + .then((data) => setUsers(data.items)) |
| 63 | - } finally { | 63 | + .catch(() => { |
| 64 | - if (!abortController?.signal.aborted) setLoading(false); | 64 | + if (!controller.signal.aborted) setUsers([]); |
| 65 | - } | 65 | + }) |
| 66 | + .finally(() => setLoading(false)); | ||
| 67 | + }, 250); | ||
| 68 | + onCleanup(() => window.clearTimeout(timer)); | ||
| 66 | }); | 69 | }); |
| 67 | 70 | ||
| 68 | createEffect(() => { | 71 | createEffect(() => { |