No need to switch to IO this early in the process

This commit is contained in:
Vitor Pamplona 2024-04-02 16:08:59 -04:00
parent fbf676bdb2
commit 4d7de6bc19
2 changed files with 32 additions and 40 deletions

View File

@ -26,8 +26,6 @@ import com.vitorpamplona.amethyst.service.previews.BahaUrlPreview
import com.vitorpamplona.amethyst.service.previews.IUrlPreviewCallback import com.vitorpamplona.amethyst.service.previews.IUrlPreviewCallback
import com.vitorpamplona.amethyst.service.previews.UrlInfoItem import com.vitorpamplona.amethyst.service.previews.UrlInfoItem
import com.vitorpamplona.amethyst.ui.components.UrlPreviewState import com.vitorpamplona.amethyst.ui.components.UrlPreviewState
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@Stable @Stable
object UrlCachedPreviewer { object UrlCachedPreviewer {
@ -37,46 +35,44 @@ object UrlCachedPreviewer {
suspend fun previewInfo( suspend fun previewInfo(
url: String, url: String,
onReady: suspend (UrlPreviewState) -> Unit, onReady: suspend (UrlPreviewState) -> Unit,
) = withContext(Dispatchers.IO) { ) {
cache[url]?.let { cache[url]?.let {
onReady(it) onReady(it)
return@withContext return
} }
BahaUrlPreview( BahaUrlPreview(
url, url,
object : IUrlPreviewCallback { object : IUrlPreviewCallback {
override suspend fun onComplete(urlInfo: UrlInfoItem) = override suspend fun onComplete(urlInfo: UrlInfoItem) {
withContext(Dispatchers.IO) { cache[url]?.let {
cache[url]?.let { if (it is UrlPreviewState.Loaded || it is UrlPreviewState.Empty) {
if (it is UrlPreviewState.Loaded || it is UrlPreviewState.Empty) {
onReady(it)
return@withContext
}
}
val state =
if (urlInfo.fetchComplete() && urlInfo.url == url) {
UrlPreviewState.Loaded(urlInfo)
} else {
UrlPreviewState.Empty
}
cache.put(url, state)
onReady(state)
}
override suspend fun onFailed(throwable: Throwable) =
withContext(Dispatchers.IO) {
cache[url]?.let {
onReady(it) onReady(it)
return@withContext return
}
}
val state =
if (urlInfo.fetchComplete() && urlInfo.url == url) {
UrlPreviewState.Loaded(urlInfo)
} else {
UrlPreviewState.Empty
} }
val state = UrlPreviewState.Error(throwable.message ?: "Error Loading url preview") cache.put(url, state)
cache.put(url, state) onReady(state)
onReady(state) }
override suspend fun onFailed(throwable: Throwable) {
cache[url]?.let {
onReady(it)
return
} }
val state = UrlPreviewState.Error(throwable.message ?: "Error Loading url preview")
cache.put(url, state)
onReady(state)
}
}, },
) )
.fetchUrlPreview() .fetchUrlPreview()

View File

@ -21,18 +21,14 @@
package com.vitorpamplona.amethyst.service.previews package com.vitorpamplona.amethyst.service.previews
import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
class BahaUrlPreview(val url: String, var callback: IUrlPreviewCallback?) { class BahaUrlPreview(val url: String, var callback: IUrlPreviewCallback?) {
suspend fun fetchUrlPreview(timeOut: Int = 30000) = suspend fun fetchUrlPreview(timeOut: Int = 30000) =
withContext(Dispatchers.IO) { try {
try { fetch(timeOut)
fetch(timeOut) } catch (t: Throwable) {
} catch (t: Throwable) { if (t is CancellationException) throw t
if (t is CancellationException) throw t callback?.onFailed(t)
callback?.onFailed(t)
}
} }
private suspend fun fetch(timeOut: Int = 30000) { private suspend fun fetch(timeOut: Int = 30000) {