mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-11-16 03:50:24 +00:00
rewrite createSubContextAndDoWithUpdatesFilter and doInContext to work with supervisor scope
This commit is contained in:
@@ -132,39 +132,33 @@ fun <BC : BehaviourContext> BC.createSubContext(
|
||||
|
||||
/**
|
||||
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
|
||||
*
|
||||
* @param stopOnCompletion ___FALSE BY DEFAULT___. Will stop [this] in case if passed true
|
||||
*/
|
||||
suspend fun <T, BC : BehaviourContext> BC.doInContext(
|
||||
stopOnCompletion: Boolean = false,
|
||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||
): T {
|
||||
return withContext(coroutineContext) {
|
||||
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
||||
behaviourContextReceiver()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
||||
* 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,
|
||||
stopOnCompletion: Boolean = true,
|
||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||
): T {
|
||||
return createSubContext(
|
||||
scope,
|
||||
triggersHolder,
|
||||
updatesUpstreamFlow
|
||||
).doInContext(
|
||||
stopOnCompletion,
|
||||
behaviourContextReceiver
|
||||
)
|
||||
return supervisorScope {
|
||||
createSubContext(
|
||||
scope = this,
|
||||
triggersHolder = triggersHolder,
|
||||
updatesUpstreamFlow = updatesUpstreamFlow
|
||||
).doInContext(
|
||||
behaviourContextReceiver = behaviourContextReceiver
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,9 +30,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
|
||||
scope,
|
||||
{ markerFactory(it.second) }
|
||||
) { (update, triggerData) ->
|
||||
createSubContextAndDoWithUpdatesFilter(
|
||||
stopOnCompletion = false
|
||||
) {
|
||||
createSubContextAndDoWithUpdatesFilter {
|
||||
if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) {
|
||||
scenarioReceiver(triggerData)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user