KQueue

class KQueue<T>(initialItems: List<T> = emptyList())(source)

A reactive FIFO queue.

Port of the useQueue React hook (which itself built on useArray).

val queue = KQueue<String>()
queue.enqueue("first")
queue.enqueue("second")

println(queue.peek()) // "first"
println(queue.dequeue()) // "first"
println(queue.size) // 1

// Observe reactively:
queue.state.collect { items -> render(items) }

Parameters

initialItems

Items to pre-populate the queue with (front → back order).

Constructors

Link copied to clipboard
constructor(initialItems: List<T> = emptyList())

Properties

Link copied to clipboard

true when the queue contains no items.

Link copied to clipboard
val size: Int

Number of items currently in the queue.

Link copied to clipboard
val state: StateFlow<List<T>>

The underlying queue contents as a StateFlow (front → back).

Functions

Link copied to clipboard
fun clear()

Removes all items from the queue.

Link copied to clipboard
fun dequeue(): T?

Removes and returns the front item, or null if the queue is empty.

Link copied to clipboard
fun enqueue(item: T)

Adds item to the back of the queue.

Link copied to clipboard
fun peek(): T?

Returns the front item without removing it, or null if empty.

Link copied to clipboard
open override fun toString(): String