com.fireflysource.common.coroutine.ChannelExtension.kt Maven / Gradle / Ivy
The newest version!
package com.fireflysource.common.coroutine
import kotlinx.coroutines.channels.Channel
inline fun Channel.consumeAll(crossinline block: (T) -> Unit) {
try {
while (true) {
val result = this.tryReceive()
if (result.isFailure) {
break
}
val message = result.getOrNull() ?: break
block(message)
}
} catch (ignore: Exception) {
}
}
fun Channel.clear() {
this.consumeAll { }
}