{"version":3,"file":"useThrottledValue.cjs","names":["useThrottledState"],"sources":["../../src/throttler/useThrottledValue.ts"],"sourcesContent":["import { useEffect } from 'react'\nimport { useThrottledState } from './useThrottledState'\nimport type { ReactThrottler, ReactThrottlerOptions } from './useThrottler'\nimport type { ThrottlerState } from '@tanstack/pacer/throttler'\n\n/**\n * A high-level React hook that creates a throttled version of a value that updates at most once within a specified time window.\n * This hook uses React's useState internally to manage the throttled state.\n *\n * Throttling ensures the value updates occur at a controlled rate regardless of how frequently the input value changes.\n * This is useful for rate-limiting expensive re-renders or API calls that depend on rapidly changing values.\n *\n * The hook returns a tuple containing:\n * - The throttled value that updates according to the leading/trailing edge behavior specified in the options\n * - The throttler instance with control methods\n *\n * For more direct control over throttling behavior without React 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 [throttledValue, throttler] = useThrottledValue(rawValue, { wait: 1000 });\n *\n * // Opt-in to re-render when execution count changes (optimized for tracking executions)\n * const [throttledValue, throttler] = useThrottledValue(\n *   rawValue,\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 [throttledValue, throttler] = useThrottledValue(\n *   rawValue,\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 [throttledValue, throttler] = useThrottledValue(\n *   rawValue,\n *   { wait: 1000 },\n *   (state) => ({\n *     lastExecutionTime: state.lastExecutionTime,\n *     nextExecutionTime: state.nextExecutionTime\n *   })\n * );\n *\n * // With custom leading/trailing behavior\n * const [throttledValue, throttler] = useThrottledValue(rawValue, {\n *   wait: 1000,\n *   leading: true,   // Update immediately on first change\n *   trailing: false  // Skip trailing edge updates\n * });\n *\n * // Access the selected throttler state (will be empty object {} unless selector provided)\n * const { executionCount, isPending } = throttler.state;\n * ```\n */\nexport function useThrottledValue<\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  ReactThrottler<React.Dispatch<React.SetStateAction<TValue>>, TSelected>,\n] {\n  const [throttledValue, setThrottledValue, throttler] = useThrottledState(\n    value,\n    options,\n    selector,\n  )\n\n  useEffect(() => {\n    setThrottledValue(value)\n  }, [value, setThrottledValue])\n\n  return [throttledValue, throttler]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiFA,SAAgB,kBAId,OACA,SAIA,UAMA;CACA,MAAM,CAAC,gBAAgB,mBAAmB,aAAaA,4CACrD,OACA,SACA,SACD;AAED,4BAAgB;AACd,oBAAkB,MAAM;IACvB,CAAC,OAAO,kBAAkB,CAAC;AAE9B,QAAO,CAAC,gBAAgB,UAAU"}