Fix global search parallelism

pull/203/head
Koitharu 4 years ago
parent 9b748f7334
commit 91b17ef4a2
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -89,11 +89,10 @@ class MultiSearchViewModel(
}
}
private suspend fun searchImpl(q: String) {
private suspend fun searchImpl(q: String) = coroutineScope {
val sources = settings.getMangaSources(includeHidden = false)
val dispatcher = Dispatchers.Default.limitedParallelism(MAX_PARALLELISM)
val deferredList = coroutineScope {
sources.map { source ->
val deferredList = sources.map { source ->
async(dispatcher) {
runCatching {
val list = MangaRepository(source).getList(offset = 0, query = q)
@ -108,7 +107,7 @@ class MultiSearchViewModel(
}
}
}
}
val errors = ArrayList<Throwable>()
for (deferred in deferredList) {
deferred.await()
@ -120,13 +119,12 @@ class MultiSearchViewModel(
errors.add(it)
}
}
if (listData.value.isNotEmpty()) {
return
}
if (listData.value.isEmpty()) {
when (errors.size) {
0 -> Unit
1 -> throw errors[0]
else -> throw CompositeException(errors)
}
}
}
}
Loading…
Cancel
Save