combineStates

fun <A, B, R> combineStates(a: StateFlow<A>, b: StateFlow<B>, transform: (A, B) -> R): StateFlow<R>

Like combine for two StateFlows, but the result is a StateFlow, and no CoroutineScope is needed.

val canPay: StateFlow<Boolean> =
combineStates(total, balance) { t, b -> b >= t }

The rules are the same as for mapState: transform runs on every read, so keep it fast and free of side effects. There are overloads for three and four sources.


fun <A, B, C, R> combineStates(a: StateFlow<A>, b: StateFlow<B>, c: StateFlow<C>, transform: (A, B, C) -> R): StateFlow<R>

The three-source form of combineStates. The same rules apply.


fun <A, B, C, D, R> combineStates(a: StateFlow<A>, b: StateFlow<B>, c: StateFlow<C>, d: StateFlow<D>, transform: (A, B, C, D) -> R): StateFlow<R>

The four-source form of combineStates. The same rules apply.