From a90df1b88c1f62576061bc146ca2151b075dc155 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 18 Jul 2020 03:41:50 +0800 Subject: [PATCH] Downgrade coroutines until Kotlin 1.4 --- mobile/build.gradle.kts | 2 +- .../be/mygod/librootkotlinx/RootServer.kt | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/mobile/build.gradle.kts b/mobile/build.gradle.kts index a8f6072f..af306ec5 100644 --- a/mobile/build.gradle.kts +++ b/mobile/build.gradle.kts @@ -95,7 +95,7 @@ dependencies { implementation("com.takisoft.preferencex:preferencex-simplemenu:1.1.0") implementation("eu.chainfire:librootjava:1.3.0") implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.2") - implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.8") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.6") testImplementation("junit:junit:4.13") androidTestImplementation("androidx.room:room-testing:$roomVersion") androidTestImplementation("androidx.test:runner:1.2.0") diff --git a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt index b2c8d77e..eceedd80 100644 --- a/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt +++ b/mobile/src/main/java/be/mygod/librootkotlinx/RootServer.kt @@ -476,34 +476,35 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U cancellables[callback] = { commandJob.cancel() } defaultWorker.launch(commandJob) { val result = try { - val result = command.execute(); - { output.pushResult(callback, result) } + val result = command.execute() + withContext(callbackWorker + NonCancellable) { output.pushResult(callback, result) } } catch (e: Throwable) { - { output.pushThrowable(callback, e) } + withContext(callbackWorker + NonCancellable) { output.pushThrowable(callback, e) } + } finally { + cancellables.remove(callback) } - cancellables.remove(callback) - withContext(callbackWorker + NonCancellable) { result() } } } is RootCommandChannel<*> -> defaultWorker.launch { - val result = try { + try { coroutineScope { command.create(this).also { cancellables[callback] = { it.cancel() } }.consumeEach { result -> withContext(callbackWorker) { output.pushResult(callback, result) } } - }; - @Suppress("BlockingMethodInNonBlockingContext") { + } + @Suppress("BlockingMethodInNonBlockingContext") + withContext(callbackWorker + NonCancellable) { output.writeByte(CHANNEL_CONSUMED) output.writeLong(callback) output.flush() } } catch (e: Throwable) { - { output.pushThrowable(callback, e) } + withContext(callbackWorker + NonCancellable) { output.pushThrowable(callback, e) } + } finally { + cancellables.remove(callback) } - cancellables.remove(callback) - withContext(callbackWorker + NonCancellable) { result() } } is Shutdown -> break@loop else -> throw IllegalArgumentException("Unrecognized input: $command")