what's the best way to emit the results of one flo...
# coroutines
w
what's the best way to emit the results of one flow into another? For example i am using a bluetooth library that has states for
Connected
and
Disconnected
as a
StateFlow
. I am trying to add more states such as
Connecting
or
LookingForSavedDevice
at a higher level in my program so I would like to use a
StateFlow
which both takes in emissions from the library functions as well as allow me to mutate the
StateFlow
myself. So I think I'm looking for a way to collect into another flow:
bluetoothLibrary.stateFlow.collect(myOtherStateFlow)
I'm aware I can just emit into the new flow in a
collect { }
block or
onEach { }
but I'm not sure what is canonical
e
Copy code
myOtherStateFlow.emitAll(bluetoothLibrary.stateFlow)
is equivalent to
Copy code
bluetoothLibrary.stateFlow.collect { myOtherStateFlow.emit(it) }
but is that really what you want? in most cases I'd consider
Copy code
val myFlow = merge(
    bluetoothLibrary.stateFlow,
    myOtherStateFlow,
)
to be better than having multiple mutators of a single MutableStateFlow
🙏 1
w
I agree that might be cleaner to just use a
merge
. I'll have to check if that still returns a
StateFlow
or just a
Flow
at that point but otherwise that's a great idea. Thank you