MutableReactiveList

A mutable list that also exposes a StateFlow via asStateFlow, allowing for reactive observation of its contents.

It delegates MutableList functionality to an internal list and emits an immutable List snapshot to its collectors whenever the list is modified. This enables reactive programming patterns where observers can automatically respond to changes in the list's contents.

The interface extends both MutableReactiveCollection and MutableList, providing all standard list operations while maintaining reactive capabilities. Additional extension functions like getAsFlow and subListAsFlow allow for more granular observation of specific elements or ranges.

Example:

val todoList = reactiveListOf("Buy groceries", "Walk the dog")

// Observe all changes to the list
todoList.asStateFlow().collect { todos ->
println("Todo list updated: $todos")
} // Immediately emits: "Todo list updated: [Buy groceries, Walk the dog]"

// Observe specific index changes
todoList.getAsFlow(0).collect { firstTodo ->
println("First todo: $firstTodo")
} // Immediately emits: "First todo: Buy groceries"

// Perform batch operations to minimize emissions
todoList.batchNotify {
add("Clean house")
removeAt(1)
set(0, "Buy organic groceries")
} // Single emission: "Todo list updated: [Buy organic groceries, Clean house]"

// Standard list operations work as expected
todoList.add("Exercise") // Triggers emission
todoList[1] = "Deep clean house" // Triggers emission
println(todoList.size) // 3

Parameters

E

The type of elements contained in the list.

Properties

Link copied to clipboard
abstract override val size: Int

Functions

Link copied to clipboard
abstract override fun add(element: E): Boolean
abstract fun add(index: Int, element: E)
Link copied to clipboard
abstract override fun addAll(elements: Collection<E>): Boolean
abstract fun addAll(index: Int, elements: Collection<E>): Boolean
Link copied to clipboard
abstract fun asStateFlow(): StateFlow<List<E>>

Returns the collection's content as a StateFlow of an immutable collection IC type.

Link copied to clipboard
abstract fun batchNotify(block: MutableList<E>.() -> Unit)

Executes a block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.

Link copied to clipboard
abstract suspend fun batchNotifyAsync(block: suspend MutableList<E>.() -> Unit)

Executes a suspending block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.

Link copied to clipboard
abstract override fun clear()
Link copied to clipboard
abstract operator override fun contains(element: E): Boolean
Link copied to clipboard
abstract override fun containsAll(elements: Collection<E>): Boolean
Link copied to clipboard
abstract operator fun get(index: Int): E
Link copied to clipboard
fun <E> MutableReactiveList<E>.getAsFlow(index: Int): Flow<E?>

Creates a Flow that emits the element at the specified index whenever the list changes.

Link copied to clipboard
abstract fun indexOf(element: E): Int
Link copied to clipboard
abstract override fun isEmpty(): Boolean
Link copied to clipboard
abstract operator override fun iterator(): Iterator<E>
Link copied to clipboard
abstract fun lastIndexOf(element: E): Int
Link copied to clipboard
abstract override fun listIterator(): MutableListIterator<E>
abstract override fun listIterator(index: Int): MutableListIterator<E>
Link copied to clipboard
abstract override fun remove(element: E): Boolean
Link copied to clipboard
abstract override fun removeAll(elements: Collection<E>): Boolean
Link copied to clipboard
abstract fun removeAt(index: Int): E
Link copied to clipboard
abstract override fun retainAll(elements: Collection<E>): Boolean
Link copied to clipboard
abstract operator fun set(index: Int, element: E): E
Link copied to clipboard
abstract override fun subList(fromIndex: Int, toIndex: Int): MutableList<E>
Link copied to clipboard
fun <E> MutableReactiveList<E>.subListAsFlow(fromIndex: Int, toIndex: Int, strict: Boolean = true): Flow<List<E>>

Returns a Flow that emits a sublist view of this reactive list between the specified fromIndex (inclusive) and toIndex (exclusive).

Link copied to clipboard

Returns a new MutableReactiveList filled with all elements of this collection.

Link copied to clipboard

Returns a new MutableReactiveSet filled with all elements of this collection.