tgbotapi/tgbotapi.behaviour_builder/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/behaviour_builder/expectations/WaitPollUpdates.kt

47 lines
1.6 KiB
Kotlin
Raw Normal View History

2021-10-03 14:37:14 +00:00
package dev.inmo.tgbotapi.extensions.behaviour_builder.expectations
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
2022-08-05 10:31:39 +00:00
import dev.inmo.tgbotapi.extensions.utils.pollUpdateOrNull
2021-10-03 14:37:14 +00:00
import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.types.polls.*
import dev.inmo.tgbotapi.utils.RiskFeature
import dev.inmo.tgbotapi.utils.lowLevelRiskFeatureMessage
import kotlinx.coroutines.flow.Flow
2021-10-03 14:37:14 +00:00
typealias PollMapper<T> = suspend T.() -> T?
@RiskFeature(lowLevelRiskFeatureMessage)
suspend inline fun <reified O : Poll> BehaviourContext.waitPolls(
2021-10-03 14:37:14 +00:00
initRequest: Request<*>? = null,
noinline errorFactory: NullableRequestBuilder<*> = { null }
): Flow<O> = expectFlow(
2021-10-03 14:37:14 +00:00
initRequest,
errorFactory
) {
2022-08-05 10:31:39 +00:00
(it.pollUpdateOrNull() ?.data as? O).let(::listOfNotNull)
}
2021-10-03 14:37:14 +00:00
/**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot
*/
suspend fun BehaviourContext.waitPollUpdates(
2021-10-03 14:37:14 +00:00
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }
) = waitPolls<Poll>(initRequest, errorFactory)
2021-10-03 14:37:14 +00:00
/**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot
*/
suspend fun BehaviourContext.waitQuizPollUpdates(
2021-10-03 14:37:14 +00:00
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }
) = waitPolls<QuizPoll>(initRequest, errorFactory)
2021-10-03 14:37:14 +00:00
/**
* This wait will be triggered only for stopped polls and polls, which are sent by the bot
*/
suspend fun BehaviourContext.waitRegularPollUpdates(
2021-10-03 14:37:14 +00:00
initRequest: Request<*>? = null,
errorFactory: NullableRequestBuilder<*> = { null }
) = waitPolls<RegularPoll>(initRequest, errorFactory)