Framework
Version
Debouncer API Reference
Throttler API Reference
Rate Limiter API Reference
Queue API Reference
Batcher API Reference

queue

Function: queue()

ts
function queue<TValue>(fn, initialOptions): (item, position, runOnItemsChange) => boolean
function queue<TValue>(fn, initialOptions): (item, position, runOnItemsChange) => boolean

Defined in: queuer.ts:672

Creates a queue that processes items immediately upon addition. Items are processed sequentially in FIFO order by default.

This is a simplified wrapper around the Queuer class that only exposes the addItem method. The queue is always isRunning and will process items as they are added. For more control over queue processing, use the Queuer class directly.

State Management:

  • Uses TanStack Store for reactive state management
  • Use initialState to provide initial state values when creating the queuer
  • Use onExecute callback to react to item execution and implement custom logic
  • Use onItemsChange callback to react to items being added or removed from the queue
  • Use onExpire callback to react to items expiring and implement custom logic
  • Use onReject callback 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.state property
  • When using framework adapters (React/Solid), state is accessed from the hook's state property

Example usage:

ts
// 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 1
// 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 1

Type Parameters

TValue

Parameters

fn

(item) => void

initialOptions

QueuerOptions<TValue>

Returns

Function

Adds an item to the queue. If the queue is full, the item is rejected and onReject is called. 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:

ts
queuer.addItem('task');
queuer.addItem('task2', 'front');
queuer.addItem('task');
queuer.addItem('task2', 'front');

Parameters

item

TValue

position

QueuePosition = ...

runOnItemsChange

boolean = true

Returns

boolean

Our Partners
Unkey
Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.