import Alpine from 'alpinejs'
import { createPacerScope } from '@tanstack/alpine-pacer'
import type {
AlpineAsyncRateLimiter,
AsyncRateLimiterState,
} from '@tanstack/alpine-pacer'
import { TanStackDevtoolsCore } from '@tanstack/devtools'
import { pacerDevtoolsPlugin } from '@tanstack/pacer-devtools'
interface SearchResult {
id: number
title: string
}
Alpine.data('demo', () => {
const scope = createPacerScope()
return {
async fakeApi(term: string): Promise<Array<SearchResult>> {
await new Promise((resolve) => setTimeout(resolve, 300))
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)}` },
]
},
windowType: 'fixed' as 'fixed' | 'sliding',
searchTerm: '',
results: [] as Array<SearchResult>,
error: null as Error | null,
async handleSearch(term: string) {
if (!term) {
this.results = []
return
}
const data = await this.fakeApi(term)
this.results = data
this.error = null
},
async onSearchChange(e: Event) {
const newTerm = (e.target as HTMLInputElement).value
this.searchTerm = newTerm
await this.setSearchAsyncRateLimiter!.maybeExecute(newTerm)
},
setSearchAsyncRateLimiter: null as AlpineAsyncRateLimiter<
(term: string) => Promise<void>,
AsyncRateLimiterState<(term: string) => Promise<void>>
> | null,
init() {
this.setSearchAsyncRateLimiter = scope.createAsyncRateLimiter(
this.handleSearch.bind(this),
() => ({
key: 'createAsyncRateLimiter',
windowType: this.windowType,
limit: 3,
window: 3000,
onReject: (_args, rateLimiter) => {
console.log(
`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`,
)
},
onError: (cause) => {
console.error('Search failed:', cause)
this.error = cause as Error
this.results = []
},
}),
(state) => state,
)
},
destroy() {
;(() => {
console.log('unmount')
this.setSearchAsyncRateLimiter!.reset()
})()
scope.destroy()
},
}
})
Alpine.data('lifecycle', () => ({
mounted: true,
toggleMounted(event: KeyboardEvent) {
if (event.key === 'Enter') this.mounted = !this.mounted
},
}))
Alpine.data('devtools', () => {
let host: TanStackDevtoolsCore | undefined
let target: HTMLDivElement | undefined
return {
init() {
if (!import.meta.env.DEV) return
target = document.createElement('div')
document.body.append(target)
host = new TanStackDevtoolsCore({ plugins: [pacerDevtoolsPlugin()] })
host.mount(target)
},
destroy() {
host?.unmount()
target?.remove()
},
}
})
Alpine.start()