zak.taccardi
04/08/2020, 12:16 AMuserId
in a Flow<State>
where the userId
changes over time?
data class State(
val userId: String, // the current user may change over time
val isHungry: Boolean, // user may become hungry over time
val currentTime: Int // hot code path, changes frequently
)
fun CoroutineScope.handleFirstStateForGoodPeople(states: Flow<State>) {
states
// when EACH user has `isHungry==true`
// I want to call a function
// but only the FIRST time this state is reached PER user (distinct by `userId`)
.onEachInternal { state -> callSomeFunction() }
.launchIn(this)
}
bezrukov
04/08/2020, 6:21 AMdistinct
then it will be
states.filter { it.isHungry }.distinctBy { it.user }