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

rewrite MainTrigger as noinline

This commit is contained in:
InsanusMokrassar 2024-06-15 12:36:14 +06:00
parent 60fdbb8ac4
commit 46c5c62e15
3 changed files with 26 additions and 13 deletions

View File

@ -38,7 +38,9 @@ suspend fun <T> FlowsUpdatesFilter.expectFlow(
filter: suspend (Update) -> List<T>
): Flow<T> {
val flow = allUpdatesFlow.map {
val result = safelyWithResult { filter(it) }
val result = runCatching {
filter(it)
}
if (result.isFailure || result.getOrThrow().isEmpty()) {
if (cancelTrigger(it)) {
cancelRequestFactory(it) ?.also {
@ -47,14 +49,20 @@ suspend fun <T> FlowsUpdatesFilter.expectFlow(
}
}
errorFactory(it) ?.also { errorRequest ->
safelyWithoutExceptions { bot.execute(errorRequest) }
runCatching {
bot.execute(errorRequest)
}
}
emptyList()
} else {
result.getOrThrow()
}
}.flatten()
initRequest ?.also { safelyWithoutExceptions { bot.execute(initRequest) } }
initRequest ?.also {
runCatching {
bot.execute(initRequest)
}
}
return flow
}

View File

@ -9,6 +9,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.whenCommonMessage
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
import dev.inmo.tgbotapi.types.files.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.*
@ -27,7 +28,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T : MessageContent>
is BaseSentMessageUpdate -> it.data.whenCommonMessage(::listOfNotNull)
else -> null
} ?.mapNotNull { message ->
if (message.content is T) message as CommonMessage<T> else null
message.withContentOrNull<T>()
}
}

View File

@ -9,12 +9,12 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.types.update.abstracts.Update
internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
internal suspend fun <BC : BehaviourContext, T> BC.on(
markerFactory: MarkerFactory<in T, Any>?,
initialFilter: SimpleFilter<T>? = null,
noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, T, Update>? = null,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>,
noinline updateToData: (Update) -> List<T>?
subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, T, Update>? = null,
scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>,
updateToData: (Update) -> List<T>?
) = flowsUpdatesFilter.expectFlow(
bot,
filter = initialFilter ?.let {
@ -24,17 +24,21 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
} ?: emptyList()
}
} ?: {
updateToData(it) ?.mapNotNull { data ->
updateToData(it) ?.map { data ->
it to data
} ?: emptyList()
}
).run {
val handler: suspend (Pair<Update, T>) -> Unit = { (update, triggerData) ->
createSubContextAndDoWithUpdatesFilter {
if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) {
scenarioReceiver(triggerData)
val handler: suspend (Pair<Update, T>) -> Unit = subcontextUpdatesFilter ?.let {
{ (update, triggerData) ->
createSubContextAndDoWithUpdatesFilter {
if (subcontextUpdatesFilter(this, triggerData, update)) {
scenarioReceiver(triggerData)
}
}
}
} ?: { (_, triggerData) ->
createSubContextAndDoWithUpdatesFilter(behaviourContextReceiver = { scenarioReceiver(triggerData) })
}
markerFactory ?.let {
subscribeSafelyWithoutExceptionsAsync(