Skip to content

Commit 4dfb1a0

Browse files
committed
[ECO-5375] Updated liveObjectsAdapter sendAsync method to use suspendCancellableCoroutine
1 parent 431cfe7 commit 4dfb1a0

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

  • live-objects/src/main/kotlin/io/ably/lib/objects

live-objects/src/main/kotlin/io/ably/lib/objects/Helpers.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package io.ably.lib.objects
22

33
import io.ably.lib.realtime.CompletionListener
4+
import io.ably.lib.types.AblyException
45
import io.ably.lib.types.ErrorInfo
56
import io.ably.lib.types.ProtocolMessage
6-
import kotlinx.coroutines.CompletableDeferred
7+
import kotlinx.coroutines.suspendCancellableCoroutine
8+
import kotlin.coroutines.resume
9+
import kotlin.coroutines.resumeWithException
710

8-
internal suspend fun LiveObjectsAdapter.sendAsync(message: ProtocolMessage) {
9-
val deferred = CompletableDeferred<Unit>()
11+
internal suspend fun LiveObjectsAdapter.sendAsync(message: ProtocolMessage) = suspendCancellableCoroutine { continuation ->
1012
try {
1113
this.send(message, object : CompletionListener {
1214
override fun onSuccess() {
13-
deferred.complete(Unit)
15+
continuation.resume(Unit)
1416
}
1517

1618
override fun onError(reason: ErrorInfo) {
17-
deferred.completeExceptionally(Exception(reason.message))
19+
continuation.resumeWithException(AblyException.fromErrorInfo(reason))
1820
}
1921
})
2022
} catch (e: Exception) {
21-
deferred.completeExceptionally(e)
23+
continuation.resumeWithException(e)
2224
}
23-
deferred.await()
2425
}
2526

2627
internal enum class ProtocolMessageFormat(private val value: String) {

0 commit comments

Comments
 (0)