Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference

createRateLimitedSignal

Function: createRateLimitedSignal()

ts
function createRateLimitedSignal<TValue>(value, initialOptions): [Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>]
function createRateLimitedSignal<TValue>(value, initialOptions): [Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>]

Defined in: rate-limiter/createRateLimitedSignal.ts:57

A Solid hook that creates a rate-limited state value that enforces a hard limit on state updates within a time window. This hook combines Solid's createSignal with rate limiting functionality to provide controlled state updates.

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.

For smoother update patterns, consider:

  • createThrottledSignal: When you want consistent spacing between updates (e.g. UI changes)
  • createDebouncedSignal: When you want to collapse rapid updates into a single update (e.g. search input)

Rate limiting should primarily be used when you need to enforce strict limits, like API rate limits.

The hook returns a tuple containing:

  • The rate-limited state value accessor
  • A rate-limited setter function that respects the configured limits
  • The rateLimiter instance for additional control

For more direct control over rate limiting without state management, consider using the lower-level createRateLimiter hook instead.

Type Parameters

TValue

Parameters

value

TValue

initialOptions

RateLimiterOptions<Setter<TValue>>

Returns

[Accessor<TValue>, Setter<TValue>, SolidRateLimiter<Setter<TValue>>]

Example

tsx
// Basic rate limiting - update state at most 5 times per minute
const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
  limit: 5,
  window: 60000
});

// With rejection callback
const [value, setValue] = createRateLimitedSignal(0, {
  limit: 3,
  window: 5000,
  onReject: (rateLimiter) => {
    alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
  }
});

// Access rateLimiter state via signals
const handleSubmit = () => {
  const remaining = rateLimiter.remainingInWindow();
  if (remaining > 0) {
    setValue(newValue);
  } else {
    showRateLimitWarning();
  }
};
// Basic rate limiting - update state at most 5 times per minute
const [value, setValue, rateLimiter] = createRateLimitedSignal(0, {
  limit: 5,
  window: 60000
});

// With rejection callback
const [value, setValue] = createRateLimitedSignal(0, {
  limit: 3,
  window: 5000,
  onReject: (rateLimiter) => {
    alert(`Rate limit reached. Try again in ${rateLimiter.getMsUntilNextWindow()}ms`);
  }
});

// Access rateLimiter state via signals
const handleSubmit = () => {
  const remaining = rateLimiter.remainingInWindow();
  if (remaining > 0) {
    setValue(newValue);
  } else {
    showRateLimitWarning();
  }
};
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.