mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
refactoring and fixes
This commit is contained in:
parent
3e58114eeb
commit
ad97008f12
@ -94,7 +94,7 @@ class DefaultBehaviourContextWithFSM<T : State>(
|
|||||||
private val additionalHandlers = mutableListOf<BehaviourWithFSMStateHandlerHolder<*, T>>()
|
private val additionalHandlers = mutableListOf<BehaviourWithFSMStateHandlerHolder<*, T>>()
|
||||||
private var actualHandlersList = additionalHandlers + handlers
|
private var actualHandlersList = additionalHandlers + handlers
|
||||||
|
|
||||||
private suspend fun getSubContext(context: Any) = updatesFlows.getOrPut(context) {
|
private fun getSubContext(context: Any) = updatesFlows.getOrPut(context) {
|
||||||
createSubContext()
|
createSubContext()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +113,7 @@ inline fun <T> BehaviourContext(
|
|||||||
crossinline block: BehaviourContext.() -> T
|
crossinline block: BehaviourContext.() -> T
|
||||||
) = DefaultBehaviourContext(bot, scope, upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow, triggersHolder = triggersHolder).run(block)
|
) = DefaultBehaviourContext(bot, scope, upstreamUpdatesFlow = flowsUpdatesFilter.allUpdatesFlow, triggersHolder = triggersHolder).run(block)
|
||||||
|
|
||||||
suspend fun <BC : BehaviourContext> BC.createSubContext(
|
fun <BC : BehaviourContext> BC.createSubContext(
|
||||||
scope: CoroutineScope = LinkedSupervisorScope(),
|
scope: CoroutineScope = LinkedSupervisorScope(),
|
||||||
triggersHolder: TriggersHolder = this.triggersHolder,
|
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow.accumulatorFlow(scope),
|
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow.accumulatorFlow(scope),
|
||||||
@ -132,20 +132,51 @@ suspend fun <BC : BehaviourContext> BC.createSubContext(
|
|||||||
) as BC
|
) as BC
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and
|
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
|
||||||
* [CoroutineScope] as new [BehaviourContext.scope]
|
*
|
||||||
|
* @param stopOnCompletion ___FALSE BY DEFAULT___. Will stop [this] in case if passed true
|
||||||
*/
|
*/
|
||||||
suspend fun <T, BC : BehaviourContext> BC.doInContext(
|
suspend fun <T, BC : BehaviourContext> BC.doInContext(
|
||||||
stopOnCompletion: Boolean = true,
|
stopOnCompletion: Boolean = false,
|
||||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||||
): T {
|
): T {
|
||||||
return behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
return withContext(coroutineContext) {
|
||||||
|
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates new one [BehaviourContext], adding subsequent [FlowsUpdatesFilter] in case [updatesFilter] is provided and
|
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
||||||
* [CoroutineScope] as new [BehaviourContext.scope]
|
* using [doInContext]
|
||||||
|
*
|
||||||
|
* @param stopOnCompletion ___TRUE BY DEFAULT___
|
||||||
*/
|
*/
|
||||||
|
suspend fun <T, BC : BehaviourContext> BC.createSubContextAndDoWithUpdatesFilter(
|
||||||
|
scope: CoroutineScope = LinkedSupervisorScope(),
|
||||||
|
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||||
|
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow.accumulatorFlow(scope),
|
||||||
|
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>? = null,
|
||||||
|
stopOnCompletion: Boolean = true,
|
||||||
|
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||||
|
): T {
|
||||||
|
return createSubContext(
|
||||||
|
scope,
|
||||||
|
triggersHolder,
|
||||||
|
updatesUpstreamFlow,
|
||||||
|
updatesFilter
|
||||||
|
).doInContext(
|
||||||
|
stopOnCompletion,
|
||||||
|
behaviourContextReceiver
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
||||||
|
* using [doInContext]
|
||||||
|
*
|
||||||
|
* @param stopOnCompletion ___TRUE BY DEFAULT___
|
||||||
|
*/
|
||||||
|
@Deprecated("Renamed", ReplaceWith("createSubContextAndDoWithUpdatesFilter", "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoWithUpdatesFilter"))
|
||||||
suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
|
suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
|
||||||
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>?,
|
updatesFilter: CustomBehaviourContextAndTypeReceiver<BC, Boolean, Update>?,
|
||||||
stopOnCompletion: Boolean = true,
|
stopOnCompletion: Boolean = true,
|
||||||
@ -165,13 +196,21 @@ suspend fun <T, BC : BehaviourContext> BC.doInSubContextWithUpdatesFilter(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Deprecated("Redundant", ReplaceWith("createSubContextAndDoWithUpdatesFilter", "dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContextAndDoWithUpdatesFilter"))
|
||||||
suspend fun <T> BehaviourContext.doInSubContext(
|
suspend fun <T> BehaviourContext.doInSubContext(
|
||||||
stopOnCompletion: Boolean = true,
|
stopOnCompletion: Boolean = true,
|
||||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
||||||
scope: CoroutineScope = LinkedSupervisorScope(),
|
scope: CoroutineScope = LinkedSupervisorScope(),
|
||||||
triggersHolder: TriggersHolder = this.triggersHolder,
|
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||||
behaviourContextReceiver: BehaviourContextReceiver<T>
|
behaviourContextReceiver: BehaviourContextReceiver<T>
|
||||||
) = doInSubContextWithUpdatesFilter(updatesFilter = null, stopOnCompletion, updatesUpstreamFlow, scope, triggersHolder, behaviourContextReceiver)
|
) = createSubContextAndDoWithUpdatesFilter(
|
||||||
|
scope,
|
||||||
|
triggersHolder,
|
||||||
|
updatesUpstreamFlow,
|
||||||
|
updatesFilter = null,
|
||||||
|
stopOnCompletion,
|
||||||
|
behaviourContextReceiver
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will cancel ALL subsequent contexts, expectations and waiters
|
* This method will cancel ALL subsequent contexts, expectations and waiters
|
||||||
|
@ -21,12 +21,9 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
|
|||||||
scope,
|
scope,
|
||||||
markerFactory::invoke
|
markerFactory::invoke
|
||||||
) { triggerData ->
|
) { triggerData ->
|
||||||
val scope = LinkedSupervisorScope()
|
createSubContextAndDoWithUpdatesFilter(
|
||||||
doInSubContextWithUpdatesFilter(
|
|
||||||
updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData),
|
updatesFilter = subcontextUpdatesFilter ?.toOneType(triggerData),
|
||||||
stopOnCompletion = false,
|
stopOnCompletion = false
|
||||||
updatesUpstreamFlow = allUpdatesFlow.accumulatorFlow(scope),
|
|
||||||
scope = scope
|
|
||||||
) {
|
) {
|
||||||
scenarioReceiver(triggerData)
|
scenarioReceiver(triggerData)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user