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

louiscad

02/28/2018, 1:36 PM
Hi, is there a way to have a
ReceiveChannel
/ actor that cancels the work done for a value if a new one is received? My goal is to perform a computation for the latest received value and drop any in progress work.
j

Jonathan

02/28/2018, 1:38 PM
Copy code
val actor = actor<Int> {

  var job: Job? = null

  consumeEach {
    job?.cancel()
    job = launch(coroutineContext) {
      // TODO
    }
  }
}
Would it behave as you want ?
l

louiscad

02/28/2018, 1:55 PM
@Jonathan Yes! Thank you! 🙂
3 Views