Removes duplicated Loading of Relay Info

This commit is contained in:
Vitor Pamplona
2025-09-03 15:44:23 -04:00
parent c64e65ddb4
commit c9cdc74c91
2 changed files with 30 additions and 27 deletions

View File

@@ -56,6 +56,8 @@ object Nip11CachedRetriever {
class Empty(
data: Nip11RelayInformation,
) : RetrieveResult(data, TimeUtils.now())
fun isValid() = time > TimeUtils.oneHourAgo()
}
private val relayInformationEmptyCache = LruCache<NormalizedRelayUrl, Nip11RelayInformation>(1000)
@@ -79,16 +81,18 @@ object Nip11CachedRetriever {
fun getFromCache(relay: NormalizedRelayUrl): Nip11RelayInformation {
val result = relayInformationDocumentCache.get(relay)
if (result == null) {
// resets the clock
val empty = getEmpty(relay)
relayInformationDocumentCache.put(relay, RetrieveResult.Empty(empty))
return empty
}
return when (result) {
is RetrieveResult.Success -> return result.data
is RetrieveResult.Error -> return result.data
is RetrieveResult.Empty -> return result.data
is RetrieveResult.Loading -> return result.data
else -> {
val empty = getEmpty(relay)
relayInformationDocumentCache.put(relay, RetrieveResult.Empty(empty))
empty
}
is RetrieveResult.Success -> result.data
is RetrieveResult.Error -> result.data
is RetrieveResult.Empty -> result.data
is RetrieveResult.Loading -> result.data
}
}
@@ -100,23 +104,23 @@ object Nip11CachedRetriever {
) {
val doc = relayInformationDocumentCache.get(relay)
if (doc != null) {
if (doc is RetrieveResult.Success) {
onInfo(doc.data)
} else if (doc is RetrieveResult.Loading) {
if (TimeUtils.now() - doc.time < TimeUtils.ONE_MINUTE) {
// just wait.
} else {
retrieve(relay, okHttpClient, onInfo, onError)
when (doc) {
is RetrieveResult.Success -> onInfo(doc.data)
is RetrieveResult.Loading -> {
if (doc.isValid()) {
// just wait.
} else {
retrieve(relay, okHttpClient, onInfo, onError)
}
}
} else if (doc is RetrieveResult.Error) {
if (TimeUtils.now() - doc.time < TimeUtils.ONE_HOUR) {
onError(relay, doc.error, null)
} else {
retrieve(relay, okHttpClient, onInfo, onError)
is RetrieveResult.Error -> {
if (doc.isValid()) {
onError(relay, doc.error, null)
} else {
retrieve(relay, okHttpClient, onInfo, onError)
}
}
} else {
// Empty
retrieve(relay, okHttpClient, onInfo, onError)
is RetrieveResult.Empty -> retrieve(relay, okHttpClient, onInfo, onError)
}
} else {
retrieve(relay, okHttpClient, onInfo, onError)
@@ -135,10 +139,12 @@ object Nip11CachedRetriever {
okHttpClient = okHttpClient,
onInfo = {
relayInformationDocumentCache.put(relay, RetrieveResult.Success(it))
relayInformationEmptyCache.remove(relay)
onInfo(it)
},
onError = { relay, code, errorMsg ->
relayInformationDocumentCache.put(relay, RetrieveResult.Error(getEmpty(relay), code, errorMsg))
relayInformationEmptyCache.remove(relay)
onError(relay, code, errorMsg)
},
)

View File

@@ -26,7 +26,6 @@ import com.vitorpamplona.amethyst.model.FeatureSetType
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
import com.vitorpamplona.amethyst.ui.navigation.routes.Route
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.publicChannels.ephemChat.header.loadRelayInfo
@Composable
fun BasicRelaySetupInfoDialog(
@@ -35,8 +34,6 @@ fun BasicRelaySetupInfoDialog(
accountViewModel: AccountViewModel,
nav: INav,
) {
val relayInfo by loadRelayInfo(item.relay, accountViewModel)
BasicRelaySetupInfoClickableRow(
item = item,
loadProfilePicture = accountViewModel.settings.showProfilePictures.value,