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