I'm trying to define a `val textField: Flow<Str...
# coroutines
o
I'm trying to define a
val textField: Flow<String>
that depends on a
val textInputEvents: Channel<TextInputEvent>
and a
val clearTextFieldEvents: Channel<ClearTextInputEvent>
. I'm trying to initialize the textField flow by combining the textInputEvents and clearTextFieldEvents channels and decide the value of the text field based of those two event streams, only problem is that just
Copy code
val textField: Flow<String> = textInputEvents
    .consumeAsFlow()
    .combine(clearTextFieldEvents.consumeAsFlow()) { textInputEvent, clearTextFieldEvent ->
        // Which one of the two just emitted?
        // return textInputEvent.text or ""
    }
wouldn't do it, since I have to know which one of the channels emitted value is the most recent one, which isnt possible. So my question kinda is: is there a combine operator that also tells you which flow emitted the most recent value? EDIT: perhaps this is a usecase for the select expression (https://kotlinlang.org/docs/reference/coroutines/select-expression.html) ?
d
Does it matter which one was emitted? If yes, then probably select or just map one event to the other? (They are probably related anyways?)
o
Could you elaborate what you mean with mapping one event to another>
d
As in: Maybe for your purposes a`ClearTextInputEvent` is equal to an
TextInputEvent("")
.
Copy code
val textField: Flow<String> = textInputEvents
    .consumeAsFlow()
    .merge(clearTextFieldEvents.map { TextInputEvent("") }.consumeAsFlow()) { event ->
    }
so we just map one to the other and merge
Sorry, just saw that you were using combine. You probably want
merge
(takes whatever happens on any of them). For merge, emitted objects have to be of same type. Depending on what you're doing inside the collector, you can also transform both to a string right away (if you only need the new string)
o
Thanks, that is a good idea