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:
parent
f4b1e4a150
commit
b975a1b036
@ -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`:
|
||||||
|
@ -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 {
|
||||||
|
val newContext = copy(
|
||||||
flowsUpdatesFilter = FlowsUpdatesFilter(),
|
flowsUpdatesFilter = FlowsUpdatesFilter(),
|
||||||
scope = CoroutineScope(scope.coroutineContext + SupervisorJob())
|
scope = this
|
||||||
).run {
|
)
|
||||||
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 {
|
||||||
|
val updatesScope = CoroutineScope(coroutineContext + SupervisorJob())
|
||||||
|
|
||||||
|
return doInSubContextWithFlowsUpdatesFilterSetup(
|
||||||
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
|
newFlowsUpdatesFilterSetUp = updatesFilter ?.let {
|
||||||
{ oldOne ->
|
{ oldOne ->
|
||||||
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
|
oldOne.allUpdatesFlow.filter { updatesFilter(it) }.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
|
||||||
}
|
}
|
||||||
} ?: { oldOne ->
|
} ?: { oldOne ->
|
||||||
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(this, asUpdateReceiver)
|
oldOne.allUpdatesFlow.subscribeSafelyWithoutExceptions(updatesScope, asUpdateReceiver)
|
||||||
},
|
},
|
||||||
stopOnCompletion,
|
stopOnCompletion
|
||||||
behaviourContextReceiver
|
) {
|
||||||
)
|
coroutineContext.job.invokeOnCompletion { updatesScope.cancel() }
|
||||||
|
behaviourContextReceiver()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
suspend fun <T> BehaviourContext.doInSubContext(
|
suspend fun <T> BehaviourContext.doInSubContext(
|
||||||
stopOnCompletion: Boolean = true,
|
stopOnCompletion: Boolean = true,
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user