Logging failures to reduce the amount of network calls,

pull/37/head
Vitor Pamplona 2023-01-24 23:09:03 -03:00
rodzic f931e8dfa9
commit ca9f94eaac
1 zmienionych plików z 10 dodań i 1 usunięć

Wyświetl plik

@ -15,6 +15,7 @@ import kotlinx.coroutines.launch
object UrlCachedPreviewer {
val cache = ConcurrentHashMap<String, UrlInfoItem>()
val failures = ConcurrentHashMap<String, Throwable>()
fun previewInfo(url: String, callback: IUrlPreviewCallback? = null) {
cache[url]?.let {
@ -22,6 +23,11 @@ object UrlCachedPreviewer {
return
}
failures[url]?.let {
callback?.onFailed(it)
return
}
val scope = CoroutineScope(Job() + Dispatchers.IO)
scope.launch {
BahaUrlPreview(url, object : IUrlPreviewCallback {
@ -31,6 +37,7 @@ object UrlCachedPreviewer {
}
override fun onFailed(throwable: Throwable) {
failures.put(url, throwable)
callback?.onFailed(throwable)
}
}).fetchUrlPreview()
@ -53,8 +60,10 @@ object UrlCachedPreviewer {
// Preload Images? Isn't this too heavy?
} else if (videoExtension.matcher(removedParamsFromUrl).matches()) {
// Do nothing for now.
} else {
} else if (isValidURL(removedParamsFromUrl)) {
previewInfo(it)
} else {
previewInfo("https://${it}")
}
}
}