https://kotlinlang.org logo
#coroutines
Title
# coroutines
w

withoutclass

02/27/2018, 3:28 PM
has anyone run into needing to debounce a channel yet?
j

Jonathan

02/27/2018, 3:28 PM
I use
Copy code
fun <E> ReceiveChannel<E>.debounce(duration: Long, timeUnit: TimeUnit = TimeUnit.MILLISECONDS): ReceiveChannel<E> = produce {
  var job: Job? = null

  consumeEach { element ->
    job?.cancel()
    job = launch(coroutineContext) {
      delay(duration, timeUnit)
      send(element)
    }
  }

  job?.join()
}
w

withoutclass

02/27/2018, 3:29 PM
Very cool, thank you!
w

withoutclass

02/28/2018, 3:55 PM
Beautiful, I found a similar link from Roman by googling quick.
I'll have to take a look at the two versions and compare 😄
5 Views