Hi all! I'm new using channels and have a doubt ab...
# coroutines
b
Hi all! I'm new using channels and have a doubt about how to apply a kind of map in a
ConflatedBroadcastChannel
, this is what I want:
Copy code
val conflated = ConflatedBroadcastChannel<Int>()
val stringConflated: ConflatedBroadcastChannel<String>() = conflated.toStringConflated()

launch {
  stringConflated.consumeEach { println(it) }
}

launch {
  conflated.send(2) // will print "2" (as string)
}
I've tried some approachs to
toStringConflated
such as:
Copy code
suspend fun ConflatedBroadcastChannel<Int>.toStringConflated(): ConflatedBroadcastChannel<String> =
    ConflatedBroadcastChannel<String>().apply {
        this@toConflatedString.consumeEach { // map value here }
    }
But when I apply a
consumerEach
to my
ConflatedBroadcastChannel<Int>
my
ConflatedBroadcastChannel<String>()
stops to consume values, is there a way to do what I'm trying?
found a solution:
Copy code
suspend fun CoroutineScope.mappableConflated(conflated: ConflatedBroadcastChannel<Int>): ConflatedBroadcastChannel<String> =
    ConflatedBroadcastChannel<String>().apply {
        launch {
            conflated.consumeEach {
                this@apply.send(it.toString())
            }
        }
    }
b
Yeah, the issue is your
consumeEach
was causing your code to suspend before
stringConflated
was actually assigned