function injectThrottler<TFn, TSelected>(
fn,
options,
selector): AngularThrottler<TFn, TSelected>;
Defined in: throttler/injectThrottler.ts:85
An Angular function that creates and manages a Throttler instance.
This is a lower-level function that provides direct access to the Throttler's functionality. This allows you to integrate it with any state management solution you prefer.
This function provides throttling functionality to limit how often a function can be called, ensuring it executes at most once within a specified time window.
The throttler will execute the function immediately (if leading is enabled) and then prevent further executions until the wait period has elapsed.
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
ThrottlerOptions<TFn>
(state) => TSelected
AngularThrottler<TFn, TSelected>
// Default behavior - no reactive state subscriptions
const throttler = injectThrottler(
(scrollY: number) => updateScrollPosition(scrollY),
{ wait: 100 }
);
// Opt-in to track isPending changes (optimized for loading states)
const throttler = injectThrottler(
(scrollY: number) => updateScrollPosition(scrollY),
{ wait: 100 },
(state) => ({ isPending: state.isPending })
);
// In an event handler
window.addEventListener('scroll', () => {
throttler.maybeExecute(window.scrollY);
});
// Access the selected state (will be empty object {} unless selector provided)
const { isPending } = throttler.state();