function createRateLimitedValue<TValue, TSelected>(
value,
initialOptions,
selector?): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>]
function createRateLimitedValue<TValue, TSelected>(
value,
initialOptions,
selector?): [Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>]
Defined in: rate-limiter/createRateLimitedValue.ts:83
A high-level Solid hook that creates a rate-limited version of a value that updates at most a certain number of times within a time window. This hook uses Solid's createSignal internally to manage the rate-limited state.
Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out or intelligently collapse updates. This can lead to bursts of rapid updates followed by periods of no updates.
The rate limiter supports two types of windows:
For smoother update patterns, consider:
Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.
The hook returns a tuple containing:
For more direct control over rate limiting behavior without Solid state management, consider using the lower-level createRateLimiter hook instead.
The hook uses TanStack Store for reactive state management via the underlying rate limiter instance. The selector parameter allows you to specify which rate limiter state changes will trigger reactive updates, optimizing performance by preventing unnecessary subscriptions 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 reactive updates and gives you full control over when your component subscribes to state changes. Only when you provide a selector will the reactive system track the selected state values.
Available rate limiter state properties:
• TValue
• TSelected = {}
Accessor<TValue>
RateLimiterOptions<Setter<TValue>>
(state) => TSelected
[Accessor<TValue>, SolidRateLimiter<Setter<TValue>, TSelected>]
// Default behavior - no reactive state subscriptions
const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
limit: 5,
window: 60000,
windowType: 'sliding'
});
// Opt-in to reactive updates when limit state changes (optimized for UI feedback)
const [rateLimitedValue, rateLimiter] = createRateLimitedValue(
rawValue,
{ limit: 5, window: 60000 },
(state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })
);
// Use the rate-limited value
console.log(rateLimitedValue()); // Access the current rate-limited value
// Access rate limiter state via signals
console.log('Is at limit:', rateLimiter.state().isAtLimit);
// Control the rate limiter
rateLimiter.reset(); // Reset the rate limit window
// Default behavior - no reactive state subscriptions
const [rateLimitedValue, rateLimiter] = createRateLimitedValue(rawValue, {
limit: 5,
window: 60000,
windowType: 'sliding'
});
// Opt-in to reactive updates when limit state changes (optimized for UI feedback)
const [rateLimitedValue, rateLimiter] = createRateLimitedValue(
rawValue,
{ limit: 5, window: 60000 },
(state) => ({ isAtLimit: state.isAtLimit, remainingInWindow: state.remainingInWindow })
);
// Use the rate-limited value
console.log(rateLimitedValue()); // Access the current rate-limited value
// Access rate limiter state via signals
console.log('Is at limit:', rateLimiter.state().isAtLimit);
// Control the rate limiter
rateLimiter.reset(); // Reset the rate limit window
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.