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