1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-03 07:09:23 +00:00

updates in behaviour builder

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

View File

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

View File

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

View File

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

View File

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

View File

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