2021-09-13 17:30:01 +00:00
|
|
|
package dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling
|
|
|
|
|
2022-08-05 15:44:36 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptionsAsync
|
2021-09-13 17:30:01 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.expectFlow
|
|
|
|
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
|
|
|
|
|
2021-10-13 08:22:01 +00:00
|
|
|
internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
|
2021-09-13 17:30:01 +00:00
|
|
|
markerFactory: MarkerFactory<in T, Any>,
|
2022-04-21 09:42:32 +00:00
|
|
|
initialFilter: SimpleFilter<T>? = null,
|
2021-10-13 08:22:01 +00:00
|
|
|
noinline subcontextUpdatesFilter: CustomBehaviourContextAndTwoTypesReceiver<BC, Boolean, T, Update>? = null,
|
|
|
|
noinline scenarioReceiver: CustomBehaviourContextAndTypeReceiver<BC, Unit, T>,
|
2021-09-13 17:30:01 +00:00
|
|
|
noinline updateToData: (Update) -> List<T>?
|
|
|
|
) = flowsUpdatesFilter.expectFlow(bot) {
|
|
|
|
updateToData(it) ?.mapNotNull { data ->
|
2022-05-21 15:44:27 +00:00
|
|
|
if (initialFilter ?.invoke(data) != false) it to data else null
|
2021-09-13 17:30:01 +00:00
|
|
|
} ?: emptyList()
|
|
|
|
}.subscribeSafelyWithoutExceptionsAsync(
|
|
|
|
scope,
|
2022-05-21 15:44:27 +00:00
|
|
|
{ markerFactory(it.second) }
|
|
|
|
) { (update, triggerData) ->
|
2022-05-11 04:56:31 +00:00
|
|
|
createSubContextAndDoWithUpdatesFilter(
|
|
|
|
stopOnCompletion = false
|
2021-09-13 17:30:01 +00:00
|
|
|
) {
|
2022-05-21 15:44:27 +00:00
|
|
|
if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) {
|
|
|
|
scenarioReceiver(triggerData)
|
|
|
|
}
|
2021-09-13 17:30:01 +00:00
|
|
|
}
|
|
|
|
}
|