Peter Ertl
10/16/2020, 8:46 AM/**
* receive a chunk of elements from channel.
*
* @param size maximum amount of elements to receive
* @return list of elements (empty list indicates the channel is closed)
*/
@ExperimentalCoroutinesApi
suspend fun <E : Any> ReceiveChannel<E>.receiveChunk(size: Int): List<E> =
mutableListOf<E>().apply {
for (i in 0 until size) {
receiveOrNull()?.apply(::add) ?: break
}
}