Last active
July 9, 2019 10:32
-
-
Save Tetraquark/1f1edcb726f9e25eb3e88f7bdb39f00c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ru.tetraquark.kexperiments.callbacktosuspend.SomeJavaApi | |
import kotlinx.coroutines.* | |
import kotlin.Exception | |
import kotlin.coroutines.Continuation | |
import kotlin.coroutines.resume | |
import kotlin.coroutines.resumeWithException | |
import kotlin.coroutines.suspendCoroutine | |
fun main() = runBlocking { | |
val oldJavaApi = SomeJavaApi() | |
runDefaultCallback(oldJavaApi) | |
println("") | |
runSuspendedCallback(oldJavaApi, this) | |
oldJavaApi.destroy() | |
} | |
suspend fun runDefaultCallback(oldJavaApi: SomeJavaApi) { | |
val defaultApiCallback = object : SomeJavaApi.CustomCallback { | |
override fun onSuccess(data: String) { | |
println("[Thread id: ${Thread.currentThread().id}]\tOld api callback onSuccess: $data") | |
} | |
override fun onFailure(exception: Exception) { | |
println("[Thread id: ${Thread.currentThread().id}]\tOld api callback onFailure: $exception") | |
} | |
} | |
oldJavaApi.successRemoteCall(defaultApiCallback) | |
oldJavaApi.failedRemoteCall(defaultApiCallback) | |
println("[Thread id: ${Thread.currentThread().id}]\tMain thread is going to sleep.") | |
delay(3500) | |
} | |
suspend fun runSuspendedCallback(oldJavaApi: SomeJavaApi, coroutineScope: CoroutineScope) { | |
val successfulJob = coroutineScope.launch { | |
val apiCallResult = suspendCoroutine<String> { continuation -> | |
val suspendedCallback = createOldApiSuspendedCallback(continuation) | |
oldJavaApi.successRemoteCall(suspendedCallback) | |
} | |
println("[Thread id: ${Thread.currentThread().id}]\tSuccessful coroutine result: $apiCallResult") | |
} | |
val unsuccessfulJob = coroutineScope.launch { | |
try { | |
suspendCoroutine<String> { continuation -> | |
val suspendedCallback = createOldApiSuspendedCallback(continuation) | |
oldJavaApi.failedRemoteCall(suspendedCallback) | |
} | |
} catch (exception: Exception) { | |
println("[Thread id: ${Thread.currentThread().id}]\tUnsuccessful coroutine result: $exception") | |
} | |
} | |
// Waiting all coroutines | |
println("[Thread id: ${Thread.currentThread().id}]\tMain thread is waiting for coroutines.") | |
joinAll(successfulJob, unsuccessfulJob) | |
} | |
fun createOldApiSuspendedCallback(continuation: Continuation<String>): SomeJavaApi.CustomCallback { | |
return object : SomeJavaApi.CustomCallback { | |
override fun onSuccess(data: String) { | |
println("[Thread id: ${Thread.currentThread().id}]\tSuspended callback onSuccess: resume continuation") | |
continuation.resume(data) | |
} | |
override fun onFailure(exception: Exception) { | |
println("[Thread id: ${Thread.currentThread().id}]\tSuspended callback onFailure: " + | |
"resume continuation with exception") | |
continuation.resumeWithException(exception) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment