1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 00:57:13 +00:00

updates in behaviour builder

This commit is contained in:
InsanusMokrassar 2021-04-05 16:41:27 +06:00
parent f4b1e4a150
commit b975a1b036
9 changed files with 41 additions and 23 deletions

View File

@ -2,6 +2,10 @@
## 0.33.3 ## 0.33.3
* `Behaviour Builder`:
* Rewrite logic of `doInSubContextWithUpdatesFilter` and `doInSubContextWithFlowsUpdatesFilterSetup` extensions
* All triggers now work with `stopOnCompletion` set up to `false`
## 0.33.2 ## 0.33.2
* `Common`: * `Common`:

View File

@ -36,12 +36,13 @@ suspend fun <T> BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup(
newFlowsUpdatesFilterSetUp: BehaviourContextAndTypeReceiver<Unit, FlowsUpdatesFilter>?, newFlowsUpdatesFilterSetUp: BehaviourContextAndTypeReceiver<Unit, FlowsUpdatesFilter>?,
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,
behaviourContextReceiver: BehaviourContextReceiver<T> behaviourContextReceiver: BehaviourContextReceiver<T>
) = copy( ): T = supervisorScope {
flowsUpdatesFilter = FlowsUpdatesFilter(), val newContext = copy(
scope = CoroutineScope(scope.coroutineContext + SupervisorJob()) flowsUpdatesFilter = FlowsUpdatesFilter(),
).run { scope = this
)
newFlowsUpdatesFilterSetUp ?.let { newFlowsUpdatesFilterSetUp ?.let {
it.apply { invoke(this@run, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) } it.apply { invoke(newContext, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) }
} }
behaviourContextReceiver().also { if (stopOnCompletion) stop() } behaviourContextReceiver().also { if (stopOnCompletion) stop() }
} }
@ -54,17 +55,23 @@ suspend fun <T> BehaviourContext.doInSubContextWithUpdatesFilter(
updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?, updatesFilter: BehaviourContextAndTypeReceiver<Boolean, Update>?,
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,
behaviourContextReceiver: BehaviourContextReceiver<T> behaviourContextReceiver: BehaviourContextReceiver<T>
) = doInSubContextWithFlowsUpdatesFilterSetup( ): T {
newFlowsUpdatesFilterSetUp = updatesFilter ?.let { val updatesScope = CoroutineScope(coroutineContext + SupervisorJob())
{ oldOne ->
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) return doInSubContextWithFlowsUpdatesFilterSetup(
} newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
} ?: { oldOne -> { oldOne ->
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver) oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
}, }
stopOnCompletion, } ?: { oldOne ->
behaviourContextReceiver oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
) },
stopOnCompletion
) {
coroutineContext.job.invokeOnCompletion { updatesScope.cancel() }
behaviourContextReceiver()
}
}
suspend fun <T> BehaviourContext.doInSubContext( suspend fun <T> BehaviourContext.doInSubContext(
stopOnCompletion: Boolean = true, stopOnCompletion: Boolean = true,

View File

@ -26,7 +26,8 @@ internal suspend inline fun <reified T : CallbackQuery> BehaviourContext.onCallb
{ it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerQuery.user.id.chatId }
} else { } else {
null null
} },
stopOnCompletion = false
) { ) {
scenarioReceiver(triggerQuery) scenarioReceiver(triggerQuery)
} }

View File

@ -24,7 +24,8 @@ internal suspend inline fun <reified U : ChatMemberUpdatedUpdate> BehaviourConte
{ it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerChatMemberUpdated.chat.id.chatId }
} else { } else {
null null
} },
stopOnCompletion = false
) { ) {
scenarioReceiver(triggerChatMemberUpdated) scenarioReceiver(triggerChatMemberUpdated)
} }

View File

@ -50,7 +50,8 @@ internal suspend inline fun <reified T : MessageContent> BehaviourContext.onCont
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
} else { } else {
null null
} },
stopOnCompletion = false
) { ) {
scenarioReceiver(triggerMessage) scenarioReceiver(triggerMessage)
} }

View File

@ -29,7 +29,8 @@ internal suspend inline fun <reified T : ChatEvent> BehaviourContext.onEvent(
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
} else null } else null,
stopOnCompletion = false
) { ) {
scenarioReceiver(triggerMessage) scenarioReceiver(triggerMessage)
} }

View File

@ -27,7 +27,8 @@ internal suspend inline fun <reified T : InlineQuery> BehaviourContext.onInlineQ
{ it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerQuery.from.id.chatId }
} else { } else {
null null
} },
stopOnCompletion = false
) { ) {
scenarioReceiver(triggerQuery) scenarioReceiver(triggerQuery)
} }

View File

@ -32,7 +32,8 @@ internal suspend inline fun <reified T : MediaGroupContent> BehaviourContext.bui
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == mediaGroupChat.id.chatId } { it.sourceChat() ?.id ?.chatId == mediaGroupChat.id.chatId }
} else null } else null,
stopOnCompletion = false
) { ) {
scenarioReceiver(mediaGroup) scenarioReceiver(mediaGroup)
} }

View File

@ -25,7 +25,8 @@ suspend inline fun <reified T : EncryptedPassportElement> BehaviourContext.onPas
doInSubContextWithUpdatesFilter( doInSubContextWithUpdatesFilter(
updatesFilter = if (includeFilterByChatInBehaviourSubContext) { updatesFilter = if (includeFilterByChatInBehaviourSubContext) {
{ it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId } { it.sourceChat() ?.id ?.chatId == triggerMessage.chat.id.chatId }
} else null } else null,
stopOnCompletion = false
) { ) {
scenarioReceiver(triggerMessage) scenarioReceiver(triggerMessage)
} }