其他
需要在前端或 Node 中统一控制函数执行时机(防抖、节流…
适用于用 TanStack Pacer 对搜索输入、滚动事件、API 调用、文件上传、分析事件和网络请求做防抖、节流、限流、队列与批处理。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 40/100 ×0.15
适用场景
需要在前端或 Node 中统一控制函数执特定
解决思路
- 安装核心包:`npm install @tanstack/pacer`。
- 安装 React hooks:`npm install @tanstack/react-pacer`。
- 防抖使用 `Debouncer`:`import { Debouncer } from '@tanstack/pacer'`,以 `new Debouncer(fn, { wait, leading, trailing, maxWait, enabled, onExecute })` 创建实例。
- 操作防抖实例:用 `debouncer.maybeExecute(args)` 触发;用 `debouncer.cancel()` 取消;用 `debouncer.getExecutionCount()` 获取执行次数;用 `debouncer.setOptions({ wait: 500 })` 动态重配置。
- 防抖工厂函数:`import { debounce } from '@tanstack/pacer'`,用 `const debouncedSearch = debounce(fn, { wait: 300 })` 创建,调用 `debouncedSearch('term')`,用 `debouncedSearch.cancel()` 取消。
- React 防抖 hooks:用 `useDebouncer(fn, { wait: 300 })` 获取完整实例;用 `useDebouncedCallback(fn, { wait: 300 })` 获取简单防抖函数;用 `useDebouncedState(initialValue, { wait: 300 })` 管理防抖 state;用 `useDebouncedValue(reactiveValue, { wait: 300 })` 获取防抖后的响应式值。
- 节流使用 `Throttler`:`import { Throttler } from '@tanstack/pacer'`,以 `new Throttler(fn, { wait, leading, trailing, enabled, onExecute })` 创建实例;默认 `leading: true`、`trailing: true`。
- 操作节流实例:用 `throttler.maybeExecute(args)` 触发;用 `throttler.cancel()` 取消。
- React 节流 hooks:用 `useThrottler` 获取完整实例;用 `useThrottledCallback(handleScroll, { wait: 100 })` 创建节流函数;用 `useThrottledState({ x: 0, y: 0 }, { wait: 100 })` 管理节流 state;用 `useThrottledValue` 获取节流后的值。
- 限流使用 `RateLimiter`:`import { RateLimiter } from '@tanstack/pacer'`,以 `new RateLimiter(async fn, { limit, window, enabled, onExecute, onReject })` 创建实例。
- 操作限流实例:用 `limiter.maybeExecute('/api/data')` 触发,超出限制会被拒绝;用 `limiter.getExecutionCount()` 获取执行次数;用 `limiter.getRejectionCount()` 获取拒绝次数。
- React 限流 hooks:用 `useRateLimiter` 获取完整实例;用 `useRateLimitedCallback(apiCall, { limit: 5, window: 1000 })` 创建限流函数;用 `useRateLimitedState` 管理限流 state;用 `useRateLimitedValue` 获取限流后的值。
- 排队使用 `Queue`:`import { Queue } from '@tanstack/pacer'`,以 `new Queue({ concurrency: 1, started: true })` 创建实例。
- 操作队列:用 `queue.add(() => uploadFile(file1))` 添加任务;用 `queue.start()` 开始;用 `queue.pause()` 暂停;用 `queue.clear()` 清空;用 `queue.getSize()` 获取待处理数量;用 `queue.getPending()` 获取当前执行数量。
- 批处理使用 `Batcher`:`import { Batcher } from '@tanstack/pacer'`,以 `new Batcher(fn, { maxSize: 50, wait: 1000 })` 创建实例。
- 操作批处理器:用 `batcher.add(logEntry)` 累积;用 `batcher.flush()` 手动刷新;用 `batcher.getSize()` 获取当前批次大小;用 `batcher.clear()` 丢弃批次。
- 异步变体类:`import { AsyncDebouncer, AsyncThrottler } from '@tanstack/pacer'`,用 `new AsyncDebouncer(async (query) => { ... }, { wait: 300 })` 处理异步函数。
- 异步工厂函数:`import { asyncDebounce, asyncThrottle } from '@tanstack/pacer'`,按需用 `asyncDebounce`、`asyncThrottle` 包装异步函数。
- React 异步 hooks:使用 `useAsyncDebouncer`、`useAsyncThrottler`。
- 按场景选择工具:搜索输入用 `Debouncer`;滚动事件用 `Throttler`;API 保护用 `RateLimiter`;文件上传用 `Queue`;分析事件用 `Batcher`;网络请求用 `AsyncDebouncer`。
- 处理 leading/trailing:`leading: true` 立即执行并在 wait 结束前抑制,适合按钮点击;`trailing: true` 在活动停止后执行,适合搜索输入;两者都设,立即执行且最终 wait 后执行,适合滚动节流。
- 防抖时使用 `maxWait` 保证连续活动期间一定执行。
- 网络请求使用 async 变体,以处理 abort/cancellation。
- React hooks 自动处理清理,无需手动 teardown。
- 使用 `setOptions` 做动态重配置,例如为高级用户减少 wait。
- 组合工具处理复杂场景,例如 rate-limited queue。
- 在 `RateLimiter` 上使用 `onReject` 告知用户被限流。
适用边界 · 注意事项
- 需要固定频率周期性执行时,不要用 debounce,应使用 throttle。
- 连续事件持续很久且必须中途执行时,不要只用无 maxWait 的 debounce。
- 不要在 React 每次 render 中新建实例;非 React 环境不要忘记调用 cancel() 清理。