Simplify connection details listener
This commit is contained in:
parent
c596af5f8e
commit
c90e83f246
3 changed files with 7 additions and 12 deletions
|
|
@ -25,13 +25,12 @@ class JsonConnection(
|
|||
private val api: ApiService,
|
||||
private val user: User?,
|
||||
private val sinceId: String?,
|
||||
private val connectionDetailsListener: (Collection<Long>, ConnectionState, Throwable?, Long) -> Unit,
|
||||
private val connectionDetailsListener: (String, ConnectionState, Throwable?, Long) -> Unit,
|
||||
private val notificationListener: (Subscription, Notification) -> Unit,
|
||||
private val serviceActive: () -> Boolean
|
||||
) : Connection {
|
||||
private val baseUrl = connectionId.baseUrl
|
||||
private val topicsToSubscriptionIds = connectionId.topicsToSubscriptionIds
|
||||
private val subscriptionIds = topicsToSubscriptionIds.values
|
||||
private val topicsStr = topicsToSubscriptionIds.keys.joinToString(separator = ",")
|
||||
private val url = topicUrl(baseUrl, topicsStr)
|
||||
private val parser = NotificationParser()
|
||||
|
|
@ -54,7 +53,7 @@ class JsonConnection(
|
|||
if (errorCount > 0) {
|
||||
errorCount = 0
|
||||
}
|
||||
connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTED, null, 0L)
|
||||
connectionDetailsListener(baseUrl, ConnectionState.CONNECTED, null, 0L)
|
||||
|
||||
// Blocking read loop: reads JSON lines until connection closes or is cancelled
|
||||
while (isActive && serviceActive() && !source.exhausted()) {
|
||||
|
|
@ -81,7 +80,7 @@ class JsonConnection(
|
|||
val retrySeconds = RETRY_SECONDS.getOrNull(errorCount-1) ?: RETRY_SECONDS.last()
|
||||
val nextRetryTime = System.currentTimeMillis() + (retrySeconds * 1000L)
|
||||
val error = if (isConnectionBrokenException(e)) null else e
|
||||
connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTING, error, nextRetryTime)
|
||||
connectionDetailsListener(baseUrl, ConnectionState.CONNECTING, error, nextRetryTime)
|
||||
Log.w(TAG, "[$url] Retrying connection in ${retrySeconds}s ...")
|
||||
delay(retrySeconds * 1000L)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -307,10 +307,7 @@ class SubscriberService : Service() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun onConnectionDetailsChanged(subscriptionIds: Collection<Long>, state: ConnectionState, throwable: Throwable?, nextRetryTime: Long) {
|
||||
val subscriptionId = subscriptionIds.firstOrNull() ?: return
|
||||
val subscription = repository.getSubscription(subscriptionId) ?: return
|
||||
val baseUrl = subscription.baseUrl
|
||||
private fun onConnectionDetailsChanged(baseUrl: String, state: ConnectionState, throwable: Throwable?, nextRetryTime: Long) {
|
||||
repository.updateConnectionDetails(baseUrl, state, throwable, nextRetryTime)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class WsConnection(
|
|||
private val user: User?,
|
||||
private val customHeaders: List<CustomHeader>,
|
||||
private val sinceId: String?,
|
||||
private val connectionDetailsListener: (Collection<Long>, ConnectionState, Throwable?, Long) -> Unit,
|
||||
private val connectionDetailsListener: (String, ConnectionState, Throwable?, Long) -> Unit,
|
||||
private val notificationListener: (Subscription, Notification) -> Unit,
|
||||
private val alarmManager: AlarmManager
|
||||
) : Connection {
|
||||
|
|
@ -55,7 +55,6 @@ class WsConnection(
|
|||
private val since = AtomicReference<String?>(sinceId)
|
||||
private val baseUrl = connectionId.baseUrl
|
||||
private val topicsToSubscriptionIds = connectionId.topicsToSubscriptionIds
|
||||
private val subscriptionIds = topicsToSubscriptionIds.values
|
||||
private val topicsStr = topicsToSubscriptionIds.keys.joinToString(separator = ",")
|
||||
private val shortUrl = topicShortUrl(baseUrl, topicsStr)
|
||||
|
||||
|
|
@ -141,7 +140,7 @@ class WsConnection(
|
|||
if (errorCount > 0) {
|
||||
errorCount = 0
|
||||
}
|
||||
connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTED, null, 0L)
|
||||
connectionDetailsListener(baseUrl, ConnectionState.CONNECTED, null, 0L)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -196,7 +195,7 @@ class WsConnection(
|
|||
isResponseCode(response, 101) -> WebSocketNotSupportedException(response!!.code, response.message, t)
|
||||
else -> t
|
||||
}
|
||||
connectionDetailsListener(subscriptionIds, ConnectionState.CONNECTING, error, nextRetryTime)
|
||||
connectionDetailsListener(baseUrl, ConnectionState.CONNECTING, error, nextRetryTime)
|
||||
scheduleReconnect(retrySeconds)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue