Logging failures to reduce the amount of network calls,

This commit is contained in:
Vitor Pamplona
2023-01-24 23:09:03 -03:00
parent f931e8dfa9
commit ca9f94eaac

View File

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