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

Queuer

Class: Queuer<TValue>

Defined in: queuer.ts:122

A flexible queue data structure that defaults to FIFO (First In First Out) behavior with optional position overrides for stack-like or double-ended operations.

The queuer can automatically process items as they are added, with configurable wait times between processing each item. Processing can be started/stopped and the queuer will maintain its state.

Supports priority-based ordering when a getPriority function is provided. Items with higher priority values will be processed first.

Default queue behavior:

  • addItem(item): adds to back of queuer
  • getNextItem(): removes and returns from front of queuer

Stack (LIFO) behavior:

  • addItem(item, 'back'): adds to back
  • getNextItem('back'): removes and returns from back

Double-ended queuer behavior:

  • addItem(item, position): adds to specified position ('front' or 'back')
  • getNextItem(position): removes and returns from specified position

Processing behavior:

  • start(): begins processing items in the queuer
  • stop(): pauses processing
  • wait: configurable delay between processing items
  • onItemsChange/onGetNextItem: callbacks for monitoring queuer state

Example

ts
// FIFO queuer
const queuer = new Queuer<number>();
queuer.addItem(1); // [1]
queuer.addItem(2); // [1, 2]
queuer.getNextItem(); // returns 1, queuer is [2]

// Priority queuer with processing
const priorityQueue = new Queuer<number>({
  getPriority: (n) => n, // Higher numbers have priority
  started: true, // Begin processing immediately
  wait: 1000, // Wait 1s between items
  onGetNextItem: (item, queuer) => console.log(item)
});
priorityQueue.addItem(1); // [1]
priorityQueue.addItem(3); // [3, 1] - 3 processed first
priorityQueue.addItem(2); // [3, 2, 1]
// FIFO queuer
const queuer = new Queuer<number>();
queuer.addItem(1); // [1]
queuer.addItem(2); // [1, 2]
queuer.getNextItem(); // returns 1, queuer is [2]

// Priority queuer with processing
const priorityQueue = new Queuer<number>({
  getPriority: (n) => n, // Higher numbers have priority
  started: true, // Begin processing immediately
  wait: 1000, // Wait 1s between items
  onGetNextItem: (item, queuer) => console.log(item)
});
priorityQueue.addItem(1); // [1]
priorityQueue.addItem(3); // [3, 1] - 3 processed first
priorityQueue.addItem(2); // [3, 2, 1]

Type Parameters

• TValue

Constructors

new Queuer()

ts
new Queuer<TValue>(initialOptions): Queuer<TValue>
new Queuer<TValue>(initialOptions): Queuer<TValue>

Defined in: queuer.ts:131

Parameters

initialOptions

QueuerOptions<TValue> = defaultOptions

Returns

Queuer<TValue>

Methods

addItem()

ts
addItem(
   item, 
   position, 
   runOnUpdate): boolean
addItem(
   item, 
   position, 
   runOnUpdate): boolean

Defined in: queuer.ts:231

Adds an item to the queuer and starts processing if not already running

Parameters

item

TValue

position

QueuePosition = ...

runOnUpdate

boolean = true

Returns

boolean

true if item was added, false if queuer is full


clear()

ts
clear(): void
clear(): void

Defined in: queuer.ts:210

Removes all items from the queuer

Returns

void


getAllItems()

ts
getAllItems(): TValue[]
getAllItems(): TValue[]

Defined in: queuer.ts:347

Returns a copy of all items in the queuer

Returns

TValue[]


getExecutionCount()

ts
getExecutionCount(): number
getExecutionCount(): number

Defined in: queuer.ts:354

Returns the number of items that have been removed from the queuer

Returns

number


getIsEmpty()

ts
getIsEmpty(): boolean
getIsEmpty(): boolean

Defined in: queuer.ts:326

Returns true if the queuer is empty

Returns

boolean


getIsFull()

ts
getIsFull(): boolean
getIsFull(): boolean

Defined in: queuer.ts:333

Returns true if the queuer is full

Returns

boolean


getIsIdle()

ts
getIsIdle(): boolean
getIsIdle(): boolean

Defined in: queuer.ts:375

Returns true if the queuer is running but has no items to process

Returns

boolean


getIsRunning()

ts
getIsRunning(): boolean
getIsRunning(): boolean

Defined in: queuer.ts:368

Returns true if the queuer is running

Returns

boolean


getNextItem()

ts
getNextItem(position): undefined | TValue
getNextItem(position): undefined | TValue

Defined in: queuer.ts:284

Removes and returns an item from the queuer using shift (default) or pop

Parameters

position

QueuePosition = ...

Returns

undefined | TValue

Example

ts
// Standard FIFO queuer
queuer.getNextItem()
// Stack-like behavior (LIFO)
queuer.getNextItem('back')
// Standard FIFO queuer
queuer.getNextItem()
// Stack-like behavior (LIFO)
queuer.getNextItem('back')

getOptions()

ts
getOptions(): Required<QueuerOptions<TValue>>
getOptions(): Required<QueuerOptions<TValue>>

Defined in: queuer.ts:156

Returns the current queuer options

Returns

Required<QueuerOptions<TValue>>


getPeek()

ts
getPeek(position): undefined | TValue
getPeek(position): undefined | TValue

Defined in: queuer.ts:314

Returns an item without removing it

Parameters

position

QueuePosition = ...

Returns

undefined | TValue

Example

ts
// Look at next item to getNextItem
queuer.getPeek()
// Look at last item (like stack top)
queuer.getPeek('back')
// Look at next item to getNextItem
queuer.getPeek()
// Look at last item (like stack top)
queuer.getPeek('back')

getRejectionCount()

ts
getRejectionCount(): number
getRejectionCount(): number

Defined in: queuer.ts:361

Returns the number of items that have been rejected from the queuer

Returns

number


getSize()

ts
getSize(): number
getSize(): number

Defined in: queuer.ts:340

Returns the current size of the queuer

Returns

number


reset()

ts
reset(withInitialItems?): void
reset(withInitialItems?): void

Defined in: queuer.ts:218

Resets the queuer to its initial state

Parameters

withInitialItems?

boolean

Returns

void


setOptions()

ts
setOptions(newOptions): QueuerOptions<TValue>
setOptions(newOptions): QueuerOptions<TValue>

Defined in: queuer.ts:146

Updates the queuer options Returns the new options state

Parameters

newOptions

Partial<QueuerOptions<TValue>>

Returns

QueuerOptions<TValue>


start()

ts
start(): void
start(): void

Defined in: queuer.ts:198

Starts the queuer and processes items

Returns

void


stop()

ts
stop(): void
stop(): void

Defined in: queuer.ts:189

Stops the queuer from processing items

Returns

void

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.