Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: suspend extension function remains suspended if the request is cancelled #574

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import no.nordicsemi.android.ble.data.Data
import no.nordicsemi.android.ble.exception.*
import no.nordicsemi.android.ble.response.ReadResponse
import no.nordicsemi.android.ble.response.WriteResponse
import kotlin.coroutines.cancellation.CancellationException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
Expand Down Expand Up @@ -240,12 +241,14 @@ suspend fun WaitForValueChangedRequest.suspend(): Data = suspendCancellableCoro
.invalid { continuation.resumeWithException(InvalidRequestException(this)) }
.fail { _, status ->
val exception = when (status) {
FailCallback.REASON_CANCELLED -> return@fail
FailCallback.REASON_CANCELLED -> CancellationException("Request cancelled.")
FailCallback.REASON_BLUETOOTH_DISABLED -> BluetoothDisabledException()
FailCallback.REASON_DEVICE_DISCONNECTED -> DeviceDisconnectedException()
else -> RequestFailedException(this, status)
}
continuation.resumeWithException(exception)
if (continuation.isActive) {
continuation.resumeWithException(exception)
}
}
.done { continuation.resume(data!!) }
// .then is called after both .done and .fail
Expand Down Expand Up @@ -325,12 +328,14 @@ suspend fun WaitForReadRequest.suspend(): Data = suspendCancellableCoroutine {
.invalid { continuation.resumeWithException(InvalidRequestException(this)) }
.fail { _, status ->
val exception = when (status) {
FailCallback.REASON_CANCELLED -> return@fail
FailCallback.REASON_CANCELLED -> CancellationException("Request cancelled.")
FailCallback.REASON_BLUETOOTH_DISABLED -> BluetoothDisabledException()
FailCallback.REASON_DEVICE_DISCONNECTED -> DeviceDisconnectedException()
else -> RequestFailedException(this, status)
}
continuation.resumeWithException(exception)
if (continuation.isActive) {
continuation.resumeWithException(exception)
}
}
.done { continuation.resume(data!!) }
// .then is called after both .done and .fail
Expand Down Expand Up @@ -394,12 +399,14 @@ private suspend fun TimeoutableRequest.suspendCancellable() = suspendCancellable
.invalid { continuation.resumeWithException(InvalidRequestException(this)) }
.fail { _, status ->
val exception = when (status) {
FailCallback.REASON_CANCELLED -> return@fail
FailCallback.REASON_CANCELLED -> CancellationException("Request cancelled.")
FailCallback.REASON_BLUETOOTH_DISABLED -> BluetoothDisabledException()
FailCallback.REASON_DEVICE_DISCONNECTED -> DeviceDisconnectedException()
else -> RequestFailedException(this, status)
}
continuation.resumeWithException(exception)
if (continuation.isActive) {
continuation.resumeWithException(exception)
}
}
.done { continuation.resume(Unit) }
// .then is called after both .done and .fail
Expand Down