{"version":3,"file":"useAsyncRateLimitedCallback.cjs","names":["useAsyncRateLimiter"],"sources":["../../src/async-rate-limiter/useAsyncRateLimitedCallback.ts"],"sourcesContent":["import { useCallback } from 'react'\nimport { useAsyncRateLimiter } from './useAsyncRateLimiter'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type { ReactAsyncRateLimiterOptions } from './useAsyncRateLimiter'\n\n/**\n * A React hook that creates a rate-limited version of an async callback function.\n * This hook is a convenient wrapper around the `useAsyncRateLimiter` hook,\n * providing a stable, async rate-limited function reference for use in React components.\n *\n * Async rate limiting is a \"hard limit\" approach for async functions: it allows all calls\n * until the limit is reached, then blocks (rejects) subsequent calls until the window resets.\n * Unlike throttling or debouncing, it does not attempt to space out or collapse calls.\n * This can lead to bursts of rapid executions followed by periods where all calls are blocked.\n *\n * The async rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n *   towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n *   consistent rate of execution over time.\n *\n * For smoother execution patterns, consider:\n * - useAsyncThrottledCallback: When you want consistent spacing between executions (e.g. UI updates)\n * - useAsyncDebouncedCallback: When you want to collapse rapid calls into a single execution (e.g. search input)\n *\n * Async rate limiting should primarily be used when you need to enforce strict limits\n * on async operations, like API rate limits or other scenarios requiring hard caps\n * on execution frequency.\n *\n * This hook provides a simpler API compared to `useAsyncRateLimiter`, making it ideal for basic\n * async rate limiting needs. However, it does not expose the underlying AsyncRateLimiter instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution counts\n * - Custom useCallback dependencies\n *\n * Consider using the `useAsyncRateLimiter` hook instead.\n *\n *\n * @example\n * ```tsx\n * // Rate limit async API calls to maximum 5 calls per minute with a sliding window\n * const makeApiCall = useAsyncRateLimitedCallback(\n *   async (data: ApiData) => {\n *     return fetch('/api/endpoint', { method: 'POST', body: JSON.stringify(data) });\n *   },\n *   {\n *     limit: 5,\n *     window: 60000, // 1 minute\n *     windowType: 'sliding',\n *     onReject: () => {\n *       console.warn('API rate limit reached. Please wait before trying again.');\n *     }\n *   }\n * );\n * ```\n */\nexport function useAsyncRateLimitedCallback<TFn extends AnyAsyncFunction>(\n  fn: TFn,\n  options: ReactAsyncRateLimiterOptions<TFn, {}>,\n): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {\n  const asyncRateLimitedFn = useAsyncRateLimiter(fn, options).maybeExecute\n  return useCallback(\n    (...args) => asyncRateLimitedFn(...args) as Promise<ReturnType<TFn>>,\n    [asyncRateLimitedFn],\n  )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0DA,SAAgB,4BACd,IACA,SACwD;CACxD,MAAM,qBAAqBA,gDAAoB,IAAI,QAAQ,CAAC;AAC5D,gCACG,GAAG,SAAS,mBAAmB,GAAG,KAAK,EACxC,CAAC,mBAAmB,CACrB"}