import { SolidPacerOptions } from "../types/index.js"; import { Accessor, JSX } from "solid-js"; import { Store } from "@tanstack/solid-store"; import { RateLimiter, RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limiter"; import { AnyFunction } from "@tanstack/pacer/types"; //#region src/rate-limiter/createRateLimiter.d.ts export interface SolidRateLimiterOptions extends RateLimiterOptions { /** * Optional callback invoked when the owning component unmounts. Receives the rate limiter instance. * When provided, replaces the default cleanup; use it to call reset(), add logging, etc. */ onUnmount?: (rateLimiter: SolidRateLimiter) => void; } export interface SolidRateLimiter extends Omit, 'store'> { options: RateLimiter['options'] & SolidRateLimiterOptions; setOptions: (options: Partial>) => void; /** * A Solid component that allows you to subscribe to the rate limiter state. * * This is useful for tracking specific parts of the rate limiter state * deep in your component tree without needing to pass a selector to the hook. * * @example * ({ rejectionCount: state.rejectionCount })}> * {(state) => ( *
Rejections: {state().rejectionCount}
* )} *
*/ Subscribe: (props: { selector: (state: RateLimiterState) => TSelected; children: ((state: Accessor) => JSX.Element) | JSX.Element; }) => JSX.Element; /** * Reactive state that will be updated when the rate limiter state changes * * Use this instead of `rateLimiter.store.state` */ readonly state: Accessor>; /** * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state. * The state on the store object is not reactive, as it has not been wrapped in a `useSelector` hook internally. * Although, you can make the state reactive by using the `useSelector` in your own usage. */ readonly store: Store>; } /** * A low-level Solid hook that creates a `RateLimiter` instance to enforce rate limits on function execution. * * This hook is designed to be flexible and state-management agnostic - it simply returns a rate limiter instance that * you can integrate with any state management solution (createSignal, etc). * * 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: * - 'fixed': A strict window that resets after the window period. All executions within the window count * towards the limit, and the window resets completely after the period. * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more * consistent rate of execution over time. * * For smoother execution patterns: * - Use throttling when you want consistent spacing between executions (e.g. UI updates) * - Use debouncing when you want to collapse rapid-fire events (e.g. search input) * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits) * * ## State Management and Selector * * The hook uses TanStack Store for reactive state management. You can subscribe to state changes * in two ways: * * **1. Using `rateLimiter.Subscribe` component (Recommended for component tree subscriptions)** * * Use the `Subscribe` component to subscribe to state changes deep in your component tree without * needing to pass a selector to the hook. This is ideal when you want to subscribe to state * in child components. * * **2. Using the `selector` parameter (For hook-level subscriptions)** * * The `selector` parameter allows you to specify which state changes will trigger reactive updates * at the hook level, 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 or using the `Subscribe` component. This prevents unnecessary * updates and gives you full control over when your component tracks state changes. * * Use `getRemainingInWindow()` and `getMsUntilNextWindow()` for current window capacity and timing. * * Available state properties: * - `executionTimes`: Timestamps of executions tracked for the current window * - `isExceeded`: Whether the rate limit has been reached * - `executionCount`: Number of function executions that have been completed * - `rejectionCount`: Number of function calls that were rejected due to rate limiting * * @example * ```tsx * // Default behavior - no reactive state subscriptions * const rateLimiter = createRateLimiter(apiCall, { * limit: 5, * window: 60000, * windowType: 'sliding', * }); * * // Subscribe to state changes deep in component tree using Subscribe component * ({ rejectionCount: state.rejectionCount })}> * {(state) => ( *
Rejections: {state().rejectionCount}
* )} *
* * // Opt-in to track execution count changes at hook level (optimized for tracking successful executions) * const rateLimiter = createRateLimiter( * apiCall, * { * limit: 5, * window: 60000, * windowType: 'sliding', * }, * (state) => ({ executionCount: state.executionCount }) * ); * * // Opt-in to track rejection count changes (optimized for tracking rate limit violations) * const rateLimiter = createRateLimiter( * apiCall, * { * limit: 5, * window: 60000, * windowType: 'sliding', * }, * (state) => ({ rejectionCount: state.rejectionCount }) * ); * * // Opt-in to track execution times changes (optimized for window calculations) * const rateLimiter = createRateLimiter( * apiCall, * { * limit: 5, * window: 60000, * windowType: 'sliding', * }, * (state) => ({ executionTimes: state.executionTimes }) * ); * * // Multiple state properties - track when any of these change * const rateLimiter = createRateLimiter( * 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(); * ``` */ export declare function createRateLimiter(fn: TFn, options: SolidPacerOptions>, selector?: (state: RateLimiterState) => TSelected): SolidRateLimiter; //#endregion