{"version":3,"file":"useThrottledState.cjs","names":["useThrottler"],"sources":["../../src/throttler/useThrottledState.ts"],"sourcesContent":["import { useState } from 'react'\nimport { useThrottler } from './useThrottler'\nimport type { ReactThrottler, ReactThrottlerOptions } from './useThrottler'\nimport type { ThrottlerState } from '@tanstack/pacer/throttler'\n\n/**\n * A React hook that creates a throttled state value that updates at most once within a specified time window.\n * This hook combines React's useState with throttling functionality to provide controlled state updates.\n *\n * Throttling ensures state updates occur at a controlled rate regardless of how frequently the setter is called.\n * This is useful for rate-limiting expensive re-renders or operations that depend on rapidly changing state.\n *\n * The hook returns a tuple containing:\n * - The throttled state value\n * - A throttled setter function that respects the configured wait time\n * - The throttler instance for additional control\n *\n * For more direct control over throttling without state management,\n * consider using the lower-level useThrottler hook instead.\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger a re-render,\n * optimizing performance by preventing unnecessary re-renders when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary re-renders and gives you\n * full control over when your component updates. Only when you provide a selector will the\n * component re-render when the selected state values change.\n *\n * Available throttler state properties:\n * - `executionCount`: Number of function executions that have been completed\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last function execution in milliseconds\n * - `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [value, setValue, throttler] = useThrottledState(0, { wait: 1000 });\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [value, setValue, throttler] = useThrottledState(\n *   0,\n *   { wait: 1000 },\n *   (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Opt-in to re-render when throttling state changes (optimized for loading indicators)\n * const [value, setValue, throttler] = useThrottledState(\n *   0,\n *   { wait: 1000 },\n *   (state) => ({\n *     isPending: state.isPending,\n *     status: state.status\n *   })\n * );\n *\n * // Opt-in to re-render when timing information changes (optimized for timing displays)\n * const [value, setValue, throttler] = useThrottledState(\n *   0,\n *   { wait: 1000 },\n *   (state) => ({\n *     lastExecutionTime: state.lastExecutionTime,\n *     nextExecutionTime: state.nextExecutionTime\n *   })\n * );\n *\n * // With custom leading/trailing behavior\n * const [value, setValue] = useThrottledState(0, {\n *   wait: 1000,\n *   leading: true,   // Update immediately on first change\n *   trailing: false  // Skip trailing edge updates\n * });\n *\n * // Access throttler methods if needed\n * const handleReset = () => {\n *   setValue(0);\n *   throttler.cancel(); // Cancel any pending updates\n * };\n *\n * // Access the selected throttler state (will be empty object {} unless selector provided)\n * const { executionCount, isPending } = throttler.state;\n * ```\n */\n\nexport function useThrottledState<\n  TValue,\n  TSelected = ThrottlerState<React.Dispatch<React.SetStateAction<TValue>>>,\n>(\n  value: TValue,\n  options: ReactThrottlerOptions<\n    React.Dispatch<React.SetStateAction<TValue>>,\n    TSelected\n  >,\n  selector?: (\n    state: ThrottlerState<React.Dispatch<React.SetStateAction<TValue>>>,\n  ) => TSelected,\n): [\n  TValue,\n  React.Dispatch<React.SetStateAction<TValue>>,\n  ReactThrottler<React.Dispatch<React.SetStateAction<TValue>>, TSelected>,\n] {\n  const [throttledValue, setThrottledValue] = useState<TValue>(value)\n  const throttler = useThrottler(setThrottledValue, options, selector)\n  return [throttledValue, throttler.maybeExecute, throttler]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFA,SAAgB,kBAId,OACA,SAIA,UAOA;CACA,MAAM,CAAC,gBAAgB,yCAAsC,MAAM;CACnE,MAAM,YAAYA,kCAAa,mBAAmB,SAAS,SAAS;AACpE,QAAO;EAAC;EAAgB,UAAU;EAAc;EAAU"}