Preparing to refactor the Relay classes.

This commit is contained in:
Vitor Pamplona
2024-03-15 19:28:59 -04:00
parent 1b9742597a
commit c74176684f
4 changed files with 40 additions and 28 deletions
@@ -98,7 +98,7 @@ object Client : RelayPool.Listener {
checkNotInMainThread() checkNotInMainThread()
subscriptions = subscriptions + Pair(subscriptionId, filters) subscriptions = subscriptions + Pair(subscriptionId, filters)
RelayPool.sendFilter(subscriptionId) RelayPool.sendFilter(subscriptionId, filters)
} }
fun sendFilterOnlyIfDisconnected( fun sendFilterOnlyIfDisconnected(
@@ -134,7 +134,7 @@ object Client : RelayPool.Listener {
newSporadicRelay( newSporadicRelay(
relay, relay,
feedTypes, feedTypes,
onConnected = { relay -> relay.send(signedEvent) }, onConnected = { myRelay -> myRelay.send(signedEvent) },
onDone = onDone, onDone = onDone,
) )
} }
@@ -152,7 +152,9 @@ object Client : RelayPool.Listener {
RelayPool.addRelay(relay) RelayPool.addRelay(relay)
relay.connectAndRun { relay.connectAndRun {
allSubscriptions().forEach { relay.sendFilter(requestId = it) } allSubscriptions().forEach {
relay.sendFilter(it.key, it.value)
}
onConnected(relay) onConnected(relay)
@@ -264,8 +266,8 @@ object Client : RelayPool.Listener {
listeners = listeners.minus(listener) listeners = listeners.minus(listener)
} }
fun allSubscriptions(): Set<String> { fun allSubscriptions(): Map<String, List<TypedFilter>> {
return subscriptions.keys return subscriptions
} }
fun getSubscriptionFilters(subId: String): List<TypedFilter> { fun getSubscriptionFilters(subId: String): List<TypedFilter> {
@@ -344,16 +344,15 @@ class Relay(
afterEOSEPerSubscription = LinkedHashMap(afterEOSEPerSubscription.size) afterEOSEPerSubscription = LinkedHashMap(afterEOSEPerSubscription.size)
} }
fun sendFilter(requestId: String) { fun sendFilter(
requestId: String,
filters: List<TypedFilter>,
) {
checkNotInMainThread() checkNotInMainThread()
if (read) { if (read) {
if (isConnected()) { if (isConnected()) {
if (isReady) { if (isReady) {
val filters =
Client.getSubscriptionFilters(requestId).filter { filter ->
activeTypes.any { it in filter.types }
}
if (filters.isNotEmpty()) { if (filters.isNotEmpty()) {
val request = val request =
filters.joinToStringLimited( filters.joinToStringLimited(
@@ -423,7 +422,14 @@ class Relay(
fun renewFilters() { fun renewFilters() {
// Force update all filters after AUTH. // Force update all filters after AUTH.
Client.allSubscriptions().forEach { sendFilter(requestId = it) } Client.allSubscriptions().forEach {
val filters =
it.value.filter { filter ->
activeTypes.any { it in filter.types }
}
sendFilter(requestId = it.key, filters)
}
} }
fun send(signedEvent: EventInterface) { fun send(signedEvent: EventInterface) {
@@ -77,8 +77,11 @@ object RelayPool : Relay.Listener {
relays.forEach { it.connect() } relays.forEach { it.connect() }
} }
fun sendFilter(subscriptionId: String) { fun sendFilter(
relays.forEach { it.sendFilter(subscriptionId) } subscriptionId: String,
filters: List<TypedFilter>,
) {
relays.forEach { it.sendFilter(subscriptionId, filters) }
} }
fun connectAndSendFiltersIfDisconnected() { fun connectAndSendFiltersIfDisconnected() {
@@ -356,7 +356,9 @@ private fun RenderRoomTopBar(
RoomNameOnlyDisplay( RoomNameOnlyDisplay(
room, room,
Modifier.padding(start = 10.dp).weight(1f), Modifier
.padding(start = 10.dp)
.weight(1f),
fontWeight = FontWeight.Normal, fontWeight = FontWeight.Normal,
accountViewModel.userProfile(), accountViewModel.userProfile(),
) )
@@ -504,7 +506,10 @@ fun GenericMainTopBar(
) { ) {
Box(Modifier) { Box(Modifier) {
Column( Column(
modifier = Modifier.fillMaxWidth().fillMaxHeight(), modifier =
Modifier
.fillMaxWidth()
.fillMaxHeight(),
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center, verticalArrangement = Arrangement.Center,
) { ) {
@@ -784,12 +789,14 @@ fun SimpleTextSpinner(
} }
Box( Box(
modifier = modifier =
Modifier.matchParentSize().clickable( Modifier
interactionSource = interactionSource, .matchParentSize()
indication = null, .clickable(
) { interactionSource = interactionSource,
optionsShowing = true indication = null,
}, ) {
optionsShowing = true
},
) )
} }
@@ -922,13 +929,7 @@ fun AmethystClickableIcon() {
fun debugState(context: Context) { fun debugState(context: Context) {
Client.allSubscriptions() Client.allSubscriptions()
.map { .forEach { Log.d("STATE DUMP", "${it.key} ${it.value.joinToString { it.filter.toJson() }}") }
"$it ${
Client.getSubscriptionFilters(it)
.joinToString { it.filter.toJson() }
}"
}
.forEach { Log.d("STATE DUMP", it) }
NostrAccountDataSource.printCounter() NostrAccountDataSource.printCounter()
NostrChannelDataSource.printCounter() NostrChannelDataSource.printCounter()