mirror of
				https://github.com/InsanusMokrassar/TelegramBotAPI.git
				synced 2025-11-04 06:00:15 +00:00 
			
		
		
		
	improvements in data filling
This commit is contained in:
		@@ -4,6 +4,7 @@ import dev.inmo.micro_utils.coroutines.ContextSafelyExceptionHandler
 | 
			
		||||
import dev.inmo.micro_utils.coroutines.ExceptionHandler
 | 
			
		||||
import dev.inmo.tgbotapi.bot.TelegramBot
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.DefaultCustomBehaviourContextAndTypeReceiver
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.optionallyWithDefaultReceiver
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingOfUpdatesByLongPolling
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.updateHandlerWithMediaGroupsAdaptation
 | 
			
		||||
@@ -44,11 +45,8 @@ suspend fun TelegramBot.buildBehaviour(
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
    flowsUpdatesFilter = flowUpdatesFilter,
 | 
			
		||||
    subcontextInitialAction = if (useDefaultSubcontextInitialAction) {
 | 
			
		||||
        DefaultCustomBehaviourContextAndTypeReceiver(subcontextInitialAction)
 | 
			
		||||
    } else {
 | 
			
		||||
        subcontextInitialAction
 | 
			
		||||
    }
 | 
			
		||||
    useDefaultSubcontextInitialAction = useDefaultSubcontextInitialAction,
 | 
			
		||||
    subcontextInitialAction = subcontextInitialAction
 | 
			
		||||
).apply {
 | 
			
		||||
    block()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,6 @@
 | 
			
		||||
 | 
			
		||||
package dev.inmo.tgbotapi.extensions.behaviour_builder
 | 
			
		||||
 | 
			
		||||
import dev.inmo.kslog.common.KSLog
 | 
			
		||||
import dev.inmo.micro_utils.coroutines.*
 | 
			
		||||
import dev.inmo.tgbotapi.bot.TelegramBot
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.handlers_registrar.TriggersHolder
 | 
			
		||||
@@ -84,6 +83,7 @@ class DefaultBehaviourContext(
 | 
			
		||||
    onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND,
 | 
			
		||||
    private val upstreamUpdatesFlow: Flow<Update>? = null,
 | 
			
		||||
    override val triggersHolder: TriggersHolder = TriggersHolder(),
 | 
			
		||||
    override val data: BehaviourContextData = BehaviourContextData(),
 | 
			
		||||
    override val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {}
 | 
			
		||||
) : AbstractFlowsUpdatesFilter(), TelegramBot by bot, CoroutineScope by scope, BehaviourContext {
 | 
			
		||||
 | 
			
		||||
@@ -108,8 +108,6 @@ class DefaultBehaviourContext(
 | 
			
		||||
    }.accumulatorFlow(WeakScope(scope))
 | 
			
		||||
    override val asUpdateReceiver: UpdateReceiver<Update> = additionalUpdatesSharedFlow::emit
 | 
			
		||||
 | 
			
		||||
    override val data: BehaviourContextData = BehaviourContextData()
 | 
			
		||||
 | 
			
		||||
    override fun copy(
 | 
			
		||||
        bot: TelegramBot,
 | 
			
		||||
        scope: CoroutineScope,
 | 
			
		||||
@@ -138,13 +136,17 @@ fun BehaviourContext(
 | 
			
		||||
    triggersHolder: TriggersHolder = TriggersHolder(),
 | 
			
		||||
    useDefaultSubcontextInitialAction: Boolean = true,
 | 
			
		||||
    subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {}
 | 
			
		||||
) = DefaultBehaviourContext(
 | 
			
		||||
    bot = bot,
 | 
			
		||||
    scope = scope,
 | 
			
		||||
    upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
 | 
			
		||||
    triggersHolder = triggersHolder,
 | 
			
		||||
    subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction)
 | 
			
		||||
)
 | 
			
		||||
): DefaultBehaviourContext {
 | 
			
		||||
    val data = BehaviourContextData()
 | 
			
		||||
    return DefaultBehaviourContext(
 | 
			
		||||
        bot = bot,
 | 
			
		||||
        scope = scope,
 | 
			
		||||
        upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
 | 
			
		||||
        triggersHolder = triggersHolder,
 | 
			
		||||
        data = data,
 | 
			
		||||
        subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline fun <T> BehaviourContext(
 | 
			
		||||
    bot: TelegramBot,
 | 
			
		||||
@@ -154,13 +156,56 @@ inline fun <T> BehaviourContext(
 | 
			
		||||
    noinline subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {},
 | 
			
		||||
    useDefaultSubcontextInitialAction: Boolean = true,
 | 
			
		||||
    crossinline block: BehaviourContext.() -> T
 | 
			
		||||
) = DefaultBehaviourContext(
 | 
			
		||||
    bot = bot,
 | 
			
		||||
    scope = scope,
 | 
			
		||||
    upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
 | 
			
		||||
    triggersHolder = triggersHolder,
 | 
			
		||||
    subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction)
 | 
			
		||||
).run(block)
 | 
			
		||||
): T {
 | 
			
		||||
    val data = BehaviourContextData()
 | 
			
		||||
    return DefaultBehaviourContext(
 | 
			
		||||
        bot = bot,
 | 
			
		||||
        scope = scope,
 | 
			
		||||
        upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow,
 | 
			
		||||
        triggersHolder = triggersHolder,
 | 
			
		||||
        data = data,
 | 
			
		||||
        subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
 | 
			
		||||
    ).run(block)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline fun <T> BehaviourContext(
 | 
			
		||||
    bot: TelegramBot,
 | 
			
		||||
    scope: CoroutineScope,
 | 
			
		||||
    upstreamUpdatesFlow: Flow<Update>,
 | 
			
		||||
    triggersHolder: TriggersHolder = TriggersHolder(),
 | 
			
		||||
    noinline subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = {},
 | 
			
		||||
    useDefaultSubcontextInitialAction: Boolean = true,
 | 
			
		||||
    crossinline block: BehaviourContext.() -> T
 | 
			
		||||
): T {
 | 
			
		||||
    val data = BehaviourContextData()
 | 
			
		||||
    return DefaultBehaviourContext(
 | 
			
		||||
        bot = bot,
 | 
			
		||||
        scope = scope,
 | 
			
		||||
        upstreamUpdatesFlow = upstreamUpdatesFlow,
 | 
			
		||||
        triggersHolder = triggersHolder,
 | 
			
		||||
        data = data,
 | 
			
		||||
        subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
 | 
			
		||||
    ).run(block)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fun BehaviourContext(
 | 
			
		||||
    bot: TelegramBot,
 | 
			
		||||
    scope: CoroutineScope,
 | 
			
		||||
    upstreamUpdatesFlow: Flow<Update>,
 | 
			
		||||
    subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update>,
 | 
			
		||||
    triggersHolder: TriggersHolder = TriggersHolder(),
 | 
			
		||||
    useDefaultSubcontextInitialAction: Boolean = true
 | 
			
		||||
): DefaultBehaviourContext {
 | 
			
		||||
    val data = BehaviourContextData()
 | 
			
		||||
    return DefaultBehaviourContext(
 | 
			
		||||
        bot = bot,
 | 
			
		||||
        scope = scope,
 | 
			
		||||
        upstreamUpdatesFlow = upstreamUpdatesFlow,
 | 
			
		||||
        triggersHolder = triggersHolder,
 | 
			
		||||
        data = data,
 | 
			
		||||
        subcontextInitialAction = subcontextInitialAction.optionallyWithDefaultReceiver(useDefaultSubcontextInitialAction, data)
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Creates new [BehaviourContext] using its [BehaviourContext.copy] method
 | 
			
		||||
@@ -183,7 +228,7 @@ fun <BC : BehaviourContext> BC.createSubContext(
 | 
			
		||||
) as BC
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
 | 
			
		||||
 * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [CoroutineContext]
 | 
			
		||||
 *
 | 
			
		||||
 * [this] [BehaviourContext] will **NOT** be closed automatically
 | 
			
		||||
 */
 | 
			
		||||
@@ -196,7 +241,7 @@ suspend fun <T, BC : BehaviourContext> BC.doInContext(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
 | 
			
		||||
 * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [CoroutineContext]
 | 
			
		||||
 *
 | 
			
		||||
 * [this] [BehaviourContext] will **NOT** be closed automatically
 | 
			
		||||
 */
 | 
			
		||||
@@ -218,7 +263,7 @@ suspend fun <T, BC : BehaviourContext> BC.doInNewSubContext(
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
 | 
			
		||||
 * Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [CoroutineContext]
 | 
			
		||||
 *
 | 
			
		||||
 * [this] [BehaviourContext] will **NOT** be closed automatically
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.extensions.behaviour_builder.utils
 | 
			
		||||
 | 
			
		||||
import dev.inmo.micro_utils.common.Warning
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.CustomBehaviourContextAndTypeReceiver
 | 
			
		||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.DefaultCustomBehaviourContextAndTypeReceiver.Companion.BOT_INFO_RECEIVER
 | 
			
		||||
import dev.inmo.tgbotapi.requests.bot.GetMe
 | 
			
		||||
@@ -28,12 +29,17 @@ suspend fun BehaviourContext.botInfo(): ExtendedBot? {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Warning("It is internal API and can be changed without notes")
 | 
			
		||||
fun <BC : BehaviourContext, R, U : Update> CustomBehaviourContextAndTypeReceiver<BC, R, U>.withDefaultReceiver() = DefaultCustomBehaviourContextAndTypeReceiver(this)
 | 
			
		||||
fun <BC : BehaviourContext, R, U : Update> CustomBehaviourContextAndTypeReceiver<BC, R, U>.withDefaultReceiver(
 | 
			
		||||
    data: BehaviourContextData
 | 
			
		||||
) = DefaultCustomBehaviourContextAndTypeReceiver(this).also {
 | 
			
		||||
    it(data)
 | 
			
		||||
}
 | 
			
		||||
@Warning("It is internal API and can be changed without notes")
 | 
			
		||||
fun <BC : BehaviourContext, R, U : Update> CustomBehaviourContextAndTypeReceiver<BC, R, U>.optionallyWithDefaultReceiver(
 | 
			
		||||
    include: Boolean
 | 
			
		||||
    include: Boolean,
 | 
			
		||||
    data: BehaviourContextData
 | 
			
		||||
) = if (include) {
 | 
			
		||||
    withDefaultReceiver()
 | 
			
		||||
    withDefaultReceiver(data)
 | 
			
		||||
} else {
 | 
			
		||||
    this
 | 
			
		||||
}
 | 
			
		||||
@@ -81,6 +87,10 @@ class DefaultCustomBehaviourContextAndTypeReceiver<BC : BehaviourContext, R, U :
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    operator fun invoke(data: BehaviourContextData) {
 | 
			
		||||
        data[BOT_INFO_RECEIVER] = internalReceiver
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Registers the internal bot info provider in [BehaviourContext.data] and then delegates to [wrapperReceiver].
 | 
			
		||||
     *
 | 
			
		||||
@@ -88,7 +98,7 @@ class DefaultCustomBehaviourContextAndTypeReceiver<BC : BehaviourContext, R, U :
 | 
			
		||||
     * the wrapped behaviour.
 | 
			
		||||
     */
 | 
			
		||||
    override suspend fun invoke(p1: BC, p2: U): R {
 | 
			
		||||
        p1.data[BOT_INFO_RECEIVER] = internalReceiver
 | 
			
		||||
        invoke(p1.data)
 | 
			
		||||
 | 
			
		||||
        return wrapperReceiver(p1, p2)
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user