Skip to content

Commit

Permalink
Suppress BluetoothExceptions during observation spin up/down
Browse files Browse the repository at this point in the history
  • Loading branch information
twyatt committed Dec 4, 2021
1 parent dc35f99 commit 0f04e36
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions core/src/commonMain/kotlin/Observation.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.juul.kable

import com.juul.kable.State.Connected
import com.juul.kable.State.Connecting.Observes
import com.juul.kable.logs.Logger
import com.juul.kable.logs.Logging
Expand Down Expand Up @@ -40,7 +39,7 @@ internal class Observation(
if (isObservationEnabled.value) {
// Ignore `NotConnectedException` to guard against potential race-condition where disconnect occurs
// immediately after checking `isObservationEnabled`.
suppressNotConnectedException {
suppressConnectionExceptions {
action()
}
}
Expand All @@ -56,7 +55,7 @@ internal class Observation(
if (isObservationEnabled.value) {
// Ignore `NotConnectedException` to guard against potential race-condition where disconnect occurs
// immediately after checking `isObservationEnabled`.
suppressNotConnectedException {
suppressConnectionExceptions {
subscribers.forEach { it() }
}
}
Expand All @@ -69,7 +68,7 @@ internal class Observation(

private suspend fun enableObservationIfNeeded() {
if (!isObservationEnabled.value && isConnected && hasSubscribers) {
suppressNotConnectedException {
suppressConnectionExceptions {
handler.startObservation(characteristic)
isObservationEnabled.value = true
}
Expand All @@ -78,24 +77,28 @@ internal class Observation(

private suspend fun disableObservationIfNeeded() {
if (isObservationEnabled.value && isConnected && !hasSubscribers) {
suppressNotConnectedException {
suppressConnectionExceptions {
handler.stopObservation(characteristic)
}
isObservationEnabled.value = false
}
}

/**
* It is assumed that observations are automatically cleared on disconnect, therefore in some situations
* [NotConnectedException]s can be ignored, as the corresponding [action] will be rendered unnecessary
* (e.g. clearing an observation is not needed if connection has been lost), or [action] will be re-attempted on
* [reconnect][onConnected].
* While spinning up or down an observation the connection may drop, resulting in an unnecessary connection related
* exception being thrown.
*
* Since it is assumed that observations are automatically cleared on disconnect, these exceptions can be ignored as
* the corresponding [action] will be rendered unnecessary (e.g. clearing an observation is not needed if connection
* has been lost, or [action] will be re-attempted on [reconnect][onConnected]).
*/
private inline fun suppressNotConnectedException(action: () -> Unit) {
private inline fun suppressConnectionExceptions(action: () -> Unit) {
try {
action.invoke()
} catch (e: NotConnectedException) {
logger.verbose { message = "Suppressed failure: ${e.message}" }
} catch (e: BluetoothException) {
logger.verbose { message = "Suppressed failure: ${e.message}" }
}
}
}

0 comments on commit 0f04e36

Please sign in to comment.