mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-07-22 08:13:14 +02:00
Creates a Loading state to avoid requesting NIP-11 documents twice.
This commit is contained in:
@ -33,14 +33,23 @@ import okhttp3.Response
|
|||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
object Nip11CachedRetriever {
|
object Nip11CachedRetriever {
|
||||||
open class RetrieveResult(val time: Long)
|
open class RetrieveResult(
|
||||||
|
val time: Long,
|
||||||
|
)
|
||||||
|
|
||||||
class RetrieveResultError(val error: Nip11Retriever.ErrorCode, val msg: String? = null) : RetrieveResult(TimeUtils.now())
|
class RetrieveResultError(
|
||||||
|
val error: Nip11Retriever.ErrorCode,
|
||||||
|
val msg: String? = null,
|
||||||
|
) : RetrieveResult(TimeUtils.now())
|
||||||
|
|
||||||
class RetrieveResultSuccess(val data: Nip11RelayInformation) : RetrieveResult(TimeUtils.now())
|
class RetrieveResultSuccess(
|
||||||
|
val data: Nip11RelayInformation,
|
||||||
|
) : RetrieveResult(TimeUtils.now())
|
||||||
|
|
||||||
val relayInformationDocumentCache = LruCache<String, RetrieveResult?>(100)
|
class RetrieveResultLoading : RetrieveResult(TimeUtils.now())
|
||||||
val retriever = Nip11Retriever()
|
|
||||||
|
private val relayInformationDocumentCache = LruCache<String, RetrieveResult?>(100)
|
||||||
|
private val retriever = Nip11Retriever()
|
||||||
|
|
||||||
fun getFromCache(dirtyUrl: String): Nip11RelayInformation? {
|
fun getFromCache(dirtyUrl: String): Nip11RelayInformation? {
|
||||||
val result = relayInformationDocumentCache.get(RelayUrlFormatter.getHttpsUrl(dirtyUrl)) ?: return null
|
val result = relayInformationDocumentCache.get(RelayUrlFormatter.getHttpsUrl(dirtyUrl)) ?: return null
|
||||||
@ -53,45 +62,53 @@ object Nip11CachedRetriever {
|
|||||||
onInfo: (Nip11RelayInformation) -> Unit,
|
onInfo: (Nip11RelayInformation) -> Unit,
|
||||||
onError: (String, Nip11Retriever.ErrorCode, String?) -> Unit,
|
onError: (String, Nip11Retriever.ErrorCode, String?) -> Unit,
|
||||||
) {
|
) {
|
||||||
|
checkNotInMainThread()
|
||||||
val url = RelayUrlFormatter.getHttpsUrl(dirtyUrl)
|
val url = RelayUrlFormatter.getHttpsUrl(dirtyUrl)
|
||||||
val doc = relayInformationDocumentCache.get(url)
|
val doc = relayInformationDocumentCache.get(url)
|
||||||
|
|
||||||
if (doc != null) {
|
if (doc != null) {
|
||||||
if (doc is RetrieveResultSuccess) {
|
if (doc is RetrieveResultSuccess) {
|
||||||
onInfo(doc.data)
|
onInfo(doc.data)
|
||||||
|
} else if (doc is RetrieveResultLoading) {
|
||||||
|
if (TimeUtils.now() - doc.time < TimeUtils.ONE_MINUTE) {
|
||||||
|
// just wait.
|
||||||
|
} else {
|
||||||
|
retrieve(url, dirtyUrl, onInfo, onError)
|
||||||
|
}
|
||||||
} else if (doc is RetrieveResultError) {
|
} else if (doc is RetrieveResultError) {
|
||||||
if (TimeUtils.now() - doc.time < TimeUtils.ONE_HOUR) {
|
if (TimeUtils.now() - doc.time < TimeUtils.ONE_HOUR) {
|
||||||
onError(dirtyUrl, doc.error, null)
|
onError(dirtyUrl, doc.error, null)
|
||||||
} else {
|
} else {
|
||||||
retriever.loadRelayInfo(
|
retrieve(url, dirtyUrl, onInfo, onError)
|
||||||
url = url,
|
|
||||||
dirtyUrl = dirtyUrl,
|
|
||||||
onInfo = {
|
|
||||||
relayInformationDocumentCache.put(url, RetrieveResultSuccess(it))
|
|
||||||
onInfo(it)
|
|
||||||
},
|
|
||||||
onError = { dirtyUrl, code, errorMsg ->
|
|
||||||
relayInformationDocumentCache.put(url, RetrieveResultError(code, errorMsg))
|
|
||||||
onError(url, code, errorMsg)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
retriever.loadRelayInfo(
|
retrieve(url, dirtyUrl, onInfo, onError)
|
||||||
url = url,
|
|
||||||
dirtyUrl = dirtyUrl,
|
|
||||||
onInfo = {
|
|
||||||
relayInformationDocumentCache.put(url, RetrieveResultSuccess(it))
|
|
||||||
onInfo(it)
|
|
||||||
},
|
|
||||||
onError = { dirtyUrl, code, errorMsg ->
|
|
||||||
relayInformationDocumentCache.put(url, RetrieveResultError(code, errorMsg))
|
|
||||||
onError(url, code, errorMsg)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun retrieve(
|
||||||
|
url: String,
|
||||||
|
dirtyUrl: String,
|
||||||
|
onInfo: (Nip11RelayInformation) -> Unit,
|
||||||
|
onError: (String, Nip11Retriever.ErrorCode, String?) -> Unit,
|
||||||
|
) {
|
||||||
|
relayInformationDocumentCache.put(url, RetrieveResultLoading())
|
||||||
|
retriever.loadRelayInfo(
|
||||||
|
url = url,
|
||||||
|
dirtyUrl = dirtyUrl,
|
||||||
|
onInfo = {
|
||||||
|
checkNotInMainThread()
|
||||||
|
relayInformationDocumentCache.put(url, RetrieveResultSuccess(it))
|
||||||
|
onInfo(it)
|
||||||
|
},
|
||||||
|
onError = { dirtyUrl, code, errorMsg ->
|
||||||
|
checkNotInMainThread()
|
||||||
|
relayInformationDocumentCache.put(url, RetrieveResultError(code, errorMsg))
|
||||||
|
onError(url, code, errorMsg)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Nip11Retriever {
|
class Nip11Retriever {
|
||||||
@ -111,9 +128,14 @@ class Nip11Retriever {
|
|||||||
checkNotInMainThread()
|
checkNotInMainThread()
|
||||||
try {
|
try {
|
||||||
val request: Request =
|
val request: Request =
|
||||||
Request.Builder().header("Accept", "application/nostr+json").url(url).build()
|
Request
|
||||||
|
.Builder()
|
||||||
|
.header("Accept", "application/nostr+json")
|
||||||
|
.url(url)
|
||||||
|
.build()
|
||||||
|
|
||||||
HttpClientManager.getHttpClientForUrl(dirtyUrl)
|
HttpClientManager
|
||||||
|
.getHttpClientForUrl(dirtyUrl)
|
||||||
.newCall(request)
|
.newCall(request)
|
||||||
.enqueue(
|
.enqueue(
|
||||||
object : Callback {
|
object : Callback {
|
||||||
|
Reference in New Issue
Block a user