From 402e695f992c532613749f6170736afe95f5acbb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 30 Nov 2024 14:11:43 +0600 Subject: [PATCH] add CombinedSubcontextInitialAction --- .../api/tgbotapi.behaviour_builder.api | 21 ++++++++ .../CombinedSubcontextInitialAction.kt | 53 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction.kt diff --git a/tgbotapi.behaviour_builder/api/tgbotapi.behaviour_builder.api b/tgbotapi.behaviour_builder/api/tgbotapi.behaviour_builder.api index 5af9131ecf..13fbccb148 100644 --- a/tgbotapi.behaviour_builder/api/tgbotapi.behaviour_builder.api +++ b/tgbotapi.behaviour_builder/api/tgbotapi.behaviour_builder.api @@ -82,6 +82,27 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourConte public static final fun stop (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;)V } +public final class dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction { + public fun (Ljava/util/List;Ldev/inmo/kslog/common/KSLog;)V + public synthetic fun (Ljava/util/List;Ldev/inmo/kslog/common/KSLog;ILkotlin/jvm/internal/DefaultConstructorMarker;)V + public final fun getSubactions ()Ljava/util/List; + public final fun getSubcontextInitialAction ()Lkotlin/jvm/functions/Function3; +} + +public abstract interface class dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction$SubAction { + public abstract fun invoke (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Ldev/inmo/tgbotapi/types/update/abstracts/Update;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction$SubAction$Callback : dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction$SubAction { + public fun (Lkotlin/jvm/functions/Function3;)V + public fun invoke (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Ldev/inmo/tgbotapi/types/update/abstracts/Update;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; +} + +public final class dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialActionKt { + public static final fun buildSubcontextInitialAction (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function3; + public static final fun buildSubcontextInitialActionWithSubActions (Lkotlin/jvm/functions/Function1;)Lkotlin/jvm/functions/Function3; +} + public final class dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehaviourContext : dev/inmo/tgbotapi/updateshandlers/AbstractFlowsUpdatesFilter, dev/inmo/tgbotapi/bot/RequestsExecutor, dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext, kotlinx/coroutines/CoroutineScope { public fun (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;)V public synthetic fun (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Lkotlinx/coroutines/CoroutineScope;ILkotlinx/coroutines/channels/BufferOverflow;Lkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlin/jvm/functions/Function3;ILkotlin/jvm/internal/DefaultConstructorMarker;)V diff --git a/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction.kt b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction.kt new file mode 100644 index 0000000000..5099ba9f00 --- /dev/null +++ b/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/CombinedSubcontextInitialAction.kt @@ -0,0 +1,53 @@ +package dev.inmo.tgbotapi.extensions.behaviour_builder + +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.error +import dev.inmo.tgbotapi.types.update.abstracts.Update + +class CombinedSubcontextInitialAction( + val subactions: List, + private val logger: KSLog = KSLog("CombinedSubcontextInitialAction_${subactions.size}") +) { + fun interface SubAction { + suspend operator fun BehaviourContext.invoke(update: Update) + + class Callback(private val action: CustomBehaviourContextAndTypeReceiver) : SubAction { + override suspend fun BehaviourContext.invoke(update: Update) { + action(update) + } + } + } + val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver = { update -> + subactions.forEach { subaction -> + with(subaction) { + runCatching { + invoke(update) + }.onFailure { + logger.error("Unable to execute $subaction for update $update", it) + } + } + } + } +} + +inline fun buildSubcontextInitialActionWithSubActions( + block: MutableList.() -> Unit +): CustomBehaviourContextAndTypeReceiver { + val list = mutableListOf() + list.block() + return CombinedSubcontextInitialAction( + list.toList() + ).subcontextInitialAction +} + +inline fun buildSubcontextInitialAction( + block: MutableList>.() -> Unit +): CustomBehaviourContextAndTypeReceiver { + val list = mutableListOf>() + list.block() + return CombinedSubcontextInitialAction( + list.map { + CombinedSubcontextInitialAction.SubAction.Callback(it) + } + ).subcontextInitialAction +}