Makes sure subscriptions are closed on the NostrClient utility methods

This commit is contained in:
Vitor Pamplona
2026-03-22 15:18:48 -04:00
parent 439b392aa0
commit 37f6a7b133
3 changed files with 44 additions and 36 deletions
@@ -65,15 +65,18 @@ suspend fun INostrClient.queryCountSuspend(
subscribe(listener)
val result =
try {
queryCount(subId = subId, filters = mapOf(relay to listOf(filter)))
val result =
withTimeoutOrNull(timeoutMs) {
resultChannel.receive()
}
} finally {
close(subId)
unsubscribe(listener)
}
resultChannel.close()
return result
@@ -98,6 +98,8 @@ suspend fun INostrClient.sendAndWaitForResponseDetailed(
}
}
val receivedResults =
try {
subscribe(subscription)
// subscribe before sending the result.
@@ -126,9 +128,10 @@ suspend fun INostrClient.sendAndWaitForResponseDetailed(
result
}
val receivedResults = resultSubscription.await()
resultSubscription.await()
} finally {
unsubscribe(subscription)
}
// Clean up the channel
resultChannel.close()
@@ -104,14 +104,16 @@ suspend fun INostrClient.downloadFirstEvent(
}
}
val result =
try {
openReqSubscription(subscriptionId, filters, listener)
val result =
withTimeoutOrNull(30000) {
resultChannel.receive()
}
} finally {
close(subscriptionId)
}
resultChannel.close()