重要前提
安装AI Skills的关键前提是:必须科学上网,且开启TUN模式,这一点至关重要,直接决定安装能否顺利完成,在此郑重提醒三遍:科学上网,科学上网,科学上网。查看完整安装教程 →
tanstack-pacer by tanstack-skills/tanstack-skills
npx skills add https://github.com/tanstack-skills/tanstack-skills --skill tanstack-pacerTanStack Pacer 提供了一套统一的、类型安全的工具包,用于控制函数执行的时机。它提供了基于类的 API、工厂函数和 React 钩子,用于实现防抖、节流、速率限制、队列和批处理。
核心包: @tanstack/pacer React 包: @tanstack/react-pacer 状态: Beta
npm install @tanstack/pacer
npm install @tanstack/react-pacer # React hooks
延迟执行,直到一段无活动期之后。
import { Debouncer } from '@tanstack/pacer'
const debouncer = new Debouncer(
(query: string) => fetchSearchResults(query),
{
wait: 300, // 执行前无活动的毫秒数
leading: false, // 在前沿执行(默认:false)
trailing: true, // 在后沿执行(默认:true)
maxWait: 1000, // 连续调用 1 秒后强制执行
enabled: true,
onExecute: (result) => console.log(result),
}
)
debouncer.maybeExecute('search term')
debouncer.cancel()
debouncer.getExecutionCount()
debouncer.setOptions({ wait: 500 }) // 动态重新配置
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
import { debounce } from '@tanstack/pacer'
const debouncedSearch = debounce(
(query: string) => fetchResults(query),
{ wait: 300 }
)
debouncedSearch('term')
debouncedSearch.cancel()
import {
useDebouncer,
useDebouncedCallback,
useDebouncedState,
useDebouncedValue,
} from '@tanstack/react-pacer'
// 完整的防抖器实例
const debouncer = useDebouncer(fn, { wait: 300 })
// 简单的防抖函数
const debouncedFn = useDebouncedCallback(fn, { wait: 300 })
// 防抖状态管理
const [debouncedValue, setValue] = useDebouncedState(initialValue, { wait: 300 })
// 防抖的响应式值
const debouncedValue = useDebouncedValue(reactiveValue, { wait: 300 })
将执行限制在每个时间间隔内最多一次。
import { Throttler } from '@tanstack/pacer'
const throttler = new Throttler(
(position: { x: number; y: number }) => updatePosition(position),
{
wait: 100, // 两次执行之间的最小间隔
leading: true, // 首次调用时立即执行(默认:true)
trailing: true, // 在间隔后使用最后参数执行(默认:true)
enabled: true,
onExecute: (result) => console.log(result),
}
)
throttler.maybeExecute({ x: 100, y: 200 })
throttler.cancel()
import {
useThrottler,
useThrottledCallback,
useThrottledState,
useThrottledValue,
} from '@tanstack/react-pacer'
const throttledFn = useThrottledCallback(handleScroll, { wait: 100 })
const [throttledPos, setPos] = useThrottledState({ x: 0, y: 0 }, { wait: 100 })
控制在一个时间窗口内的最大执行次数。
import { RateLimiter } from '@tanstack/pacer'
const limiter = new RateLimiter(
async (endpoint: string) => fetch(endpoint).then(r => r.json()),
{
limit: 10, // 每个窗口内的最大执行次数
window: 60000, // 时间窗口(毫秒)(60秒)
enabled: true,
onExecute: (result) => console.log(result),
onReject: (...args) => console.warn('Rate limited:', args),
}
)
limiter.maybeExecute('/api/data') // 如果超出限制则被拒绝
limiter.getExecutionCount()
limiter.getRejectionCount()
import {
useRateLimiter,
useRateLimitedCallback,
useRateLimitedState,
useRateLimitedValue,
} from '@tanstack/react-pacer'
const rateLimitedFn = useRateLimitedCallback(apiCall, { limit: 5, window: 1000 })
具有可配置并发性的顺序执行。
import { Queue } from '@tanstack/pacer'
const queue = new Queue({
concurrency: 1, // 最大并发任务数
started: true, // 立即开始处理
})
queue.add(() => uploadFile(file1))
queue.add(() => uploadFile(file2))
queue.start()
queue.pause()
queue.clear()
queue.getSize() // 待处理计数
queue.getPending() // 当前正在执行计数
将调用分组以进行组合处理。
import { Batcher } from '@tanstack/pacer'
const batcher = new Batcher(
(items: LogEntry[]) => sendBatchToServer(items),
{
maxSize: 50, // 达到 50 个项目时自动刷新
wait: 1000, // 1 秒后自动刷新
}
)
batcher.add(logEntry) // 累积
batcher.flush() // 手动刷新
batcher.getSize() // 当前批次大小
batcher.clear() // 丢弃批次
import { AsyncDebouncer, asyncDebounce, AsyncThrottler, asyncThrottle } from '@tanstack/pacer'
const asyncDebouncer = new AsyncDebouncer(
async (query: string) => {
const response = await fetch(`/api/search?q=${query}`)
return response.json()
},
{ wait: 300 }
)
// React 异步钩子
import { useAsyncDebouncer, useAsyncThrottler } from '@tanstack/react-pacer'
| 场景 | 工具 | 原因 |
|---|---|---|
| 搜索输入 | Debouncer | 等待用户停止输入 |
| 滚动事件 | Throttler | 活动期间的定期更新 |
| API 保护 | RateLimiter | 对调用频率的硬性限制 |
| 文件上传 | Queue | 顺序处理 |
| 分析事件 | Batcher | 分组以提高效率 |
| 网络请求 | AsyncDebouncer | 处理中止/重试 |
leading: true): 立即执行,在等待期结束前抑制后续执行。适用于按钮点击。trailing: true): 在活动停止后执行。适用于搜索输入。maxWait 以保证在连续活动期间执行setOptions 进行动态重新配置(例如,为高级用户减少等待时间)onReject 以在用户被限制时通知他们maxWaitcancel())每周安装量
61
代码仓库
GitHub 星标数
5
首次出现
2026年2月21日
安全审计
安装于
codex57
opencode56
gemini-cli55
cursor55
amp54
kimi-cli54
TanStack Pacer provides a unified, type-safe toolkit for controlling function execution timing. It offers class-based APIs, factory functions, and React hooks for debouncing, throttling, rate limiting, queuing, and batching.
Core: @tanstack/pacer React: @tanstack/react-pacer Status: Beta
npm install @tanstack/pacer
npm install @tanstack/react-pacer # React hooks
Delays execution until after a period of inactivity.
import { Debouncer } from '@tanstack/pacer'
const debouncer = new Debouncer(
(query: string) => fetchSearchResults(query),
{
wait: 300, // ms of inactivity before execution
leading: false, // Execute on leading edge (default: false)
trailing: true, // Execute on trailing edge (default: true)
maxWait: 1000, // Force execution after 1s of continuous calls
enabled: true,
onExecute: (result) => console.log(result),
}
)
debouncer.maybeExecute('search term')
debouncer.cancel()
debouncer.getExecutionCount()
debouncer.setOptions({ wait: 500 }) // Dynamic reconfiguration
import { debounce } from '@tanstack/pacer'
const debouncedSearch = debounce(
(query: string) => fetchResults(query),
{ wait: 300 }
)
debouncedSearch('term')
debouncedSearch.cancel()
import {
useDebouncer,
useDebouncedCallback,
useDebouncedState,
useDebouncedValue,
} from '@tanstack/react-pacer'
// Full debouncer instance
const debouncer = useDebouncer(fn, { wait: 300 })
// Simple debounced function
const debouncedFn = useDebouncedCallback(fn, { wait: 300 })
// Debounced state management
const [debouncedValue, setValue] = useDebouncedState(initialValue, { wait: 300 })
// Debounced reactive value
const debouncedValue = useDebouncedValue(reactiveValue, { wait: 300 })
Limits execution to at most once per interval.
import { Throttler } from '@tanstack/pacer'
const throttler = new Throttler(
(position: { x: number; y: number }) => updatePosition(position),
{
wait: 100, // Minimum interval between executions
leading: true, // Execute immediately on first call (default: true)
trailing: true, // Execute after interval with last args (default: true)
enabled: true,
onExecute: (result) => console.log(result),
}
)
throttler.maybeExecute({ x: 100, y: 200 })
throttler.cancel()
import {
useThrottler,
useThrottledCallback,
useThrottledState,
useThrottledValue,
} from '@tanstack/react-pacer'
const throttledFn = useThrottledCallback(handleScroll, { wait: 100 })
const [throttledPos, setPos] = useThrottledState({ x: 0, y: 0 }, { wait: 100 })
Controls execution with a maximum count within a time window.
import { RateLimiter } from '@tanstack/pacer'
const limiter = new RateLimiter(
async (endpoint: string) => fetch(endpoint).then(r => r.json()),
{
limit: 10, // Max executions per window
window: 60000, // Time window in ms (60s)
enabled: true,
onExecute: (result) => console.log(result),
onReject: (...args) => console.warn('Rate limited:', args),
}
)
limiter.maybeExecute('/api/data') // Rejected if limit exceeded
limiter.getExecutionCount()
limiter.getRejectionCount()
import {
useRateLimiter,
useRateLimitedCallback,
useRateLimitedState,
useRateLimitedValue,
} from '@tanstack/react-pacer'
const rateLimitedFn = useRateLimitedCallback(apiCall, { limit: 5, window: 1000 })
Sequential execution with configurable concurrency.
import { Queue } from '@tanstack/pacer'
const queue = new Queue({
concurrency: 1, // Max concurrent tasks
started: true, // Start processing immediately
})
queue.add(() => uploadFile(file1))
queue.add(() => uploadFile(file2))
queue.start()
queue.pause()
queue.clear()
queue.getSize() // Pending count
queue.getPending() // Currently executing count
Groups calls for combined processing.
import { Batcher } from '@tanstack/pacer'
const batcher = new Batcher(
(items: LogEntry[]) => sendBatchToServer(items),
{
maxSize: 50, // Auto-flush at 50 items
wait: 1000, // Auto-flush after 1s
}
)
batcher.add(logEntry) // Accumulates
batcher.flush() // Manual flush
batcher.getSize() // Current batch size
batcher.clear() // Discard batch
import { AsyncDebouncer, asyncDebounce, AsyncThrottler, asyncThrottle } from '@tanstack/pacer'
const asyncDebouncer = new AsyncDebouncer(
async (query: string) => {
const response = await fetch(`/api/search?q=${query}`)
return response.json()
},
{ wait: 300 }
)
// React async hooks
import { useAsyncDebouncer, useAsyncThrottler } from '@tanstack/react-pacer'
| Scenario | Utility | Why |
|---|---|---|
| Search input | Debouncer | Wait for user to stop typing |
| Scroll events | Throttler | Periodic updates during activity |
| API protection | RateLimiter | Hard limit on call frequency |
| File uploads | Queue | Sequential processing |
| Analytics events | Batcher | Group for efficiency |
| Network requests | AsyncDebouncer | Handle abort/retry |
leading: true): Execute immediately, suppress until wait expires. Good for button clicks.trailing: true): Execute after activity stops. Good for search inputs.maxWait with debouncing to guarantee execution during continuous activitysetOptions for dynamic reconfiguration (e.g., reducing wait for power users)onReject on RateLimiter to inform users when they're rate limitedmaxWait with debounce for long-running continuous eventscancel())Weekly Installs
61
Repository
GitHub Stars
5
First Seen
Feb 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex57
opencode56
gemini-cli55
cursor55
amp54
kimi-cli54
UI组件模式实战指南:构建可复用React组件库与设计系统
10,700 周安装