function useQueuer<TValue, TSelected>(
fn,
options,
selector): ReactQueuer<TValue, TSelected>
function useQueuer<TValue, TSelected>(
fn,
options,
selector): ReactQueuer<TValue, TSelected>
定义于:react-pacer/src/queuer/useQueuer.ts:132
一个 React Hook,用于创建和管理 Queuer 实例。
这是一个低级 Hook,可以直接访问 Queuer 的功能,而无需任何内置状态管理。通过使用 onItemsChange 回调函数,您可以将其集成到您偏好的任何状态管理解决方案中(useState、Redux、Zustand 等)。
有关具有内置状态管理的 Hook,请参阅 useQueuedState。
Queuer 扩展了基础的 Queue,增加了处理能力。项目按顺序同步处理,并在处理每个项目之间可选择性地添加延迟。Queuer 包含一个内部的 tick 机制,可以启动和停止,使其可用作调度器。启动后,它将在每次 tick 时处理一个项目,并在 tick 之间有可选的等待时间。
默认使用 FIFO(先进先出)行为,但可以通过在添加项目时指定“front”位置来配置为 LIFO(后进先出)。
该 Hook 使用 TanStack Store 进行响应式状态管理。selector 参数允许您指定哪些状态更改将触发重新渲染,通过防止不相关的状态更改导致不必要的重新渲染来优化性能。
默认情况下,不会进行响应式状态订阅,您必须通过提供 selector 函数来选择加入状态跟踪。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供 selector 时,当选定的状态值发生变化时,组件才会重新渲染。
可用的状态属性
• TValue
• TSelected = {}
(item) => void
QueuerOptions<TValue> = {}
(state) => TSelected
ReactQueuer<TValue, TSelected>
// Default behavior - no reactive state subscriptions
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 }
);
// Opt-in to re-render when queue size changes (optimized for displaying queue length)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Opt-in to re-render when processing state changes (optimized for loading indicators)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Example with custom state management and scheduling
const [items, setItems] = useState([]);
const queue = useQueuer(
(item) => console.log('Processing:', item),
{
started: true, // Start processing immediately
wait: 1000, // Process one item every second
onItemsChange: (queue) => setItems(queue.peekAllItems()),
getPriority: (item) => item.priority // Process higher priority items first
}
);
// Add items to process - they'll be handled automatically
queue.addItem('task1');
queue.addItem('task2');
// Control the scheduler
queue.stop(); // Pause processing
queue.start(); // Resume processing
// Access the selected state (will be empty object {} unless selector provided)
const { size, isRunning, executionCount } = queue.state;
// Default behavior - no reactive state subscriptions
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 }
);
// Opt-in to re-render when queue size changes (optimized for displaying queue length)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Opt-in to re-render when processing state changes (optimized for loading indicators)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const queue = useQueuer(
(item) => console.log('Processing:', item),
{ started: true, wait: 1000 },
(state) => ({
executionCount: state.executionCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Example with custom state management and scheduling
const [items, setItems] = useState([]);
const queue = useQueuer(
(item) => console.log('Processing:', item),
{
started: true, // Start processing immediately
wait: 1000, // Process one item every second
onItemsChange: (queue) => setItems(queue.peekAllItems()),
getPriority: (item) => item.priority // Process higher priority items first
}
);
// Add items to process - they'll be handled automatically
queue.addItem('task1');
queue.addItem('task2');
// Control the scheduler
queue.stop(); // Pause processing
queue.start(); // Resume processing
// Access the selected state (will be empty object {} unless selector provided)
const { size, isRunning, executionCount } = queue.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。