Updated on 2026-08-14

This commit is contained in:
Tangem 2023-10-10 22:14:03 +03:00
parent 8027be74aa
commit a730af0f1f
9 changed files with 174 additions and 35 deletions

View file

@ -51,6 +51,12 @@ internal class DefaultNetworksRepository(
}
}.cancellable()
override suspend fun fetchNetworkPendingTransactions(userWalletId: UserWalletId, networks: Set<Network>) {
withContext(dispatchers.io) {
fetchNetworksPendingTransactionsIfCacheExpired(userWalletId, networks, false)
}
}
override suspend fun getNetworkStatusesSync(
userWalletId: UserWalletId,
networks: Set<Network>,
@ -76,6 +82,22 @@ internal class DefaultNetworksRepository(
}
}
private suspend fun fetchNetworksPendingTransactionsIfCacheExpired(
userWalletId: UserWalletId,
networks: Set<Network>,
refresh: Boolean,
) {
coroutineScope {
networks
.map { network ->
async {
fetchNetworkPendingTransactionsIfCacheExpired(userWalletId, network, refresh)
}
}
.awaitAll()
}
}
private suspend fun fetchNetworkStatusIfCacheExpired(
userWalletId: UserWalletId,
network: Network,
@ -88,6 +110,20 @@ internal class DefaultNetworksRepository(
)
}
private suspend fun fetchNetworkPendingTransactionsIfCacheExpired(
userWalletId: UserWalletId,
network: Network,
refresh: Boolean,
) {
val key = getNetworksStatusesCacheKey(userWalletId, network)
cacheRegistry.invalidate(key)
cacheRegistry.invokeOnExpire(
key = key,
skipCache = refresh,
block = { fetchNetworkPendingTransactions(userWalletId, network) },
)
}
private suspend fun fetchNetworkStatus(userWalletId: UserWalletId, network: Network) {
val currencies = getCurrencies(userWalletId, network)
@ -110,6 +146,27 @@ internal class DefaultNetworksRepository(
networksStatusesStore.store(userWalletId, networkStatus)
}
private suspend fun fetchNetworkPendingTransactions(userWalletId: UserWalletId, network: Network) {
val currencies = getCurrencies(userWalletId, network)
val result = walletManagersFacade.updatePendingTransactions(
userWalletId = userWalletId,
network = network,
)
withContext(NonCancellable) {
invalidateCacheKeyIfNeeded(userWalletId, network, result)
}
val networkStatus = networkStatusFactory.createNetworkStatus(
network = network,
result = result,
currencies = currencies.toSet(),
)
networksStatusesStore.store(userWalletId, networkStatus)
}
private suspend fun getCurrencies(userWalletId: UserWalletId, network: Network): Sequence<CryptoCurrency> {
val userWallet = requireNotNull(userWalletsStore.getSyncOrNull(userWalletId)) {
"Unable to find user wallet with provided ID: $userWalletId"