{"version":3,"file":"useQueuedState.cjs","names":["useQueuer"],"sources":["../../src/queuer/useQueuedState.ts"],"sourcesContent":["import { useQueuer } from './useQueuer'\nimport type { ReactQueuer, ReactQueuerOptions } from './useQueuer'\nimport type { Queuer, QueuerState } from '@tanstack/pacer/queuer'\n\n/**\n * A React hook that creates a queuer with managed state, combining React's useState with queuing functionality.\n * This hook provides both the current queue state and queue control methods.\n *\n * The queue state is automatically updated whenever items are added, removed, or reordered in the queue.\n * All queue operations are reflected in the state array returned by the hook.\n *\n * The queue can be started and stopped to automatically process items at a specified interval,\n * making it useful as a scheduler. When started, it will process one item per tick, with an\n * optional wait time between ticks.\n *\n * The hook returns a tuple containing:\n * - The current queue state as an array\n * - The queue instance with methods for queue manipulation\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying queuer instance.\n * The `selector` parameter allows you to specify which queuer 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 queuer state properties:\n * - `executionCount`: Number of items that have been processed by the queuer\n * - `expirationCount`: Number of items that have been removed due to expiration\n * - `isEmpty`: Whether the queuer has no items to process\n * - `isFull`: Whether the queuer has reached its maximum capacity\n * - `isIdle`: Whether the queuer is not currently processing any items\n * - `isRunning`: Whether the queuer is active and will process items automatically\n * - `items`: Array of items currently waiting to be processed\n * - `itemTimestamps`: Timestamps when items were added for expiration tracking\n * - `pendingTick`: Whether the queuer has a pending timeout for processing the next item\n * - `rejectionCount`: Number of items that have been rejected from being added\n * - `size`: Number of items currently in the queue\n * - `status`: Current processing status ('idle' | 'running' | 'stopped')\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [items, addItem, queue] = useQueuedState(\n *   (item) => console.log('Processing:', item),\n *   {\n *     initialItems: ['item1', 'item2'],\n *     started: true,\n *     wait: 1000,\n *     getPriority: (item) => item.priority\n *   }\n * );\n *\n * // Opt-in to re-render when queue contents change (optimized for displaying queue items)\n * const [items, addItem, queue] = useQueuedState(\n *   (item) => console.log('Processing:', item),\n *   { started: true, wait: 1000 },\n *   (state) => ({\n *     items: state.items,\n *     size: state.size,\n *     isEmpty: state.isEmpty\n *   })\n * );\n *\n * // Opt-in to re-render when processing state changes (optimized for loading indicators)\n * const [items, addItem, queue] = useQueuedState(\n *   (item) => console.log('Processing:', item),\n *   { started: true, wait: 1000 },\n *   (state) => ({\n *     isRunning: state.isRunning,\n *     isIdle: state.isIdle,\n *     status: state.status,\n *     pendingTick: state.pendingTick\n *   })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for stats display)\n * const [items, addItem, queue] = useQueuedState(\n *   (item) => console.log('Processing:', item),\n *   { started: true, wait: 1000 },\n *   (state) => ({\n *     executionCount: state.executionCount,\n *     expirationCount: state.expirationCount,\n *     rejectionCount: state.rejectionCount\n *   })\n * );\n *\n * // Add items to queue\n * const handleAdd = (item) => {\n *   addItem(item);\n * };\n *\n * // Start automatic processing\n * const startProcessing = () => {\n *   queue.start();\n * };\n *\n * // Stop automatic processing\n * const stopProcessing = () => {\n *   queue.stop();\n * };\n *\n * // Manual processing still available\n * const handleProcess = () => {\n *   const nextItem = queue.getNextItem();\n *   if (nextItem) {\n *     processItem(nextItem);\n *   }\n * };\n *\n * // Access the selected queuer state (will be empty object {} unless selector provided)\n * const { size, isRunning, executionCount } = queue.state;\n * ```\n */\nexport function useQueuedState<\n  TValue,\n  TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<\n    QueuerState<TValue>,\n    'items'\n  >,\n>(\n  fn: (item: TValue) => void,\n  options: ReactQueuerOptions<TValue, TSelected> = {},\n  selector?: (state: QueuerState<TValue>) => TSelected,\n): [Array<TValue>, Queuer<TValue>['addItem'], ReactQueuer<TValue, TSelected>] {\n  const queue = useQueuer(fn, options, selector)\n\n  return [queue.state.items, queue.addItem, queue]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsHA,SAAgB,eAOd,IACA,UAAiD,EAAE,EACnD,UAC4E;CAC5E,MAAM,QAAQA,4BAAU,IAAI,SAAS,SAAS;AAE9C,QAAO;EAAC,MAAM,MAAM;EAAO,MAAM;EAAS;EAAM"}