1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-24 19:18:44 +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> filter: suspend (Update) -> List<T>
): Flow<T> { ): Flow<T> {
val flow = allUpdatesFlow.map { val flow = allUpdatesFlow.map {
val result = safelyWithResult { filter(it) } val result = runCatching {
filter(it)
}
if (result.isFailure || result.getOrThrow().isEmpty()) { if (result.isFailure || result.getOrThrow().isEmpty()) {
if (cancelTrigger(it)) { if (cancelTrigger(it)) {
cancelRequestFactory(it) ?.also { cancelRequestFactory(it) ?.also {
@ -47,14 +49,20 @@ suspend fun <T> FlowsUpdatesFilter.expectFlow(
} }
} }
errorFactory(it) ?.also { errorRequest -> errorFactory(it) ?.also { errorRequest ->
safelyWithoutExceptions { bot.execute(errorRequest) } runCatching {
bot.execute(errorRequest)
}
} }
emptyList() emptyList()
} else { } else {
result.getOrThrow() result.getOrThrow()
} }
}.flatten() }.flatten()
initRequest ?.also { safelyWithoutExceptions { bot.execute(initRequest) } } initRequest ?.also {
runCatching {
bot.execute(initRequest)
}
}
return flow 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.ByChatMessageMarkerFactory
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.extensions.utils.whenCommonMessage 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.files.TelegramMediaFile
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.content.* 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) is BaseSentMessageUpdate -> it.data.whenCommonMessage(::listOfNotNull)
else -> null else -> null
} ?.mapNotNull { message -> } ?.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.extensions.behaviour_builder.utils.marker_factories.MarkerFactory
import dev.inmo.tgbotapi.types.update.abstracts.Update 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>?, markerFactory: MarkerFactory<in T, Any>?,
initialFilter: SimpleFilter<T>? = null, initialFilter: SimpleFilter<T>? = null,
noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, T, Update>? = null, subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, T, Update>? = null,
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>, scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>,
noinline updateToData: (Update) -> List<T>? updateToData: (Update) -> List<T>?
) = flowsUpdatesFilter.expectFlow( ) = flowsUpdatesFilter.expectFlow(
bot, bot,
filter = initialFilter ?.let { filter = initialFilter ?.let {
@ -24,17 +24,21 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
} ?: emptyList() } ?: emptyList()
} }
} ?: { } ?: {
updateToData(it) ?.mapNotNull { data -> updateToData(it) ?.map { data ->
it to data it to data
} ?: emptyList() } ?: emptyList()
} }
).run { ).run {
val handler: suspend (Pair<Update, T>) -> Unit = { (update, triggerData) -> val handler: suspend (Pair<Update, T>) -> Unit = subcontextUpdatesFilter ?.let {
createSubContextAndDoWithUpdatesFilter { { (update, triggerData) ->
if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) { createSubContextAndDoWithUpdatesFilter {
scenarioReceiver(triggerData) if (subcontextUpdatesFilter(this, triggerData, update)) {
scenarioReceiver(triggerData)
}
} }
} }
} ?: { (_, triggerData) ->
createSubContextAndDoWithUpdatesFilter(behaviourContextReceiver = { scenarioReceiver(triggerData) })
} }
markerFactory ?.let { markerFactory ?.let {
subscribeSafelyWithoutExceptionsAsync( subscribeSafelyWithoutExceptionsAsync(