dave08
12/23/2024, 10:58 AMpublic fun <T> MutableState<T>.updateCopy(block: Copy<T>.() -> Unit) {
update { it.copy(block) }
}
/**
* Updates the value in this [MutableStateFlow]
* by performing the operations in the [Copy] [block].
*/
public fun <T> MutableStateFlow<T>.updateCopy(block: Copy<T>.() -> Unit) {
update { it.copy(block) }
}
the update functions themselves are and would allow to call suspend funs from within them... so if these were too, we could use the current ui state to call the suspend fun and update it atomically...stojan
12/23/2024, 11:29 AMdave08
12/23/2024, 11:53 AMstojan
12/23/2024, 11:56 AMdave08
12/23/2024, 11:57 AMdave08
12/23/2024, 11:58 AMstojan
12/23/2024, 12:02 PMUpdates the MutableStateFlow.value atomically using the specified function of its value.
function may be evaluated multiple times, if value is being concurrently updated.source: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/update.html
dave08
12/23/2024, 12:03 PMdave08
12/23/2024, 12:04 PMdave08
12/23/2024, 12:07 PMstojan
12/23/2024, 12:08 PMdave08
12/23/2024, 12:09 PMuiState.update { prev ->
val foo = someService.getFoo(prev.id)
prev.copy(foo = foo)
}
stojan
12/23/2024, 12:11 PMval foo = someService.getFoo(uiState.value.id)
uiState.update { prev -> prev.copy(foo == foo) }
now, the update
function is pure, and safe to execute multiple timesdave08
12/23/2024, 12:13 PMdave08
12/23/2024, 12:13 PMdave08
12/23/2024, 12:14 PMstojan
12/23/2024, 12:32 PMdave08
12/23/2024, 5:23 PM