vcs: work around 'coroutineToIndicator' cancellation

GitOrigin-RevId: 91a873513decc6fec836654310e3e3f3b6b44e7a
This commit is contained in:
Aleksey Pivovarov
2023-03-13 19:42:17 +00:00
committed by intellij-monorepo-bot
parent 1a1ba2c247
commit a06ba79aee
@@ -124,7 +124,7 @@ abstract class AsyncChangesTree : ChangesTree {
private fun start() {
scope.launch {
_requests.collectLatest { request ->
_requests.asyncCollectLatest(scope) { request ->
handleRequest(request)
}
}
@@ -287,3 +287,19 @@ abstract class TwoStepAsyncChangesTreeModel<T>(val scope: CoroutineScope) : Asyn
return newJob.await()
}
}
/**
* [coroutineToIndicator]-friendly [collectLatest] implementation. Otherwise, indicator will never be cancelled.
*/
private suspend fun <T> Flow<T>.asyncCollectLatest(scope: CoroutineScope, action: suspend (value: T) -> Unit) {
var lastJob: Job? = null
collect { request ->
lastJob?.let {
it.cancel()
it.join()
}
lastJob = scope.launch {
action(request)
}
}
}