chore: improve event syncing #29
@@ -64,7 +64,8 @@ object NostrManager {
|
||||
|
||||
val BOOTSTRAP_RELAYS = listOf(
|
||||
"wss://relay.primal.net",
|
||||
"wss://purplepag.es"
|
||||
"wss://relay.ditto.pub",
|
||||
"wss://user.kindpag.es",
|
||||
)
|
||||
|
||||
val INDEXER_RELAY = listOf(
|
||||
@@ -113,7 +114,7 @@ class Nostr {
|
||||
|
||||
suspend fun init(
|
||||
dbPath: String,
|
||||
logLevel: LogLevel = LogLevel.DEBUG
|
||||
logLevel: LogLevel = LogLevel.WARN
|
||||
) {
|
||||
try {
|
||||
if (isInitialized.value) return
|
||||
@@ -646,7 +647,7 @@ class Nostr {
|
||||
|
||||
suspend fun fetchMetadataBatch(keys: List<PublicKey>) {
|
||||
try {
|
||||
val limit = keys.size.toULong() * 4u
|
||||
val limit = keys.size.toULong() * 2u
|
||||
val opts = SubscribeAutoCloseOptions().exitPolicy(ReqExitPolicy.ExitOnEose)
|
||||
|
||||
// Construct a filter for metadata events
|
||||
@@ -655,16 +656,13 @@ class Nostr {
|
||||
.authors(keys)
|
||||
.limit(limit)
|
||||
|
||||
// Construct a target that includes all filters
|
||||
val target =
|
||||
ReqTarget.manual(
|
||||
mapOf(
|
||||
RelayUrl.parse("wss://purplepag.es") to listOf(filter),
|
||||
RelayUrl.parse("wss://relay.primal.net") to listOf(filter),
|
||||
)
|
||||
)
|
||||
// Construct request target
|
||||
val target = mutableMapOf<RelayUrl, List<Filter>>()
|
||||
NostrManager.BOOTSTRAP_RELAYS.forEach { relay ->
|
||||
target[RelayUrl.parse(relay)] = listOf(filter)
|
||||
}
|
||||
|
||||
client?.subscribe(target = target, closeOn = opts)
|
||||
client?.subscribe(target = ReqTarget.manual(target), closeOn = opts)
|
||||
} catch (e: Exception) {
|
||||
throw IllegalStateException("Failed to fetch metadata batch: ${e.message}", e)
|
||||
}
|
||||
|
||||
@@ -82,7 +82,7 @@ class NostrViewModel(
|
||||
val errorEvents = _errorEvents.receiveAsFlow()
|
||||
|
||||
private val _metadataStore = mutableMapOf<PublicKey, MutableStateFlow<Metadata?>>()
|
||||
|
||||
|
||||
private val metadataRequestChannel = Channel<PublicKey>(Channel.UNLIMITED)
|
||||
|
||||
private val seenPublicKeys = mutableSetOf<PublicKey>()
|
||||
@@ -213,16 +213,20 @@ class NostrViewModel(
|
||||
val timeout = 500L // 500ms timeout for batching
|
||||
|
||||
while (true) {
|
||||
// Get the first pubkey
|
||||
val firstKey = metadataRequestChannel.receive()
|
||||
batch.add(firstKey)
|
||||
|
||||
// Get current time
|
||||
val lastFlushTime = Clock.System.now().toEpochMilliseconds()
|
||||
|
||||
while (batch.isNotEmpty()) {
|
||||
// Get the next pubkey
|
||||
val nextKey = withTimeoutOrNull(timeout.milliseconds) {
|
||||
metadataRequestChannel.receive()
|
||||
}
|
||||
|
||||
// Only add the key if it's not null
|
||||
// Only add the pubkey if it's not null
|
||||
if (nextKey != null) batch.add(nextKey)
|
||||
|
||||
// Get current time
|
||||
|
||||
Reference in New Issue
Block a user