{"version":3,"file":"useAsyncThrottledCallback.cjs","names":["useAsyncThrottler"],"sources":["../../src/async-throttler/useAsyncThrottledCallback.ts"],"sourcesContent":["import { useCallback } from 'react'\nimport { useAsyncThrottler } from './useAsyncThrottler'\nimport type { ReactAsyncThrottlerOptions } from './useAsyncThrottler'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\n\n/**\n * A React hook that creates a throttled version of an async callback function.\n * This hook is a convenient wrapper around the `useAsyncThrottler` hook,\n * providing a stable, throttled async function reference for use in React components.\n *\n * The throttled async function will execute at most once within the specified wait time period,\n * regardless of how many times it is called. If called multiple times during the wait period,\n * only the first invocation will execute, and subsequent calls will be ignored until\n * the wait period has elapsed. The returned function always returns a promise\n * that resolves or rejects with the result of the original async function.\n *\n * This hook provides a simpler API compared to `useAsyncThrottler`, making it ideal for basic\n * async throttling needs. However, it does not expose the underlying AsyncThrottler instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution/error state\n * - Custom useCallback dependencies\n *\n * Consider using the `useAsyncThrottler` hook instead.\n *\n *\n * @example\n * ```tsx\n * // Throttle an async API call\n * const handleApiCall = useAsyncThrottledCallback(async (data) => {\n *   const result = await sendDataToServer(data);\n *   return result;\n * }, {\n *   wait: 200 // Execute at most once every 200ms\n * });\n *\n * // Use in an event handler\n * <button onClick={() => handleApiCall(formData)}>Send</button>\n * ```\n */\nexport function useAsyncThrottledCallback<TFn extends AnyAsyncFunction>(\n  fn: TFn,\n  options: ReactAsyncThrottlerOptions<TFn, {}>,\n): (...args: Parameters<TFn>) => Promise<ReturnType<TFn>> {\n  const asyncThrottledFn = useAsyncThrottler(fn, options).maybeExecute\n  return useCallback(\n    (...args) => asyncThrottledFn(...args) as Promise<ReturnType<TFn>>,\n    [asyncThrottledFn],\n  )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,0BACd,IACA,SACwD;CACxD,MAAM,mBAAmBA,4CAAkB,IAAI,QAAQ,CAAC;AACxD,gCACG,GAAG,SAAS,iBAAiB,GAAG,KAAK,EACtC,CAAC,iBAAiB,CACnB"}