From 32451f4e1cd142072089fb7ffbf7e1116a366dbb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 3 May 2022 09:44:39 +0600 Subject: [PATCH 1/5] start 0.38.19 --- CHANGELOG.md | 2 ++ gradle.properties | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f05e77f22..36ae24b52e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.38.19 + ## 0.38.18 * `Core`: diff --git a/gradle.properties b/gradle.properties index 876f4856d8..0401f4f412 100644 --- a/gradle.properties +++ b/gradle.properties @@ -20,6 +20,6 @@ javax_activation_version=1.1.1 dokka_version=1.6.10 library_group=dev.inmo -library_version=0.38.18 +library_version=0.38.19 github_release_plugin_version=2.3.7 From 89d13de3076a4ce2a096178ec9fd8480d9b21475 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 3 May 2022 10:38:48 +0600 Subject: [PATCH 2/5] deprecation of BehaviourContextWithFSMBuilder --- .../BehaviourContextWithFSM.kt | 61 +++++++++++- .../BehaviourContextWithFSMBuilder.kt | 98 ++++--------------- .../behaviour_builder/TelegramBotWithFSM.kt | 4 +- 3 files changed, 77 insertions(+), 86 deletions(-) diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt index be5cdb8923..98ec32c183 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt @@ -9,6 +9,8 @@ import dev.inmo.micro_utils.coroutines.accumulatorFlow import kotlinx.coroutines.* import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.* +import kotlin.jvm.JvmName +import kotlin.reflect.KClass /** * Interface which combine [BehaviourContext] and [StatesMachine]. Subcontext of triggers and states contexts must have @@ -30,6 +32,25 @@ interface BehaviourContextWithFSM : BehaviourContext, StatesMachine add(kClass: KClass, strict: Boolean = false, handler: BehaviourWithFSMStateHandler) + + /** + * Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that + * for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass + * requirements + * + * @see BehaviourWithFSMStateHandlerHolder + * @see strictlyOn + */ + fun addStrict(kClass: KClass, handler: BehaviourWithFSMStateHandler) = add(kClass, strict = true, handler) + override fun copy( bot: TelegramBot, scope: CoroutineScope, @@ -40,14 +61,42 @@ interface BehaviourContextWithFSM : BehaviourContext, StatesMachine companion object { + @JvmName("invokeWithMutableList") + operator fun invoke( + behaviourContext: BehaviourContext, + handlers: MutableList>, + statesManager: StatesManager + ) = DefaultBehaviourContextWithFSM(behaviourContext, statesManager, handlers.toMutableList()) operator fun invoke( behaviourContext: BehaviourContext, handlers: List>, statesManager: StatesManager - ) = DefaultBehaviourContextWithFSM(behaviourContext, statesManager, handlers) + ) = invoke(behaviourContext, handlers.toMutableList(), statesManager) } } + +/** + * Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that + * for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement + * + * @see BehaviourWithFSMStateHandlerHolder + * @see BehaviourContextWithFSM.add + */ +@Suppress("MemberVisibilityCanBePrivate") +inline fun BehaviourContextWithFSM.onStateOrSubstate(handler: BehaviourWithFSMStateHandler) = add(I::class, strict = false, handler) + +/** + * Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that + * for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass + * requirements + * + * @see BehaviourWithFSMStateHandlerHolder + * @see BehaviourContextWithFSM.addStrict + */ +@Suppress("MemberVisibilityCanBePrivate") +inline fun BehaviourContextWithFSM.strictlyOn(handler: BehaviourWithFSMStateHandler) = addStrict(I::class, handler) + /** * Default realization of [BehaviourContextWithFSM]. It uses [behaviourContext] as a base for this object as * [BehaviourContext], but managing substates contexts updates for avoiding of updates lost between states @@ -55,7 +104,7 @@ interface BehaviourContextWithFSM : BehaviourContext, StatesMachine( private val behaviourContext: BehaviourContext, private val statesManager: StatesManager, - private val handlers: List> + private val handlers: MutableList> ) : BehaviourContext by behaviourContext, BehaviourContextWithFSM { private val updatesFlows = mutableMapOf>() private fun getContextUpdatesFlow(context: Any) = updatesFlows.getOrPut(context) { @@ -68,6 +117,10 @@ class DefaultBehaviourContextWithFSM( handlers ) + override fun add(kClass: KClass, strict: Boolean, handler: BehaviourWithFSMStateHandler) { + handlers.add(BehaviourWithFSMStateHandlerHolder(kClass, strict, handler)) + } + override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions { val statePerformer: suspend (T) -> Unit = { state: T -> val newState = launchStateHandling(state, getContextUpdatesFlow(state.context), handlers) @@ -106,9 +159,9 @@ class DefaultBehaviourContextWithFSM( onBufferOverflow: BufferOverflow, upstreamUpdatesFlow: Flow?, updatesFilter: BehaviourContextAndTypeReceiver? - ): BehaviourContextWithFSM = BehaviourContextWithFSM( + ): DefaultBehaviourContextWithFSM = BehaviourContextWithFSM( behaviourContext.copy(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter), - handlers, + handlers.toMutableList(), statesManager ) } diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt index 7723b81c70..2e5cfe4615 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt @@ -14,70 +14,8 @@ import kotlinx.coroutines.* import kotlinx.coroutines.flow.Flow import kotlin.reflect.KClass -class BehaviourContextWithFSMBuilder internal constructor( - private val resultBehaviourContext: BehaviourContextWithFSM, - private val handlers: MutableList> -) : BehaviourContextWithFSM by resultBehaviourContext { - internal constructor( - baseBehaviourContext: BehaviourContext, - statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - handlers: MutableList> = mutableListOf() - ) : this(DefaultBehaviourContextWithFSM(baseBehaviourContext, statesManager, handlers), handlers) - - /** - * Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that - * for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement - * - * @see BehaviourWithFSMStateHandlerHolder - * @see onStateOrSubstate - */ - fun add(kClass: KClass, handler: BehaviourWithFSMStateHandler) { - handlers.add(BehaviourWithFSMStateHandlerHolder(kClass, false, handler)) - } - - /** - * Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that - * for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass - * requirements - * - * @see BehaviourWithFSMStateHandlerHolder - * @see strictlyOn - */ - fun addStrict(kClass: KClass, handler: BehaviourWithFSMStateHandler) { - handlers.add(BehaviourWithFSMStateHandlerHolder(kClass, true, handler)) - } - - - /** - * Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that - * for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement - * - * @see BehaviourWithFSMStateHandlerHolder - * @see BehaviourContextWithFSMBuilder.add - */ - @Suppress("MemberVisibilityCanBePrivate") - inline fun onStateOrSubstate(handler: BehaviourWithFSMStateHandler) { - add(I::class, handler) - } - - /** - * Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that - * for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass - * requirements - * - * @see BehaviourWithFSMStateHandlerHolder - * @see BehaviourContextWithFSMBuilder.addStrict - */ - @Suppress("MemberVisibilityCanBePrivate") - inline fun strictlyOn(handler: BehaviourWithFSMStateHandler) { - addStrict(I::class, handler) - } - - /** - * Returns completed [resultBehaviourContext], [handlers] and [statesManager] - */ - internal fun build() = resultBehaviourContext -} +@Deprecated("Will be removed soon") +typealias BehaviourContextWithFSMBuilder = BehaviourContextWithFSM /** * Creates [BehaviourContextWithFSM] via creating of [DefaultBehaviourContext] with [this] as [TelegramBot], @@ -95,16 +33,16 @@ suspend fun TelegramBot.buildBehaviourWithFSM( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> -): BehaviourContextWithFSM = BehaviourContextWithFSMBuilder( + block: CustomBehaviourContextReceiver, Unit> +): BehaviourContextWithFSM = BehaviourContextWithFSM( DefaultBehaviourContext( this, defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope, upstreamUpdatesFlow = upstreamUpdatesFlow ), - statesManager, - presetHandlers -).apply { block() }.build() + presetHandlers, + statesManager +).apply { block() } /** * Use [buildBehaviourWithFSM] to create [BehaviourContextWithFSM] and launch getting of updates @@ -117,7 +55,7 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ): Pair, Job> = buildBehaviourWithFSM( upstreamUpdatesFlow, scope, @@ -147,8 +85,8 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( * @see BehaviourContext * @see BehaviourContextWithFSM * @see longPolling - * @see BehaviourContextWithFSMBuilder.strictlyOn - * @see BehaviourContextWithFSMBuilder.onStateOrSubstate + * @see BehaviourContextWithFSM.strictlyOn + * @see BehaviourContextWithFSM.onStateOrSubstate */ @PreviewFeature suspend fun TelegramBot.buildBehaviourWithFSM( @@ -157,16 +95,16 @@ suspend fun TelegramBot.buildBehaviourWithFSM( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> -): BehaviourContextWithFSM = BehaviourContextWithFSMBuilder( + block: CustomBehaviourContextReceiver, Unit> +): BehaviourContextWithFSM = BehaviourContextWithFSM( DefaultBehaviourContext( this, defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope, upstreamUpdatesFlow = flowUpdatesFilter.allUpdatesFlow ), - statesManager, - presetHandlers -).apply { block() }.build() + presetHandlers, + statesManager +).apply { block() } /** * Use [buildBehaviourWithFSM] to create [BehaviourContextWithFSM] and launch getting of updates @@ -176,8 +114,8 @@ suspend fun TelegramBot.buildBehaviourWithFSM( * @see buildBehaviourWithFSMAndStartLongPolling * @see BehaviourContext * @see longPolling - * @see BehaviourContextWithFSMBuilder.strictlyOn - * @see BehaviourContextWithFSMBuilder.onStateOrSubstate + * @see BehaviourContextWithFSM.strictlyOn + * @see BehaviourContextWithFSM.onStateOrSubstate */ @PreviewFeature suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( @@ -185,7 +123,7 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ) = FlowsUpdatesFilter().let { buildBehaviourWithFSM( it, diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt index edfc67de48..91e5d64e43 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt @@ -39,7 +39,7 @@ suspend fun telegramBotWithBehaviourAndFSM( statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), testServer: Boolean = false, - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ): TelegramBot = telegramBot( token, apiUrl, @@ -76,7 +76,7 @@ suspend fun telegramBotWithBehaviourAndFSMAndStartLongPolling( statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), testServer: Boolean = false, - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ): Pair { return telegramBot( token, From 3e3adab46b263729caf5152f5a4288c6d70e3901 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 3 May 2022 13:58:13 +0600 Subject: [PATCH 3/5] complete fix of issue with subcontexts --- .../BehaviourContextWithFSM.kt | 20 +++++++++++++++++++ .../BehaviourContextWithFSMBuilder.kt | 14 ++++++------- .../behaviour_builder/TelegramBotWithFSM.kt | 4 ++-- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt index 98ec32c183..74ae650f11 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt @@ -147,6 +147,26 @@ class DefaultBehaviourContextWithFSM( launch { statePerformer(it) } } } + /** + * Add NON STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Non strict means that + * for input [State] will be used [KClass.isInstance] and any inheritor of [kClass] will pass this requirement + * + * @see BehaviourWithFSMStateHandlerHolder + * @see BehaviourContextWithFSM.add + */ + @Suppress("MemberVisibilityCanBePrivate") + inline fun onStateOrSubstate(handler: BehaviourWithFSMStateHandler) = add(I::class, strict = false, handler) + + /** + * Add STRICT [handler] to list of available in future [BehaviourContextWithFSM]. Strict means that + * for input [State] will be used [State]::class == [kClass] and any [State] with exactly the same type will pass + * requirements + * + * @see BehaviourWithFSMStateHandlerHolder + * @see BehaviourContextWithFSM.addStrict + */ + @Suppress("MemberVisibilityCanBePrivate") + inline fun strictlyOn(handler: BehaviourWithFSMStateHandler) = addStrict(I::class, handler) override suspend fun startChain(state: T) { statesManager.startChain(state) diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt index 2e5cfe4615..4e968c8b51 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt @@ -33,8 +33,8 @@ suspend fun TelegramBot.buildBehaviourWithFSM( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> -): BehaviourContextWithFSM = BehaviourContextWithFSM( + block: CustomBehaviourContextReceiver, Unit> +): DefaultBehaviourContextWithFSM = BehaviourContextWithFSM( DefaultBehaviourContext( this, defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope, @@ -55,8 +55,8 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> -): Pair, Job> = buildBehaviourWithFSM( + block: CustomBehaviourContextReceiver, Unit> +): Pair, Job> = buildBehaviourWithFSM( upstreamUpdatesFlow, scope, defaultExceptionsHandler, @@ -95,8 +95,8 @@ suspend fun TelegramBot.buildBehaviourWithFSM( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> -): BehaviourContextWithFSM = BehaviourContextWithFSM( + block: CustomBehaviourContextReceiver, Unit> +): DefaultBehaviourContextWithFSM = BehaviourContextWithFSM( DefaultBehaviourContext( this, defaultExceptionsHandler ?.let { scope + ContextSafelyExceptionHandler(it) } ?: scope, @@ -123,7 +123,7 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ) = FlowsUpdatesFilter().let { buildBehaviourWithFSM( it, diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt index 91e5d64e43..ffc7af1215 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt @@ -39,7 +39,7 @@ suspend fun telegramBotWithBehaviourAndFSM( statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), testServer: Boolean = false, - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ): TelegramBot = telegramBot( token, apiUrl, @@ -76,7 +76,7 @@ suspend fun telegramBotWithBehaviourAndFSMAndStartLongPolling( statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), presetHandlers: MutableList> = mutableListOf(), testServer: Boolean = false, - block: CustomBehaviourContextReceiver, Unit> + block: CustomBehaviourContextReceiver, Unit> ): Pair { return telegramBot( token, From d0add888c44c53332d5d53dc4411a2453a9fa09f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 3 May 2022 14:28:03 +0600 Subject: [PATCH 4/5] now the handlers from the upstream lists are not modified in the whole fsm behaviour context --- .../BehaviourContextWithFSM.kt | 22 +++++++++---------- .../BehaviourContextWithFSMBuilder.kt | 8 +++---- .../behaviour_builder/TelegramBotWithFSM.kt | 4 ++-- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt index 74ae650f11..6212a4134a 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSM.kt @@ -61,17 +61,11 @@ interface BehaviourContextWithFSM : BehaviourContext, StatesMachine companion object { - @JvmName("invokeWithMutableList") - operator fun invoke( - behaviourContext: BehaviourContext, - handlers: MutableList>, - statesManager: StatesManager - ) = DefaultBehaviourContextWithFSM(behaviourContext, statesManager, handlers.toMutableList()) operator fun invoke( behaviourContext: BehaviourContext, handlers: List>, statesManager: StatesManager - ) = invoke(behaviourContext, handlers.toMutableList(), statesManager) + ) = DefaultBehaviourContextWithFSM(behaviourContext, statesManager, handlers) } } @@ -104,9 +98,12 @@ inline fun BehaviourContextWithFSM.strictlyOn(handl class DefaultBehaviourContextWithFSM( private val behaviourContext: BehaviourContext, private val statesManager: StatesManager, - private val handlers: MutableList> + private val handlers: List> ) : BehaviourContext by behaviourContext, BehaviourContextWithFSM { private val updatesFlows = mutableMapOf>() + private val additionalHandlers = mutableListOf>() + private var actualHandlersList = additionalHandlers + handlers + private fun getContextUpdatesFlow(context: Any) = updatesFlows.getOrPut(context) { allUpdatesFlow.accumulatorFlow(scope) } @@ -114,16 +111,17 @@ class DefaultBehaviourContextWithFSM( override suspend fun StatesMachine.handleState(state: T): T? = launchStateHandling( state, allUpdatesFlow, - handlers + actualHandlersList ) override fun add(kClass: KClass, strict: Boolean, handler: BehaviourWithFSMStateHandler) { - handlers.add(BehaviourWithFSMStateHandlerHolder(kClass, strict, handler)) + additionalHandlers.add(BehaviourWithFSMStateHandlerHolder(kClass, strict, handler)) + actualHandlersList = additionalHandlers + handlers } override fun start(scope: CoroutineScope): Job = scope.launchSafelyWithoutExceptions { val statePerformer: suspend (T) -> Unit = { state: T -> - val newState = launchStateHandling(state, getContextUpdatesFlow(state.context), handlers) + val newState = launchStateHandling(state, getContextUpdatesFlow(state.context), actualHandlersList) if (newState != null) { statesManager.update(state, newState) } else { @@ -181,7 +179,7 @@ class DefaultBehaviourContextWithFSM( updatesFilter: BehaviourContextAndTypeReceiver? ): DefaultBehaviourContextWithFSM = BehaviourContextWithFSM( behaviourContext.copy(bot, scope, broadcastChannelsSize, onBufferOverflow, upstreamUpdatesFlow, updatesFilter), - handlers.toMutableList(), + handlers, statesManager ) } diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt index 4e968c8b51..b1dd83032b 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextWithFSMBuilder.kt @@ -32,7 +32,7 @@ suspend fun TelegramBot.buildBehaviourWithFSM( scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - presetHandlers: MutableList> = mutableListOf(), + presetHandlers: List> = listOf(), block: CustomBehaviourContextReceiver, Unit> ): DefaultBehaviourContextWithFSM = BehaviourContextWithFSM( DefaultBehaviourContext( @@ -54,7 +54,7 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - presetHandlers: MutableList> = mutableListOf(), + presetHandlers: List> = listOf(), block: CustomBehaviourContextReceiver, Unit> ): Pair, Job> = buildBehaviourWithFSM( upstreamUpdatesFlow, @@ -94,7 +94,7 @@ suspend fun TelegramBot.buildBehaviourWithFSM( scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - presetHandlers: MutableList> = mutableListOf(), + presetHandlers: List> = listOf(), block: CustomBehaviourContextReceiver, Unit> ): DefaultBehaviourContextWithFSM = BehaviourContextWithFSM( DefaultBehaviourContext( @@ -122,7 +122,7 @@ suspend fun TelegramBot.buildBehaviourWithFSMAndStartLongPolling( scope: CoroutineScope = defaultCoroutineScopeProvider(), defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - presetHandlers: MutableList> = mutableListOf(), + presetHandlers: List> = listOf(), block: CustomBehaviourContextReceiver, Unit> ) = FlowsUpdatesFilter().let { buildBehaviourWithFSM( diff --git a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt index ffc7af1215..2d55cf322a 100644 --- a/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt +++ b/tgbotapi.behaviour_builder.fsm/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/TelegramBotWithFSM.kt @@ -37,7 +37,7 @@ suspend fun telegramBotWithBehaviourAndFSM( builder: KtorRequestsExecutorBuilder.() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - presetHandlers: MutableList> = mutableListOf(), + presetHandlers: List> = listOf(), testServer: Boolean = false, block: CustomBehaviourContextReceiver, Unit> ): TelegramBot = telegramBot( @@ -74,7 +74,7 @@ suspend fun telegramBotWithBehaviourAndFSMAndStartLongPolling( builder: KtorRequestsExecutorBuilder.() -> Unit = {}, defaultExceptionsHandler: ExceptionHandler? = null, statesManager: StatesManager = DefaultStatesManager(InMemoryDefaultStatesManagerRepo()), - presetHandlers: MutableList> = mutableListOf(), + presetHandlers: List> = listOf(), testServer: Boolean = false, block: CustomBehaviourContextReceiver, Unit> ): Pair { From 3c3607d81777c0c58dddbfe5e65d9e21bfe37ef8 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 3 May 2022 14:32:29 +0600 Subject: [PATCH 5/5] fill changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36ae24b52e..ececa0b868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ ## 0.38.19 +* `BehaviourBuilder`: + * Hotfixes +* `BehaviourBuilder FSM`: + * `BehaviourContextWithFSMBuilder` deprecated in favor to `BehaviourContextWithFSM` + * Now it is possible to define additional handlers in subcontexts of `BehaviourBuilderWithFSM` + ## 0.38.18 * `Core`: