From b236938ef5350145a64ae9ec51a677cd544f989c Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 2 Jul 2025 08:59:03 -0400 Subject: [PATCH] Simplify the unregistering instead of relying on errors and other states that might come from separate filters to the same relay --- .../NostrClientSingleDownloadExt.kt | 56 ++++--------------- 1 file changed, 12 insertions(+), 44 deletions(-) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/acessories/NostrClientSingleDownloadExt.kt b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/acessories/NostrClientSingleDownloadExt.kt index cd2645eed..29fff8941 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/acessories/NostrClientSingleDownloadExt.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/nip01Core/relay/client/acessories/NostrClientSingleDownloadExt.kt @@ -23,17 +23,20 @@ package com.vitorpamplona.quartz.nip01Core.relay.client.acessories import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.IRelayClientListener -import com.vitorpamplona.quartz.nip01Core.relay.client.listeners.RelayState import com.vitorpamplona.quartz.nip01Core.relay.client.pool.RelayBasedFilter import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient import com.vitorpamplona.quartz.nip01Core.relay.client.single.newSubId +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch fun NostrClient.downloadFirstEvent( subscriptionId: String = newSubId(), filters: List = listOf(), onResponse: (Event) -> Unit, ) { - subscribe( + val listener = object : IRelayClientListener { override fun onEvent( relay: IRelayClient, @@ -49,49 +52,14 @@ fun NostrClient.downloadFirstEvent( onResponse(event) } } + } - override fun onClosed( - relay: IRelayClient, - subId: String, - message: String, - ) { - unsubscribe(this) - close(subscriptionId) - super.onClosed(relay, subId, message) - } - - override fun onEOSE( - relay: IRelayClient, - subId: String, - arrivalTime: Long, - ) { - unsubscribe(this) - close(subscriptionId) - super.onEOSE(relay, subId, arrivalTime) - } - - override fun onError( - relay: IRelayClient, - subId: String, - error: Error, - ) { - unsubscribe(this) - close(subscriptionId) - super.onError(relay, subId, error) - } - - override fun onRelayStateChange( - relay: IRelayClient, - type: RelayState, - ) { - if (type == RelayState.DISCONNECTED) { - unsubscribe(this) - close(subscriptionId) - } - super.onRelayStateChange(relay, type) - } - }, - ) + subscribe(listener) sendFilter(subscriptionId, filters) + + GlobalScope.launch(Dispatchers.IO) { + delay(30000) + unsubscribe(listener) + } }