data: make sure fetch preselected models in the same order of their IDs

This commit is contained in:
Han Yin 2025-09-02 10:56:44 -07:00
parent 2b70887725
commit 36c3768f52
2 changed files with 10 additions and 11 deletions

View File

@ -86,25 +86,24 @@ class HuggingFaceRemoteDataSourceImpl @Inject constructor(
parallelCount: Int, parallelCount: Int,
quorum: Float, quorum: Float,
): List<HuggingFaceModelDetails> = withContext(Dispatchers.IO) { ): List<HuggingFaceModelDetails> = withContext(Dispatchers.IO) {
val successes = mutableListOf<HuggingFaceModelDetails>()
val failures = mutableListOf<Throwable>()
val sem = Semaphore(parallelCount) val sem = Semaphore(parallelCount)
supervisorScope { val results = supervisorScope {
ids.map { id -> ids.map { id ->
async { async {
sem.withPermit { sem.withPermit {
runCatching { getModelDetails(id) } try {
.onSuccess { synchronized(successes) { successes += it } } Result.success(getModelDetails(id))
.onFailure { t -> } catch (t: CancellationException) {
if (t is CancellationException) throw t Result.failure(t)
synchronized(failures) { failures += t }
} }
} }
} }
}.awaitAll() }.awaitAll()
} }
val successes = results.mapNotNull { it.getOrNull() }
val failures = results.mapNotNull { it.exceptionOrNull() }
val total = ids.size val total = ids.size
val failed = failures.size val failed = failures.size
val ok = successes.size val ok = successes.size

View File

@ -190,7 +190,7 @@ class ModelsManagementViewModel @Inject constructor(
val models = modelRepository.fetchPreselectedHuggingFaceModels().map(HuggingFaceModelDetails::toModel) val models = modelRepository.fetchPreselectedHuggingFaceModels().map(HuggingFaceModelDetails::toModel)
_managementState.emit(Download.Ready(models)) _managementState.emit(Download.Ready(models))
} catch (_: CancellationException) { } catch (_: CancellationException) {
// no-op resetManagementState()
} catch (_: UnknownHostException) { } catch (_: UnknownHostException) {
_managementState.value = Download.Error(message = "No internet connection") _managementState.value = Download.Error(message = "No internet connection")
} catch (_: SocketTimeoutException) { } catch (_: SocketTimeoutException) {