<script lang="ts">
import { TanStackDevtools } from '@tanstack/svelte-devtools'
import { pacerDevtoolsPlugin } from '@tanstack/svelte-pacer-devtools'
import { createAsyncThrottler } from '@tanstack/svelte-pacer/async-throttler'
interface SearchResult {
id: number
title: string
}
const fakeApi = async (term: string): Promise<Array<SearchResult>> => {
await new Promise((resolve) => setTimeout(resolve, 500))
return [
{ id: 1, title: `${term} result ${Math.floor(Math.random() * 100)}` },
{ id: 2, title: `${term} result ${Math.floor(Math.random() * 100)}` },
{ id: 3, title: `${term} result ${Math.floor(Math.random() * 100)}` },
]
}
let searchTerm = $state('')
let results = $state<Array<SearchResult>>([])
let error = $state<Error | null>(null)
const handleSearch = async (term: string) => {
if (!term) {
results = []
return
}
const data = await fakeApi(term)
results = data
error = null
return data
}
const setSearchAsyncThrottler = createAsyncThrottler(handleSearch, () => ({
key: 'createAsyncThrottler',
wait: 1000,
onError: (cause) => {
console.error('Search failed:', cause)
error = cause as Error
results = []
},
}))
const handleSearchThrottled = setSearchAsyncThrottler.maybeExecute
async function onSearchChange(e: Event) {
const newTerm = (e.target as HTMLInputElement).value
searchTerm = newTerm
const result = await handleSearchThrottled(newTerm)
console.log('result', result)
}
</script>
<div>
<h1>TanStack Pacer createAsyncThrottler Example</h1>
<div>
<input
autofocus
type="search"
value={searchTerm}
oninput={onSearchChange}
placeholder="Type to search..."
style="width: 100%"
autocomplete="new-password"
/>
</div>
<div style="margin-top: 10px">
<button onclick={() => setSearchAsyncThrottler.flush()}>Flush</button>
</div>
{#if error}<div>
Error: {error.message}
</div>{/if}<setSearchAsyncThrottler.Subscribe
selector={(state) => ({
isExecuting: state.isExecuting,
isPending: state.isPending,
successCount: state.successCount,
})}
>{#snippet children({ isExecuting, isPending, successCount })}<div>
<p>API calls made: {successCount}</p>
{#if results.length > 0}<ul>
{#each results as item, index (index)}<li>{item.title}</li>{/each}
</ul>{/if}{#if isPending}<p>Pending...</p>{:else}{#if isExecuting}<p>
Executing...
</p>{:else}{/if}{/if}
</div>{/snippet}</setSearchAsyncThrottler.Subscribe
><setSearchAsyncThrottler.Subscribe selector={(state) => state}
>{#snippet children(state)}<pre style="margin-top: 20px">{JSON.stringify(
state,
null,
2,
)}</pre>{/snippet}</setSearchAsyncThrottler.Subscribe
>
</div>
{#if import.meta.env.DEV}<TanStackDevtools
plugins={[pacerDevtoolsPlugin()]}
/>{/if}