Mutable Reactive List
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) // 3Parameters
The type of elements contained in the list.
Functions
Returns the collection's content as a StateFlow of an immutable collection IC type.
Executes a block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.
Executes a suspending block of modifications on the underlying mutable collection and notifies observers only once after the block has completed.
Returns a new MutableReactiveList filled with all elements of this collection.
Returns a new MutableReactiveSet filled with all elements of this collection.