function useRateLimiter<TFn, TSelected>(
fn,
options,
selector): ReactRateLimiter<TFn, TSelected>
function useRateLimiter<TFn, TSelected>(
fn,
options,
selector): ReactRateLimiter<TFn, TSelected>
定义于:react-pacer/src/rate-limiter/useRateLimiter.ts:141
一个低级别的 React Hook,用于创建一个 RateLimiter 实例,以强制执行函数执行的速率限制。
此 Hook 的设计旨在灵活且独立于状态管理 - 它仅仅返回一个速率限制器实例,您可以将其集成到任何状态管理解决方案中(useState、Redux、Zustand、Jotai 等)。
速率限制是一种简单的“硬限制”方法,它允许在时间窗口内达到最大计数之前执行,然后阻止所有后续调用,直到窗口重置。与节流或防抖不同,它不尝试智能地间隔或合并执行。
速率限制器支持两种类型的窗口
为了更平滑的执行模式
该 Hook 使用 TanStack Store 进行响应式状态管理。`selector` 参数允许您指定哪些状态更改将触发重新渲染,通过防止不必要的重新渲染来优化性能。只有当您提供 `selector` 时,组件才会在选定的状态值发生变化时重新渲染。
默认情况下,将不会有响应式状态订阅,您必须通过提供 `selector` 函数来选择加入状态跟踪。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供 `selector` 时,组件才会在选定的状态值发生变化时重新渲染。
可用的状态属性
该 Hook 返回一个包含以下内容的对象的结构:
• TFn extends AnyFunction
• TSelected = {}
TFn
RateLimiterOptions<TFn>
(state) => TSelected
ReactRateLimiter<TFn, TSelected>
// Default behavior - no reactive state subscriptions
const rateLimiter = useRateLimiter(apiCall, {
limit: 5,
window: 60000,
windowType: 'sliding',
});
// Opt-in to re-render when execution count changes (optimized for tracking successful executions)
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ executionCount: state.executionCount })
);
// Opt-in to re-render when rejection count changes (optimized for tracking rate limit violations)
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ rejectionCount: state.rejectionCount })
);
// Opt-in to re-render when execution times change (optimized for window calculations)
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ executionTimes: state.executionTimes })
);
// Multiple state properties - re-render when any of these change
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({
executionCount: state.executionCount,
rejectionCount: state.rejectionCount
})
);
// Monitor rate limit status
const handleClick = () => {
const remaining = rateLimiter.getRemainingInWindow();
if (remaining > 0) {
rateLimiter.maybeExecute(data);
} else {
showRateLimitWarning();
}
};
// Access the selected state (will be empty object {} unless selector provided)
const { executionCount, rejectionCount } = rateLimiter.state;
// Default behavior - no reactive state subscriptions
const rateLimiter = useRateLimiter(apiCall, {
limit: 5,
window: 60000,
windowType: 'sliding',
});
// Opt-in to re-render when execution count changes (optimized for tracking successful executions)
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ executionCount: state.executionCount })
);
// Opt-in to re-render when rejection count changes (optimized for tracking rate limit violations)
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ rejectionCount: state.rejectionCount })
);
// Opt-in to re-render when execution times change (optimized for window calculations)
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ executionTimes: state.executionTimes })
);
// Multiple state properties - re-render when any of these change
const rateLimiter = useRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({
executionCount: state.executionCount,
rejectionCount: state.rejectionCount
})
);
// Monitor rate limit status
const handleClick = () => {
const remaining = rateLimiter.getRemainingInWindow();
if (remaining > 0) {
rateLimiter.maybeExecute(data);
} else {
showRateLimitWarning();
}
};
// Access the selected state (will be empty object {} unless selector provided)
const { executionCount, rejectionCount } = rateLimiter.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。