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,21 +35,20 @@ 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) onReady(it)
return@withContext return
} }
} }
@ -66,11 +63,10 @@ object UrlCachedPreviewer {
onReady(state) onReady(state)
} }
override suspend fun onFailed(throwable: Throwable) = override suspend fun onFailed(throwable: Throwable) {
withContext(Dispatchers.IO) {
cache[url]?.let { cache[url]?.let {
onReady(it) onReady(it)
return@withContext return
} }
val state = UrlPreviewState.Error(throwable.message ?: "Error Loading url preview") val state = UrlPreviewState.Error(throwable.message ?: "Error Loading url preview")

View File

@ -21,19 +21,15 @@
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) {
callback?.onComplete(getDocument(url, timeOut)) callback?.onComplete(getDocument(url, timeOut))