Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

README.md

14 lines880 Bytes

1# TanStack Lit Query Batching Example
2​
3Several components request characters at the same time, and an async batcher from TanStack Pacer combines those requests into one.
4​
5## How it works
6​
7- `src/services/characters/queries.ts` defines `charactersQueries.detail(id)`, a `queryOptions` factory keyed by `['characters', 'detail', id]` with a 1-minute `staleTime`.
8- `src/services/characters/api.ts` does not fetch in `getCharacter`. Instead, it adds the id to an `AsyncBatcher` from `@tanstack/lit-pacer` with `wait: 0`, which sends one request to the Rick and Morty API for all ids queued in the same tick and resolves each query's promise with its character.
9- `src/character-list.ts` calls `createQueriesController` with one `charactersQueries.detail(id)` per entered id, so each id still has its own cache entry and status.
10​
11To run this example:
12​
13- `npm install`
14- `npm run dev`