What is the prefered way of sending one time event...
# mvikotlin
n
What is the prefered way of sending one time events to the UI from the stores? Example, we have a success and want to notify the user, I'd emit a Label and I'd be able to catch it in the Component, but how can I send it from Component -> UI?
r
I think you need to expose store.labels field
n
Got an example perhaps?
a
Hello. How UI is implemented? Android views, Compose, or something else?
n
@Arkadii Ivanov React, I went with PublishSubjects in the Components and calling onNext on specific label publishes from the store. Works well, but seems somewhat hacky, not sure why. I am subscribing once the component is mounted and that's it.
a
Indeed it looks hacky. Looks like you can have
val labels: Observable<Label>
as part of the component's interface, and just write something like this in the component's impl:
override val labels: Observable<Label> = store.labels
Or if you want separate types, you can have something like this:
Copy code
interface Some {
    val events: Observable<Event>

    sealed interface Event {
        object Success : Event
    }
}

class SomeComponent : Some {
    val store = ...

    override val events: Observable<Event> = store.labels.map(labelToComponentEvent)
}

internal val labelToComponentEvent: (Label) -> Event = {
    when (it) {
        is Label.Success -> Event.Success
    }
}
👌 1
n
Quick question, if I have a Label that should only notify the component to manipulate the router alongside UI Events like in the above example, I wouldn't be able to just directly map labels to events? Or I'd have to deal with the exhaustive warning for whens right? Eg
Copy code
Label {
 object ShowSomePopup // gets mapped to an event
 object CloseCurrentScreen // doesn't have an event tied to it
}
a
You still can map directly. Your mapper function's result type should be nullable. And you can use
mapNotNull
operator.
But it seems you don't need to map labels to manipulate the router. I would subscribe inside the component and use
when
and manipulate the router.