Support cancelling RootCommand

This commit is contained in:
Mygod
2020-07-04 07:48:56 +08:00
parent 255b4698b9
commit 562a9f26f1

View File

@@ -420,15 +420,21 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
Log.e(command.javaClass.simpleName, "Unexpected exception in RootCommandOneWay", e)
}
}
is RootCommand<*> -> defaultWorker.launch {
is RootCommand<*> -> {
val commandJob = Job()
cancellables[callback] = { commandJob.cancel() }
defaultWorker.launch(commandJob) {
val result = try {
val result = command.execute();
{ output.pushResult(callback, result) }
} catch (e: Throwable) {
{ output.pushThrowable(callback, e) }
} finally {
cancellables.remove(callback)
}
withContext(callbackWorker) { result() }
}
}
is RootCommandChannel<*> -> defaultWorker.launch {
val result = try {
coroutineScope {