https://kotlinlang.org logo
r

rafal

07/27/2017, 12:24 PM
Is it an overkill, what do you think?
Copy code
class LoopBreaker {
    var stop = false
        private set

    fun breakOut() {
        stop = true
    }
}

fun <T> ReceiveChannel<T>.consumeEachOn(context: CoroutineContext, action: suspend LoopBreaker.(T) -> Unit) = launch(context) {
    val breaker = LoopBreaker()
    for (element in this@consumeEachOn) {
        breaker.action(element)
        if (breaker.stop) break
    }
}