Removes the choice of Dispatchers from Quartz

This commit is contained in:
Vitor Pamplona
2025-10-21 20:15:05 -04:00
parent db1d5c7e82
commit c21b2ca60f
2 changed files with 3 additions and 8 deletions

View File

@@ -37,13 +37,11 @@ import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebsocketBuilder import com.vitorpamplona.quartz.nip01Core.relay.sockets.WebsocketBuilder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.sample
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
@@ -108,8 +106,7 @@ class NostrClient(
}.sample(300) }.sample(300)
.onEach { .onEach {
relayPool.updatePool(it) relayPool.updatePool(it)
}.flowOn(Dispatchers.IO) }.stateIn(
.stateIn(
scope, scope,
SharingStarted.Eagerly, SharingStarted.Eagerly,
activeRequests.desiredRelays.value + activeCounts.relays.value + eventOutbox.relays.value, activeRequests.desiredRelays.value + activeCounts.relays.value + eventOutbox.relays.value,
@@ -229,7 +226,7 @@ class NostrClient(
override fun renewFilters(relay: IRelayClient) { override fun renewFilters(relay: IRelayClient) {
if (isActive) { if (isActive) {
scope.launch(Dispatchers.IO) { scope.launch {
activeRequests.syncState(relay.url, relay::sendOrConnectAndSync) activeRequests.syncState(relay.url, relay::sendOrConnectAndSync)
activeCounts.syncState(relay.url, relay::sendOrConnectAndSync) activeCounts.syncState(relay.url, relay::sendOrConnectAndSync)
eventOutbox.syncState(relay.url, relay::sendOrConnectAndSync) eventOutbox.syncState(relay.url, relay::sendOrConnectAndSync)

View File

@@ -29,8 +29,6 @@ import com.vitorpamplona.quartz.nip01Core.relay.commands.toClient.OkMessage
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
import com.vitorpamplona.quartz.utils.Log import com.vitorpamplona.quartz.utils.Log
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.async import kotlinx.coroutines.async
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
@@ -95,7 +93,7 @@ suspend fun INostrClient.sendAndWaitForResponse(
val resultSubscription = val resultSubscription =
coroutineScope { coroutineScope {
val result = val result =
async(Dispatchers.IO) { async {
val receivedResults = mutableMapOf<NormalizedRelayUrl, Boolean>() val receivedResults = mutableMapOf<NormalizedRelayUrl, Boolean>()
// The withTimeout block will cancel the coroutine if the loop takes too long // The withTimeout block will cancel the coroutine if the loop takes too long
withTimeoutOrNull(timeoutInSeconds * 1000) { withTimeoutOrNull(timeoutInSeconds * 1000) {