Arjan van Wieringen
05/28/2022, 6:34 PMmap
, but if I want it to be a StateFlow again I need to use stateIn
but then I require a scope. Or is the mapped result still a stateflow?
The usecase is I have a class that I want to expose a StateFlow which is based on another one. But if I want to use stateIn
I need to do this from a coroutine. So it becomes something like this:
interface History {
val activeLocation: StateFlow<Location>
// ...
}
class Router<R>(
private val history: History,
private val routing: Routing<R>,
private val scope: CoroutineScope
) {
lateinit var activeRoute : StateFlow<R>
private set
init {
scope.launch {
activeRoute = history.activeLocation
.map { location -> routing.getRoute(location) }
.stateIn(scope)
}
}
}
I am not sure if this is the most optimal way of doing it... I have the feeling I am overcomplicating stuff.Robert Menke
05/28/2022, 7:47 PMscope.launch
though.Arjan van Wieringen
05/29/2022, 5:44 AMstojan
05/29/2022, 11:02 AMArjan van Wieringen
05/29/2022, 12:37 PMhfhbd
05/29/2022, 2:07 PMJoost Klitsie
06/07/2022, 8:46 AMval myFlow = otherFlow.stateIn(scope, Eagerly, <defaultValue>
Joost Klitsie
06/07/2022, 8:47 AMactiveRoute = history.activeLocation().map {..}.stateIn(scope, Eagerly, <defaultValue>)
Joost Klitsie
06/07/2022, 8:47 AMJoost Klitsie
06/07/2022, 8:48 AMArjan van Wieringen
06/07/2022, 10:51 AM