diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..7042b45 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-23 - Domain Search Race Condition +**Learning:** Asynchronous typeahead searches in SvelteKit must implement a request ID mechanism to discard stale responses. `on:keyup` triggers excessive events and misses some inputs; `on:input` is preferred for correctness and efficiency. +**Action:** Always wrap async search handlers with a request ID check and use `on:input` for text fields. diff --git a/src/routes/DomainSearch.svelte b/src/routes/DomainSearch.svelte index 2cde1b3..17a6859 100644 --- a/src/routes/DomainSearch.svelte +++ b/src/routes/DomainSearch.svelte @@ -16,6 +16,7 @@ let nameSearched: string = ''; let isLoading: boolean = false; let debounceTimer: NodeJS.Timeout; + let lastRequestId = 0; $: errors = invalid ? validator.getErrors() : []; $: invalid = domainName !== '' && !validator.validate(domainName, { raiseError: false }); @@ -36,12 +37,17 @@ return goto(url); } + // Race condition fix: track the request ID to ensure we only process the latest response + const requestId = ++lastRequestId; nameSearched = domainName.toLocaleLowerCase(); isLoading = true; - domain = await $metaNamesSdk.domainRepository.find(domainName); + const result = await $metaNamesSdk.domainRepository.find(domainName); - isLoading = false; + if (requestId === lastRequestId) { + domain = result; + isLoading = false; + } } async function submit() { @@ -51,11 +57,12 @@