Stage 4 Changes - Xantier/multiplat-workshop GitHub Wiki
Adding coroutines to common ApiClient
Common
- ApiClient.kt
- Modify ApiClient fetchPhotos to be a
suspend
fun
- Modify ApiClient fetchPhotos to be a
JS
- ApiClient.kt
- Modify ApiClient fetchPhotos to be a
suspend
fun - Remove
async block, return
response.text().await()` from function
- Modify ApiClient fetchPhotos to be a
JVM
- ApiClient.kt
- Modify ApiClient fetchPhotos to be a
suspend
fun - Modify okhttp to not block
val response = client.newCall(request).await()
- Modify ApiClient fetchPhotos to be a
suspend fun Call.await(): Response {
return suspendCoroutine { continuation ->
enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
continuation.resume(response)
}
override fun onFailure(call: Call, e: IOException) {
// Don't bother with resuming the continuation if it is already cancelled.
continuation.resumeWithException(e)
}
})
}
}
- build.gradle
- Add coroutines support to module
Common Client
-
PhotoProvider.kt
- Modify method to be suspending
-
build.gradle
- Add coroutines support to module
Client React
- index.kt
- Wrap method call into launch block
Client Desktop
-
gradle.build
- Add coroutines support to module
-
Main.kt
- Wrap method call to runBlocking block