Fix global search parallelism

This commit is contained in:
Koitharu
2022-07-14 14:23:04 +03:00
parent 9b748f7334
commit 91b17ef4a2

View File

@@ -89,26 +89,25 @@ 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) .toUi(ListMode.GRID)
.toUi(ListMode.GRID) if (list.isNotEmpty()) {
if (list.isNotEmpty()) { MultiSearchListModel(source, list.size > MIN_HAS_MORE_ITEMS, list)
MultiSearchListModel(source, list.size > MIN_HAS_MORE_ITEMS, list) } else {
} else { null
null
}
}.onFailure {
it.printStackTraceDebug()
} }
}.onFailure {
it.printStackTraceDebug()
} }
} }
} }
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) {
} 0 -> Unit
when (errors.size) { 1 -> throw errors[0]
0 -> Unit else -> throw CompositeException(errors)
1 -> throw errors[0] }
else -> throw CompositeException(errors)
} }
} }
} }