1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-03 00:15:27 +00:00
tgbotapi/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/Variants.kt

33 lines
1.1 KiB
Kotlin
Raw Normal View History

2021-01-19 12:50:45 +00:00
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.DeferredAction
import dev.inmo.micro_utils.coroutines.invokeFirstOf
2021-04-28 13:59:30 +00:00
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
2021-01-19 12:50:45 +00:00
suspend fun <T> BehaviourContext.parallel(
action: BehaviourContextReceiver<T>
) = async {
action()
}
inline infix fun <T, O> Deferred<T>.withAction(noinline callback: suspend (T) -> O) = DeferredAction(this, callback)
inline fun <T> Deferred<T>.asAction() = DeferredAction(this) { it }
suspend fun <O> BehaviourContext.oneOfActions(
2021-01-19 12:50:45 +00:00
deferredActions: Iterable<DeferredAction<*, O>>
) = deferredActions.invokeFirstOf(scope)
suspend fun <O> BehaviourContext.oneOfActions(
2021-01-19 12:50:45 +00:00
vararg deferredActions: DeferredAction<*, O>
) = this@oneOfActions.oneOfActions(deferredActions.toList())
suspend fun <O> BehaviourContext.oneOf(
deferredActions: Iterable<Deferred<O>>
) = oneOfActions(deferredActions.map { it.asAction() })
suspend fun <O> BehaviourContext.oneOf(
vararg deferredActions: Deferred<O>
2021-01-19 12:50:45 +00:00
) = oneOf(deferredActions.toList())