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

View File

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

View File

@ -1,6 +1,8 @@
package com.vitorpamplona.amethyst.service package com.vitorpamplona.amethyst.service
import com.vitorpamplona.amethyst.model.LocalCache 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.ChannelCreateEvent
import com.vitorpamplona.amethyst.service.model.ChannelHideMessageEvent import com.vitorpamplona.amethyst.service.model.ChannelHideMessageEvent
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent 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.Client
import com.vitorpamplona.amethyst.service.relays.Relay import com.vitorpamplona.amethyst.service.relays.Relay
import java.util.Collections 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.ContactListEvent
import nostr.postr.events.DeletionEvent import nostr.postr.events.DeletionEvent
import nostr.postr.events.Event import nostr.postr.events.Event
@ -104,8 +112,25 @@ abstract class NostrDataSource<T>(val debugName: String) {
} }
} }
@OptIn(ExperimentalTime::class)
fun loadTop(): List<T> { 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 { fun requestNewChannel(): Channel {

View File

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

View File

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