我正在我当前的 Android 应用程序中调查 Kotlin Coroutines & Channels。
我有以下代码来管理远程 Api 调用和控制 UI 副作用
private val historical: CompletableDeferred<List<Any>> = CompletableDeferred()
private val mutex = Mutex()
@ExperimentalCoroutinesApi
fun perform(action: Action): ReceiveChannel<List<Any>> =
produce {
mutex.withLock {
if (historical.isCompleted) {
send(historical.getCompleted())
return@produce
}
send(action.sideEffects)
val networkResponse = repository.perform(action)
send(networkResponse.sideEffects)
send(listOf(networkResponse)).also {
historical.complete(listOf(response))
}
}
}
上面的代码给了我想要的结果,但是我想将它重构为类似于函数式编程“铁路模式”的东西https://android.jlelse.eu/real-world-functional-programming-with-kotlin-arrow- b5a98e72f5e3
我的流程在哪里
stepOne(Historical.completed)
.stepTwo(action.sideEffects)
.stepThree(getReaction())
.stepFour(reaction.sideEffects)
.finalStep(reaction)
这将在任何步骤失败或历史“已完成”时“短路”
是否有可能在 Kotlin 中实现这种调用方式?和/或 Kotlin & Arrow.kt?