Skip to content

Commit

Permalink
Moving URL Preview fetcher to thread + running over a given feed list.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Jan 18, 2023
1 parent 7d18f36 commit ff529a9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ object LocalCache {
it.addReply(note)
}

UrlCachedPreviewer.preloadPreviewsFor(note)

refreshObservers()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ object NostrSingleEventDataSource: NostrDataSource<Note>("SingleEventFeed") {

fun add(eventId: String) {
eventsToWatch = eventsToWatch.plus(eventId)
println("AAA: Event Watching ${eventsToWatch.size}")
resetFilters()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ object NostrSingleUserDataSource: NostrDataSource<Note>("SingleUserFeed") {

fun add(userId: String) {
usersToWatch = usersToWatch.plus(userId)
println("AAA: User Watching ${usersToWatch.size}")
resetFilters()
}

Expand Down

0 comments on commit ff529a9

Please sign in to comment.