function useAsyncQueuer<TValue, TSelected>(
fn,
options,
selector): ReactAsyncQueuer<TValue, TSelected>
function useAsyncQueuer<TValue, TSelected>(
fn,
options,
selector): ReactAsyncQueuer<TValue, TSelected>
定义于:react-pacer/src/async-queuer/useAsyncQueuer.ts:167
一个更底层的 React hook,用于创建一个 AsyncQueuer 实例来管理异步任务队列。
特性
任务会以配置的并发限制并行处理。当一个任务完成时,如果低于并发限制,下一个待处理的任务就会被处理。
错误处理
该 hook 使用 TanStack Store 进行响应式状态管理。selector 参数允许您指定哪些状态更改将触发重新渲染,通过防止不相关状态更改时的不必要重新渲染来优化性能。
默认情况下,不会有响应式状态订阅,您必须通过提供一个 selector 函数来选择跟踪状态。这可以防止不必要的重新渲染,并让您完全控制组件何时更新。只有当您提供一个 selector 时,组件才会在选定的状态值发生变化时重新渲染。
可用的状态属性
• TValue
• TSelected = {}
(value) => Promise<any>
AsyncQueuerOptions<TValue> = {}
(state) => TSelected
ReactAsyncQueuer<TValue, TSelected>
// Default behavior - no reactive state subscriptions
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false }
);
// Opt-in to re-render when queue size changes (optimized for displaying queue length)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Opt-in to re-render when processing state changes (optimized for loading indicators)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
activeItems: state.activeItems,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false },
(state) => ({
successCount: state.successCount,
errorCount: state.errorCount,
settledCount: state.settledCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Opt-in to re-render when results are available (optimized for data display)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{
concurrency: 2,
maxSize: 100,
started: false,
onSuccess: (result) => {
console.log('Item processed:', result);
},
onError: (error) => {
console.error('Processing failed:', error);
}
},
(state) => ({
lastResult: state.lastResult,
successCount: state.successCount
})
);
// Add items to queue
asyncQueuer.addItem(newItem);
// Start processing
asyncQueuer.start();
// Access the selected state (will be empty object {} unless selector provided)
const { size, isRunning, activeItems } = asyncQueuer.state;
// Default behavior - no reactive state subscriptions
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false }
);
// Opt-in to re-render when queue size changes (optimized for displaying queue length)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false },
(state) => ({
size: state.size,
isEmpty: state.isEmpty,
isFull: state.isFull
})
);
// Opt-in to re-render when processing state changes (optimized for loading indicators)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false },
(state) => ({
isRunning: state.isRunning,
isIdle: state.isIdle,
status: state.status,
activeItems: state.activeItems,
pendingTick: state.pendingTick
})
);
// Opt-in to re-render when execution metrics change (optimized for stats display)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{ concurrency: 2, maxSize: 100, started: false },
(state) => ({
successCount: state.successCount,
errorCount: state.errorCount,
settledCount: state.settledCount,
expirationCount: state.expirationCount,
rejectionCount: state.rejectionCount
})
);
// Opt-in to re-render when results are available (optimized for data display)
const asyncQueuer = useAsyncQueuer(
async (item) => {
const result = await processItem(item);
return result;
},
{
concurrency: 2,
maxSize: 100,
started: false,
onSuccess: (result) => {
console.log('Item processed:', result);
},
onError: (error) => {
console.error('Processing failed:', error);
}
},
(state) => ({
lastResult: state.lastResult,
successCount: state.successCount
})
);
// Add items to queue
asyncQueuer.addItem(newItem);
// Start processing
asyncQueuer.start();
// Access the selected state (will be empty object {} unless selector provided)
const { size, isRunning, activeItems } = asyncQueuer.state;
您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。