function injectRateLimiter<TFn, TSelected>(
fn,
options,
selector): AngularRateLimiter<TFn, TSelected>;
Defined in: rate-limiter/injectRateLimiter.ts:99
An Angular function that creates and manages a RateLimiter instance.
This is a lower-level function that provides direct access to the RateLimiter's functionality. This allows you to integrate it with any state management solution you prefer.
Rate limiting is a simple "hard limit" approach that allows executions until a maximum count is reached within a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing, it does not attempt to space out or collapse executions intelligently.
The rate limiter supports two types of windows:
For smoother execution patterns:
The function uses TanStack Store for state management and wraps it with Angular signals. The selector parameter allows you to specify which state changes will trigger signal updates, optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
By default, there will be no reactive state subscriptions and you must opt-in to state tracking by providing a selector function. This prevents unnecessary updates and gives you full control over when your component tracks state changes.
Available state properties:
TFn extends AnyFunction
TSelected = { }
TFn
RateLimiterOptions<TFn>
(state) => TSelected
AngularRateLimiter<TFn, TSelected>
// Default behavior - no reactive state subscriptions
const rateLimiter = injectRateLimiter(apiCall, {
limit: 5,
window: 60000,
windowType: 'sliding',
});
// Opt-in to track execution count changes
const rateLimiter = injectRateLimiter(
apiCall,
{
limit: 5,
window: 60000,
windowType: 'sliding',
},
(state) => ({ executionCount: state.executionCount })
);
// 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();