Skip to content
Draft
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
51 changes: 50 additions & 1 deletion src/routes/DomainSearch.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
<script context="module" lang="ts">
import type { Domain as DomainModel } from '@metanames/sdk';

const MAX_CACHE_SIZE = 100;
const CACHE_TTL = 5 * 60 * 1000; // 5 minutes

interface CacheEntry {
result: DomainModel | null;
timestamp: number;
}

const searchCache = new Map<string, CacheEntry>();

export function getCachedResult(domainName: string): CacheEntry | undefined {
const entry = searchCache.get(domainName);
if (entry) {
if (Date.now() - entry.timestamp > CACHE_TTL) {
searchCache.delete(domainName);
return undefined;
}
return entry;
}
return undefined;
}

export function setCachedResult(domainName: string, result: DomainModel | null) {
if (searchCache.size >= MAX_CACHE_SIZE) {
const firstKey = searchCache.keys().next().value;
if (firstKey) {
searchCache.delete(firstKey);
}
}
searchCache.set(domainName, { result, timestamp: Date.now() });
}
</script>

<script lang="ts">
import { onDestroy } from 'svelte';
import Card, { Content as CardContent } from '@smui/card';
import CircularProgress from '@smui/circular-progress';
import Textfield from '@smui/textfield';
import HelperText from '@smui/textfield/helper-text';
import type { Domain as DomainModel } from '@metanames/sdk';
import IconButton from '@smui/icon-button';
import { metaNamesSdk } from '$lib/stores/sdk';
import { goto } from '$app/navigation';
Expand All @@ -30,6 +66,10 @@

$: debounce(domainName);

onDestroy(() => {
clearTimeout(debounceTimer);
});

async function search(submit = false) {
if (invalid) return;

Expand All @@ -42,13 +82,22 @@

const currentRequestId = ++requestId;
nameSearched = domainName.toLocaleLowerCase();

const cachedResult = getCachedResult(nameSearched);
if (cachedResult) {
domain = cachedResult.result;
isLoading = false;
return;
}

isLoading = true;

const result = await $metaNamesSdk.domainRepository.find(domainName);

if (currentRequestId === requestId) {
domain = result;
isLoading = false;
setCachedResult(nameSearched, result);
}
}

Expand Down