I'm having issues with transforming Android View l...
# coroutines
t
I'm having issues with transforming Android View listeners to channels. I've tried two different approaches, one through
produce
, and the other using a manual `ConflatedChannel`:
Copy code
fun View.clicks(): ReceiveChannel<Unit> = produce(bg, capacity = Channel.CONFLATED) {
    setOnClickListener { offer(Unit) }
}

fun View.clicks(): ReceiveChannel<Unit> = ConflatedChannel<Unit>().apply {
    setOnClickListener { offer(Unit) }
}
First one results in a
ClosedSendChannelException: Channel was closed
on every device I've tried it on, while the second one actually works on some devices, while causing an exception on others. Any idea on what I'm doing wrong? Common use case is simple routing to another channel:
Copy code
bg {
    someButton.clicks().map { SomeIntent }.consumeEach { intentChannel?.offer(it) }
}
Note:
bg
is just an alias for
CommonPool
.