mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-25 23:56:20 +02:00
Makes sure the NostrClient stays off in the background
This commit is contained in:
@@ -37,6 +37,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.DelicateCoroutinesApi
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.NonCancellable.isActive
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.flowOn
|
||||
@@ -80,6 +81,11 @@ class NostrClient(
|
||||
|
||||
private var listeners = setOf<IRelayClientListener>()
|
||||
|
||||
// controls the state of the client in such a way that if it is active
|
||||
// new filters will be sent to the relays and a potential reconnect can
|
||||
// be triggered.
|
||||
private var isActive = false
|
||||
|
||||
/**
|
||||
* Whatches for any changes in the relay list from subscriptions or outbox
|
||||
* and updates the relayPool as needed.
|
||||
@@ -117,10 +123,15 @@ class NostrClient(
|
||||
fun allAvailableRelays() = relayPool.getAll()
|
||||
|
||||
// Reconnects all relays that may have disconnected
|
||||
fun connect() = relayPool.connect()
|
||||
fun connect() {
|
||||
isActive = true
|
||||
relayPool.connect()
|
||||
}
|
||||
|
||||
// Reconnects all relays that may have disconnected
|
||||
fun disconnect() = relayPool.disconnect()
|
||||
fun disconnect() {
|
||||
isActive = false
|
||||
relayPool.disconnect()
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun reconnect(onlyIfChanged: Boolean = false) {
|
||||
@@ -200,6 +211,7 @@ class NostrClient(
|
||||
val oldFilters = activeRequests.getSubscriptionFiltersOrNull(subId) ?: emptyMap()
|
||||
activeRequests.addOrUpdate(subId, filters)
|
||||
|
||||
if (isActive) {
|
||||
val allRelays = filters.keys + oldFilters.keys
|
||||
|
||||
allRelays.forEach { relay ->
|
||||
@@ -223,6 +235,7 @@ class NostrClient(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun sendCount(
|
||||
subId: String = newSubId(),
|
||||
@@ -231,6 +244,7 @@ class NostrClient(
|
||||
val oldFilters = activeCounts.getSubscriptionFiltersOrNull(subId) ?: emptyMap()
|
||||
activeCounts.addOrUpdate(subId, filters)
|
||||
|
||||
if (isActive) {
|
||||
val allRelays = filters.keys + oldFilters.keys
|
||||
|
||||
allRelays.forEach { relay ->
|
||||
@@ -254,21 +268,26 @@ class NostrClient(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun sendIfExists(
|
||||
event: Event,
|
||||
connectedRelay: NormalizedRelayUrl,
|
||||
) {
|
||||
if (isActive) {
|
||||
relayPool.getRelay(connectedRelay)?.send(event)
|
||||
}
|
||||
}
|
||||
|
||||
fun send(
|
||||
event: Event,
|
||||
relayList: Set<NormalizedRelayUrl>,
|
||||
) {
|
||||
eventOutbox.markAsSending(event, relayList)
|
||||
if (isActive) {
|
||||
relayPool.send(event, relayList)
|
||||
}
|
||||
}
|
||||
|
||||
fun close(subscriptionId: String) {
|
||||
activeRequests.remove(subscriptionId)
|
||||
|
@@ -442,7 +442,7 @@ open class BasicRelayClient(
|
||||
override fun close(subscriptionId: String) {
|
||||
// avoids sending closes for subscriptions that were never sent to this relay.
|
||||
if (afterEOSEPerSubscription.containsKey(subscriptionId)) {
|
||||
writeToSocket(CloseCmd.Companion.toJson(subscriptionId))
|
||||
writeToSocket(CloseCmd.toJson(subscriptionId))
|
||||
afterEOSEPerSubscription[subscriptionId] = false
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user