Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 10 additions & 3 deletions src/routes/DomainSearch.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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() {
Expand All @@ -51,11 +57,12 @@

<div class="search-container">
<form on:submit|preventDefault={submit}>
<!-- Use on:input instead of on:keyup to handle all input types (paste, drag-drop) efficiently -->
<Textfield
class="domain-input"
variant="outlined"
bind:value={domainName}
on:keyup={() => debounce()}
on:input={() => debounce()}
bind:invalid
label="Domain name"
withTrailingIcon
Expand Down