From 21c5d42dc2e668587a23d01a79ba17b0857d82d1 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 28 Jun 2021 10:29:45 +0600 Subject: [PATCH] BehaviourContext now is interface --- CHANGELOG.md | 2 + .../behaviour_builder/BehaviourContext.kt | 50 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 542d7294ba..35d813280c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ * All triggers (`on*` extensions) have been modified to work in parallel by default (new parameter `performInParallel`, by default `true`) * All behaviour builder extensions got new parameter `defaultExceptionsHandler` + * Class `BehaviourContext` was rewritten as an interface with default realization `DefaultBehaviourContext` and + factory `BehaviourContext(TelegramBot, CoroutineScope, FlowsUpdatesFilter)` * `API`: * All `reply` and subsequent extension have been replaced in send package * `Bot API 5.3`: diff --git a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt index 5180e25055..c92ff63c62 100644 --- a/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt +++ b/tgbotapi.extensions.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext.kt @@ -14,15 +14,49 @@ typealias BehaviourContextAndTypeReceiver = suspend BehaviourContext.(I) - /** * This class contains all necessary tools for work with bots and especially for [buildBehaviour] * - * @param scope This param will be used for creating of some subscriptions inside of methods, updates listening and - * different other things in context of working with [CoroutineScope] and coroutines. - * @param flowsUpdatesFilter This parameter will be used to subscribe on different types of update + * @see DefaultBehaviourContext */ -data class BehaviourContext( - val bot: TelegramBot, - val scope: CoroutineScope, - val flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter() -) : FlowsUpdatesFilter by flowsUpdatesFilter, TelegramBot by bot, CoroutineScope by scope +interface BehaviourContext : FlowsUpdatesFilter, TelegramBot, CoroutineScope { + val bot: TelegramBot + get() = this + + /** + * Will be used for creating of some subscriptions inside of methods, updates listening and different other things + * in context of working with [CoroutineScope] and coroutines. + */ + val scope: CoroutineScope + get() = this + + /** + * This parameter will be used to subscribe on different types of update + */ + val flowsUpdatesFilter: FlowsUpdatesFilter + get() = this + + fun copy( + bot: TelegramBot = this.bot, + scope: CoroutineScope = this.scope, + flowsUpdatesFilter: FlowsUpdatesFilter = this.flowsUpdatesFilter + ): BehaviourContext +} + +class DefaultBehaviourContext( + override val bot: TelegramBot, + override val scope: CoroutineScope, + override val flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter() +) : FlowsUpdatesFilter by flowsUpdatesFilter, TelegramBot by bot, CoroutineScope by scope, BehaviourContext { + override fun copy( + bot: TelegramBot, + scope: CoroutineScope, + flowsUpdatesFilter: FlowsUpdatesFilter + ): DefaultBehaviourContext = DefaultBehaviourContext(bot, scope, flowsUpdatesFilter) +} + +fun BehaviourContext( + bot: TelegramBot, + scope: CoroutineScope, + flowsUpdatesFilter: FlowsUpdatesFilter = FlowsUpdatesFilter() +) = DefaultBehaviourContext(bot, scope, flowsUpdatesFilter) /** * Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [newFlowsUpdatesFilterSetUp] is provided and