本页还没有译文
本页 28 段还没译成Português (BR),下面显示的是英文原文。queue
还没翻译译自英文(tanstack.com)本页 28 段都还没译
function queue<TValue>(fn, initialOptions): (item, position, runOnItemsChange) => boolean;Defined in: queuer.ts:745
Creates a queue that processes items immediately upon addition. Items are processed sequentially in FIFO order by default.
This synchronous version is lighter weight and often all you need - upgrade to asyncQueue when you need promises, retry support, abort capabilities, concurrent execution, or advanced error handling.
State Management:
- Uses TanStack Store for reactive state management
- Use
initialStateto provide initial state values when creating the queuer - Use
onExecutecallback to react to item execution and implement custom logic - Use
onItemsChangecallback to react to items being added or removed from the queue - Use
onExpirecallback to react to items expiring and implement custom logic - Use
onRejectcallback to react to items being rejected when the queue is full - The state includes execution count, expiration count, rejection count, and isRunning status
- State can be accessed via the underlying Queuer instance's
store.stateproperty - When using framework adapters (React/Solid), state is accessed from the hook's state property
Example usage:
// Basic sequential processing
const processItems = queue<number>((n) => console.log(n), {
wait: 1000,
onItemsChange: (queuer) => console.log(queuer.peekAllItems())
});
processItems(1); // Logs: 1
processItems(2); // Logs: 2 after 1 completes
// Priority queue
const processPriority = queue<number>((n) => console.log(n), {
getPriority: n => n // Higher numbers processed first
});
processPriority(1);
processPriority(3); // Processed before 1Type Parameters
TValue
TValue
Parameters
fn
(item) => void
initialOptions
QueuerOptions<TValue>
Returns
(
item,
position?,
runOnItemsChange?): boolean;Adds an item to the queue. If the queue is full, the item is rejected and onReject is called.
undefined cannot be queued (it is the internal "no item" sentinel) and is always rejected.
Items can be inserted based on priority or at the front/back depending on configuration.
Returns true if the item was added, false if the queue is full.
Example usage:
queuer.addItem('task');
queuer.addItem('task2', 'front');Parameters
item
TValue
position?
QueuePosition = ...
runOnItemsChange?
boolean = true
Returns
boolean