Observing the State machine and using the as a constructore parameter to the molecule presenter, passing the event to the State Machine via the Presenter
class CounterPresenter(
private val counterStateMachine: CounterStateMachine
) {
private val events = MutableSharedFlow<CounterEvent>(extraBufferCapacity = 1)
@Composable
fun uiModel(): CounterState {
var counterState by remember {
mutableStateOf(CounterStateMachine.defaultCounterState())
}
LaunchedEffect(Unit) {
events.collect { counterEvent ->
counterStateMachine.dispatch(counterEvent)
}
}
LaunchedEffect(Unit) {
counterStateMachine.state.collect { currentState ->
counterState = currentState
}
}
return counterState
}
fun processEvent(event: CounterEvent) {
events.tryEmit(event)
}
}