I have a use case where I need to connect and disconnect from a class that acts as a service. Actions can be performed on the service only when the service is connected. Clients are notified when the service connects or disconnects by a callback:
class Service {
constructor(callback: ConnectionCallback) { ... }
fun connect() {
// Call callback.onConnected() some time after this method returns.
}
fun disconnect() {
// Call callback.onConnectionSuspended() some time after this method returns.
}
fun isConnected(): Boolean { ... }
fun performAction(actionName: String, callback: ActionCallback) {
// Perform a given action on the service, failing with a fatal exception if called when the service is not connected.
}
interface ConnectionCallback {
fun onConnected() // May be called multiple times
fun onConnectionSuspended() // May be called multiple times
fun onConnectionFailed()
}
}
I'd like to write a wrapper for that Service
class (that I don't control) using Kotlin Coroutines.
Here is a skeleton of ServiceWrapper
:
class ServiceWrapper {
private val service = Service(object : ConnectionCallback { ... })
fun connect() {
service.connect()
}
fun disconnect() {
service.disconnect()
}
suspend fun performActionWhenConnected(actionName: String): ActionResult {
suspendUntilConnected()
return suspendCoroutine { continuation ->
service.performAction(actionName, object : ActionCallback() {
override fun onSuccess(result: ActionResult) {
continuation.resume(result)
}
override fun onError() {
continuation.resumeWithException(RuntimeException())
}
}
}
}
}
How can I implement this suspendUntilConnected()
behavior using Coroutines ? Thanks in advance.