{"version":3,"file":"useAsyncQueuedState.cjs","names":["useAsyncQueuer"],"sources":["../../src/async-queuer/useAsyncQueuedState.ts"],"sourcesContent":["import { useAsyncQueuer } from './useAsyncQueuer'\nimport type {\n  ReactAsyncQueuer,\n  ReactAsyncQueuerOptions,\n} from './useAsyncQueuer'\nimport type { AsyncQueuerState } from '@tanstack/pacer/async-queuer'\n\n/**\n * A higher-level React hook that creates an `AsyncQueuer` instance with built-in state management.\n *\n * This hook combines an AsyncQueuer with React state to automatically track the queue items.\n * It returns a tuple containing:\n * - The current array of queued items as React state\n * - The queuer instance with methods to control the queue\n *\n * The queue can be configured with:\n * - Maximum concurrent operations\n * - Maximum queue size\n * - Processing function for queue items\n * - Various lifecycle callbacks\n *\n * The state will automatically update whenever items are:\n * - Added to the queue\n * - Removed from the queue\n * - Started processing\n * - Completed processing\n *\n * ## State Management and Selector\n *\n * The hook uses TanStack Store for reactive state management via the underlying async queuer instance.\n * The `selector` parameter allows you to specify which async 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 async queuer state properties:\n * - `activeItems`: Items currently being processed by the queuer\n * - `errorCount`: Number of task executions that have resulted in errors\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 * - `lastResult`: The result from the most recent task execution\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 * - `settledCount`: Number of task executions that have completed (success or error)\n * - `size`: Number of items currently in the queue\n * - `status`: Current processing status ('idle' | 'running' | 'stopped')\n * - `successCount`: Number of task executions that have completed successfully\n *\n * @example\n * ```tsx\n * // Default behavior - no reactive state subscriptions\n * const [queueItems, asyncQueuer] = useAsyncQueuedState(\n *   async (item) => {\n *     const result = await processItem(item);\n *     return result;\n *   },\n *   {\n *     concurrency: 2,\n *     maxSize: 100,\n *     started: true\n *   }\n * );\n *\n * // Opt-in to re-render when queue contents change (optimized for displaying queue items)\n * const [queueItems, asyncQueuer] = useAsyncQueuedState(\n *   async (item) => {\n *     const result = await processItem(item);\n *     return result;\n *   },\n *   { concurrency: 2, maxSize: 100, started: true },\n *   (state) => ({\n *     items: state.items,\n *     size: state.size,\n *     isEmpty: state.isEmpty,\n *     isFull: state.isFull\n *   })\n * );\n *\n * // Opt-in to re-render when processing state changes (optimized for loading indicators)\n * const [queueItems, asyncQueuer] = useAsyncQueuedState(\n *   async (item) => {\n *     const result = await processItem(item);\n *     return result;\n *   },\n *   { concurrency: 2, maxSize: 100, started: true },\n *   (state) => ({\n *     isRunning: state.isRunning,\n *     isIdle: state.isIdle,\n *     status: state.status,\n *     activeItems: state.activeItems,\n *     pendingTick: state.pendingTick\n *   })\n * );\n *\n * // Opt-in to re-render when execution metrics change (optimized for stats display)\n * const [queueItems, asyncQueuer] = useAsyncQueuedState(\n *   async (item) => {\n *     const result = await processItem(item);\n *     return result;\n *   },\n *   { concurrency: 2, maxSize: 100, started: true },\n *   (state) => ({\n *     successCount: state.successCount,\n *     errorCount: state.errorCount,\n *     settledCount: state.settledCount,\n *     expirationCount: state.expirationCount,\n *     rejectionCount: state.rejectionCount\n *   })\n * );\n *\n * // Opt-in to re-render when results are available (optimized for data display)\n * const [queueItems, asyncQueuer] = useAsyncQueuedState(\n *   async (item) => {\n *     const result = await processItem(item);\n *     return result;\n *   },\n *   { concurrency: 2, maxSize: 100, started: true },\n *   (state) => ({\n *     lastResult: state.lastResult,\n *     successCount: state.successCount\n *   })\n * );\n *\n * // Add items to queue - state updates automatically\n * asyncQueuer.addItem(async () => {\n *   const result = await fetchData();\n *   return result;\n * });\n *\n * // Start processing\n * asyncQueuer.start();\n *\n * // Stop processing\n * asyncQueuer.stop();\n *\n * // queueItems reflects current queue state\n * const pendingCount = asyncQueuer.peekPendingItems().length;\n *\n * // Access the selected async queuer state (will be empty object {} unless selector provided)\n * const { size, isRunning, activeItems } = asyncQueuer.state;\n * ```\n */\nexport function useAsyncQueuedState<\n  TValue,\n  TSelected extends Pick<AsyncQueuerState<TValue>, 'items'> = Pick<\n    AsyncQueuerState<TValue>,\n    'items'\n  >,\n>(\n  fn: (value: TValue) => Promise<any>,\n  options: ReactAsyncQueuerOptions<TValue, TSelected> = {},\n  selector?: (state: AsyncQueuerState<TValue>) => TSelected,\n): [Array<TValue>, ReactAsyncQueuer<TValue, TSelected>] {\n  const asyncQueuer = useAsyncQueuer(fn, options, selector)\n\n  return [asyncQueuer.state.items, asyncQueuer]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsJA,SAAgB,oBAOd,IACA,UAAsD,EAAE,EACxD,UACsD;CACtD,MAAM,cAAcA,sCAAe,IAAI,SAAS,SAAS;AAEzD,QAAO,CAAC,YAAY,MAAM,OAAO,YAAY"}