Tim Malseed
01/04/2021, 6:23 AMActor
, and I'd like to be able to remove/cancel an operation when a new one comes in, depending on the progress of the previous operation.Tim Malseed
01/04/2021, 6:25 AMprivate val actor = scope.actor<AdapterOperation>(capacity = Channel.CONFLATED) {
for (operation in channel) {
when (operation) {
is AdapterOperation.Update -> updateInternal()
...
}
}
}
private suspend fun updateInternal() {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val result = performSomeLongRunningTask()
}
// Todo:
// If our long running task has completed, then continue.
// But if a new update has been queued and we haven't reached
// this point, then cancel this operation.
if (coroutineContext.isActive) {
withContext(Dispatchers.Main) {
applyResultOfLongRunningTask(result)
}
}
}
Tim Malseed
01/04/2021, 6:26 AMUpdate
operation is sent, then any previous Update
operation in the queue, which has not reached a certain point (see code comment above), would be cancelled/removed from the queueTim Malseed
01/04/2021, 6:27 AMtseisel
01/04/2021, 12:57 PMscope.actor<Input> {
var job: Job? = null
for (input in channel) {
job?.cancel()
job = launch { /* some lengthy operation */ }
}
}
Tim Malseed
01/05/2021, 4:45 AM