Moving URL Preview fetcher to thread + running over a given feed list.

This commit is contained in:
Vitor Pamplona 2023-01-18 12:21:00 -05:00
parent 7d18f36e3e
commit ff529a920e
4 changed files with 26 additions and 5 deletions

@ -119,8 +119,6 @@ object LocalCache {
it.addReply(note)
}
UrlCachedPreviewer.preloadPreviewsFor(note)
refreshObservers()
}

@ -1,6 +1,8 @@
package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.UrlCachedPreviewer
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelHideMessageEvent
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
@ -11,6 +13,12 @@ import com.vitorpamplona.amethyst.service.model.RepostEvent
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.service.relays.Relay
import java.util.Collections
import kotlin.time.ExperimentalTime
import kotlin.time.measureTimedValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import nostr.postr.events.ContactListEvent
import nostr.postr.events.DeletionEvent
import nostr.postr.events.Event
@ -104,8 +112,25 @@ abstract class NostrDataSource<T>(val debugName: String) {
}
}
@OptIn(ExperimentalTime::class)
fun loadTop(): List<T> {
return feed().take(100)
val returningList = feed().take(100)
// prepare previews
val scope = CoroutineScope(Job() + Dispatchers.Main)
scope.launch {
loadPreviews(returningList)
}
return returningList
}
suspend fun loadPreviews(list: List<T>) {
list.forEach {
if (it is Note) {
UrlCachedPreviewer.preloadPreviewsFor(it)
}
}
}
fun requestNewChannel(): Channel {

@ -70,7 +70,6 @@ object NostrSingleEventDataSource: NostrDataSource<Note>("SingleEventFeed") {
fun add(eventId: String) {
eventsToWatch = eventsToWatch.plus(eventId)
println("AAA: Event Watching ${eventsToWatch.size}")
resetFilters()
}

@ -35,7 +35,6 @@ object NostrSingleUserDataSource: NostrDataSource<Note>("SingleUserFeed") {
fun add(userId: String) {
usersToWatch = usersToWatch.plus(userId)
println("AAA: User Watching ${usersToWatch.size}")
resetFilters()
}