mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-21 22:02:02 +02:00
Adds a new mechanism to wake up all connections when movement is made in the app.
This commit is contained in:
@@ -122,8 +122,6 @@ class NostrClient(
|
||||
}
|
||||
}
|
||||
|
||||
fun allAvailableRelays() = relayPool.getAll()
|
||||
|
||||
// Reconnects all relays that may have disconnected
|
||||
fun connect() {
|
||||
isActive = true
|
||||
@@ -138,10 +136,7 @@ class NostrClient(
|
||||
@Synchronized
|
||||
fun reconnect(onlyIfChanged: Boolean = false) {
|
||||
if (onlyIfChanged) {
|
||||
relayPool.getAllNeedsToReconnect().forEach {
|
||||
it.disconnect()
|
||||
}
|
||||
relayPool.connect()
|
||||
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||
} else {
|
||||
relayPool.disconnect()
|
||||
relayPool.connect()
|
||||
@@ -236,6 +231,9 @@ class NostrClient(
|
||||
relayPool.connectIfDisconnected(relay)
|
||||
}
|
||||
}
|
||||
|
||||
// wakes up all the other relays
|
||||
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -269,6 +267,9 @@ class NostrClient(
|
||||
relayPool.connectIfDisconnected(relay)
|
||||
}
|
||||
}
|
||||
|
||||
// wakes up all the other relays
|
||||
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,6 +279,9 @@ class NostrClient(
|
||||
) {
|
||||
if (isActive) {
|
||||
relayPool.getRelay(connectedRelay)?.send(event)
|
||||
|
||||
// wakes up all the other relays
|
||||
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -288,6 +292,9 @@ class NostrClient(
|
||||
eventOutbox.markAsSending(event, relayList)
|
||||
if (isActive) {
|
||||
relayPool.send(event, relayList)
|
||||
|
||||
// wakes up all the other relays
|
||||
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -29,6 +29,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.client.single.IRelayClient
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.filters.Filter
|
||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||
import com.vitorpamplona.quartz.utils.LargeCache
|
||||
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
@@ -70,16 +71,24 @@ class RelayPool(
|
||||
|
||||
fun getRelay(url: NormalizedRelayUrl): IRelayClient? = relays.get(url)
|
||||
|
||||
fun getAll() = statusFlow.value.connected
|
||||
var lastReconnectCall = TimeUtils.now()
|
||||
|
||||
fun getAllNeedsToReconnect() = relays.filter { url, relay -> relay.needsToReconnect() }
|
||||
|
||||
fun reconnectsRelaysThatNeedTo() {
|
||||
relays.forEach { url, relay ->
|
||||
if (relay.needsToReconnect()) {
|
||||
relay.disconnect()
|
||||
relay.connect()
|
||||
fun reconnectIfNeedsToORIfItIsTime() {
|
||||
if (lastReconnectCall < TimeUtils.tenSecondsAgo()) {
|
||||
relays.forEach { url, relay ->
|
||||
if (relay.isConnected()) {
|
||||
if (relay.needsToReconnect()) {
|
||||
// network has changed, force reconnect
|
||||
relay.disconnect()
|
||||
relay.connect()
|
||||
}
|
||||
} else {
|
||||
// relay is not connected. Connect if it is time
|
||||
relay.connectAndSyncFiltersIfDisconnected()
|
||||
}
|
||||
}
|
||||
|
||||
lastReconnectCall = TimeUtils.now()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user