getAndReplace

fun <T> MutableStateFlow<T>.getAndReplace(block: T.() -> T): T

A version of getAndUpdate where the current value is passed via this.

This is a simple helper for the common case where you want to copy() a data class:

data class Foo(val num: Int)

val stateFlow = MutableStateFlow(Foo(3))
val oldValue = stateFlow.getAndReplace { copy(num = 5) }

Return

The previous value before replacing.