1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-02 22:59:48 +00:00
This commit is contained in:
2021-06-28 01:00:20 +06:00
parent 47fe048b10
commit 77dff639f0
7 changed files with 69 additions and 28 deletions

View File

@@ -1,7 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.weakLaunch
import dev.inmo.micro_utils.coroutines.*
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.FlowsUpdatesFilter
@@ -37,15 +36,18 @@ suspend fun <T> BehaviourContext.doInSubContextWithFlowsUpdatesFilterSetup(
newFlowsUpdatesFilterSetUp: BehaviourContextAndTypeReceiver<Unit, FlowsUpdatesFilter>?,
stopOnCompletion: Boolean = true,
behaviourContextReceiver: BehaviourContextReceiver<T>
): T = supervisorScope {
val newContext = copy(
): T {
return copy(
flowsUpdatesFilter = FlowsUpdatesFilter(),
scope = this
)
newFlowsUpdatesFilterSetUp ?.let {
it.apply { invoke(newContext, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) }
scope = LinkedSupervisorScope()
).run {
newFlowsUpdatesFilterSetUp ?.let {
it.apply { invoke(this@run, this@doInSubContextWithFlowsUpdatesFilterSetup.flowsUpdatesFilter) }
}
withContext(coroutineContext) {
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
}
}
newContext.behaviourContextReceiver().also { if (stopOnCompletion) stop() }
}
/**

View File

@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.micro_utils.coroutines.safelyWithResult
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
@@ -38,11 +39,11 @@ suspend fun <T> FlowsUpdatesFilter.expectFlow(
filter: suspend (Update) -> List<T>
): Flow<T> {
val flow = allUpdatesFlow.flatMapConcat {
val result = safelyWithoutExceptions { filter(it) }
(if (result == null || result.isEmpty()) {
val result = safelyWithResult { filter(it) }
(if (result.isFailure || result.getOrThrow().isEmpty()) {
if (cancelTrigger(it)) {
cancelRequestFactory(it) ?.also {
safelyWithoutExceptions { bot.execute(it) }
safelyWithResult { bot.execute(it) }
throw cancelledByFilterException
}
}
@@ -51,7 +52,7 @@ suspend fun <T> FlowsUpdatesFilter.expectFlow(
}
emptyList()
} else {
result
result.getOrThrow()
}).asFlow()
}
val result = if (count == null) {