diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt index 2fa082164..a314a0dd8 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/NostrClient.kt @@ -41,6 +41,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.IO import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.combine @@ -77,12 +78,15 @@ import kotlinx.coroutines.launch */ class NostrClient( private val websocketBuilder: WebsocketBuilder, - private val scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()), + private val parentScope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()), ) : INostrClient, IRelayClientListener, AutoCloseable { private val relayPool: RelayPool = RelayPool(websocketBuilder, this) + /** Scope for all subscriptions. */ + private val scope = CoroutineScope(parentScope.coroutineContext + SupervisorJob()) + private val activeRequests: PoolRequests = PoolRequests() private val activeCounts: PoolCounts = PoolCounts() private val eventOutbox: PoolEventOutbox = PoolEventOutbox() @@ -330,5 +334,10 @@ class NostrClient( override fun close() { disconnect() + listeners = emptySet() + activeCounts.destroy() + activeRequests.destroy() + eventOutbox.destroy() + scope.cancel() } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolCounts.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolCounts.kt index c64c0a607..75b8ff0a4 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolCounts.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolCounts.kt @@ -213,4 +213,10 @@ class PoolCounts { ) { // mark as impossible to get count from this relay } + + fun destroy() { + relayState.clear() + relays.tryEmit(emptySet()) + queries.clear() + } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolEventOutbox.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolEventOutbox.kt index 8f077132a..49b0e2a16 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolEventOutbox.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolEventOutbox.kt @@ -188,4 +188,9 @@ class PoolEventOutbox { } } } + + fun destroy() { + eventOutbox = emptyMap() + relays.tryEmit(emptySet()) + } } diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolRequests.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolRequests.kt index e877ca750..0ea0c49d7 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolRequests.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/pool/PoolRequests.kt @@ -333,4 +333,11 @@ class PoolRequests { // They are the same don't do anything. } } + + fun destroy() { + relayState.clear() + desiredSubs.clear() + desiredSubListeners.clear() + desiredRelays.tryEmit(emptySet()) + } }