package com.margelo.nitro.iap import dev.hyo.openiap.ProductCommon import dev.hyo.openiap.ProductQueryType import kotlin.coroutines.cancellation.CancellationException import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope internal suspend fun collectAllQueryProducts( skusList: List, fetchKind: suspend (ProductQueryType) -> List, onFailure: (ProductQueryType, Throwable) -> Unit = { _, _ -> }, ): List = coroutineScope { val byId = linkedMapOf() var firstFailure: Throwable? = null val queries = listOf(ProductQueryType.InApp, ProductQueryType.Subs).map { kind -> kind to async { runCatching { fetchKind(kind) } } } queries.forEach { (kind, query) -> query.await().onSuccess { fetched -> fetched.forEach { product -> byId.putIfAbsent(product.id, product) } }.onFailure { error -> if (error is CancellationException) throw error onFailure(kind, error) if (firstFailure == null) firstFailure = error } } if (byId.isEmpty()) { firstFailure?.let { throw it } } skusList.mapNotNull { byId[it] } }