commonMain.ru.casperix.signals.concrete.EitherCollectionFuture.kt Maven / Gradle / Ivy
package ru.casperix.signals.concrete
import ru.casperix.misc.*
import ru.casperix.signals.api.CustomFuture
class EitherCollectionFuture(loaders: List>) :
EitherFuture, List> {
constructor(vararg loaders: EitherFuture) : this(loaders.toList())
private val resultMap: MutableList?> = loaders.map {
null
}.toMutableList()
private val outputSignal = EitherSignal, List>()
override val acceptFuture: CustomFuture<(List) -> Unit, Slot> = outputSignal.acceptFuture
override val rejectFuture: CustomFuture<(List) -> Unit, Slot> = outputSignal.rejectFuture
override val complete: Boolean get() = outputSignal.complete
init {
loaders.forEachIndexed { index, future ->
future.thenAccept {
resultMap[index] = Left(it)
outputIfCompleted()
}
future.thenReject {
resultMap[index] = Right(it)
outputIfCompleted()
}
}
}
private fun outputIfCompleted() {
val listA: List = resultMap.mapNotNull {
if (it == null) return
it.left()
}
val listB: List = resultMap.mapNotNull {
if (it == null) return
it.right()
}
if (listB.isNotEmpty()) {
outputSignal.reject(listB)
} else {
if (listA.size != resultMap.size) {
throw Exception("Invalid accept result")
}
outputSignal.accept(listA)
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy