@statesync/core / createThrottledHandler
Function: createThrottledHandler()
ts
function createThrottledHandler(onRefresh, options?): ThrottledHandler;Defined in: throttle.ts:142
Creates a ThrottledHandler that controls the rate of refresh calls.
The handler adapts its behavior based on the provided options:
- No options (or all zero): Passthrough —
trigger()callsonRefreshimmediately. debounceMsonly: Classic debounce — waits for a quiet period before callingonRefresh.throttleMsonly: Classic throttle with configurable leading/trailing edges.- Both
debounceMsandthrottleMs: Debounce is applied first, then the result is throttled.
Parameters
| Parameter | Type | Description |
|---|---|---|
onRefresh | () => void | The callback to invoke when a refresh should occur. This is the actual refresh function that fetches a new snapshot. |
options? | InvalidationThrottlingOptions | Optional throttling/debounce configuration. When omitted or empty, the handler acts as a passthrough. |
Returns
A ThrottledHandler that wraps onRefresh with the configured rate limiting.
Example
ts
import { createThrottledHandler } from '@statesync/core';
const handler = createThrottledHandler(
() => console.log('refresh!'),
{ debounceMs: 200, throttleMs: 1000 },
);
handler.trigger(); // May fire immediately or be delayed
handler.hasPending(); // true if a delayed refresh is scheduled
handler.dispose(); // Cleanup when done