mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-28 22:43:16 +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
|
// Reconnects all relays that may have disconnected
|
||||||
fun connect() {
|
fun connect() {
|
||||||
isActive = true
|
isActive = true
|
||||||
@@ -138,10 +136,7 @@ class NostrClient(
|
|||||||
@Synchronized
|
@Synchronized
|
||||||
fun reconnect(onlyIfChanged: Boolean = false) {
|
fun reconnect(onlyIfChanged: Boolean = false) {
|
||||||
if (onlyIfChanged) {
|
if (onlyIfChanged) {
|
||||||
relayPool.getAllNeedsToReconnect().forEach {
|
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||||
it.disconnect()
|
|
||||||
}
|
|
||||||
relayPool.connect()
|
|
||||||
} else {
|
} else {
|
||||||
relayPool.disconnect()
|
relayPool.disconnect()
|
||||||
relayPool.connect()
|
relayPool.connect()
|
||||||
@@ -236,6 +231,9 @@ class NostrClient(
|
|||||||
relayPool.connectIfDisconnected(relay)
|
relayPool.connectIfDisconnected(relay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wakes up all the other relays
|
||||||
|
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,6 +267,9 @@ class NostrClient(
|
|||||||
relayPool.connectIfDisconnected(relay)
|
relayPool.connectIfDisconnected(relay)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wakes up all the other relays
|
||||||
|
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -278,6 +279,9 @@ class NostrClient(
|
|||||||
) {
|
) {
|
||||||
if (isActive) {
|
if (isActive) {
|
||||||
relayPool.getRelay(connectedRelay)?.send(event)
|
relayPool.getRelay(connectedRelay)?.send(event)
|
||||||
|
|
||||||
|
// wakes up all the other relays
|
||||||
|
relayPool.reconnectIfNeedsToORIfItIsTime()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,6 +292,9 @@ class NostrClient(
|
|||||||
eventOutbox.markAsSending(event, relayList)
|
eventOutbox.markAsSending(event, relayList)
|
||||||
if (isActive) {
|
if (isActive) {
|
||||||
relayPool.send(event, relayList)
|
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.filters.Filter
|
||||||
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
|
||||||
import com.vitorpamplona.quartz.utils.LargeCache
|
import com.vitorpamplona.quartz.utils.LargeCache
|
||||||
|
import com.vitorpamplona.quartz.utils.TimeUtils
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
@@ -70,16 +71,24 @@ class RelayPool(
|
|||||||
|
|
||||||
fun getRelay(url: NormalizedRelayUrl): IRelayClient? = relays.get(url)
|
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 reconnectIfNeedsToORIfItIsTime() {
|
||||||
|
if (lastReconnectCall < TimeUtils.tenSecondsAgo()) {
|
||||||
fun reconnectsRelaysThatNeedTo() {
|
relays.forEach { url, relay ->
|
||||||
relays.forEach { url, relay ->
|
if (relay.isConnected()) {
|
||||||
if (relay.needsToReconnect()) {
|
if (relay.needsToReconnect()) {
|
||||||
relay.disconnect()
|
// network has changed, force reconnect
|
||||||
relay.connect()
|
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