in coroutinesoperators/src/main/java/com/epam/coroutinesextensions/coroutinesoperators/ChannelOperators.kt [78:107]
suspend fun <T> concat(
context: CoroutineContext = Dispatchers.Unconfined,
first: ReceiveChannel<T>,
second: ReceiveChannel<T>
): ReceiveChannel<T> = first.concatWith(context, second)
fun <T> ReceiveChannel<T>.debounce(
wait: Long = 300,
context: CoroutineContext = Dispatchers.Unconfined,
scope: CoroutineScope = GlobalScope
): ReceiveChannel<T> = scope.produce(context) {
var nextTime = 0L
consumeEach {
val curTime: Long = java.lang.System.currentTimeMillis()
if (curTime < nextTime) {
// not enough time passed from last send
delay(nextTime - curTime)
var mostRecent = it
while (!isEmpty) {
mostRecent = receive()
} // take the most recently sent without waiting
nextTime += wait // maintain strict time interval between sends
send(mostRecent)
} else {
// big pause between original events
nextTime = curTime + wait // start tracking time interval from scratch
send(it)
}
}
}