1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 00:03:48 +00:00

rewrite createSubContextAndDoWithUpdatesFilter and doInContext to work with supervisor scope

This commit is contained in:
InsanusMokrassar 2024-06-13 21:32:04 +06:00
parent 1a8cfefa4d
commit 97836c5f4e
3 changed files with 14 additions and 23 deletions

View File

@ -36,10 +36,9 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourConte
public static final fun CustomBehaviourContextReceiver (Lkotlin/jvm/functions/Function2;)Lkotlin/jvm/functions/Function2;
public static final fun createSubContext (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlinx/coroutines/flow/Flow;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;
public static synthetic fun createSubContext$default (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlinx/coroutines/flow/Flow;ILjava/lang/Object;)Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;
public static final fun createSubContextAndDoWithUpdatesFilter (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlinx/coroutines/flow/Flow;ZLkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun createSubContextAndDoWithUpdatesFilter$default (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlinx/coroutines/CoroutineScope;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlinx/coroutines/flow/Flow;ZLkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun doInContext (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;ZLkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun doInContext$default (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;ZLkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun createSubContextAndDoWithUpdatesFilter (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun createSubContextAndDoWithUpdatesFilter$default (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;Lkotlinx/coroutines/flow/Flow;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public static final fun doInContext (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun stop (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;)V
}

View File

@ -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
return supervisorScope {
createSubContext(
scope = this,
triggersHolder = triggersHolder,
updatesUpstreamFlow = updatesUpstreamFlow
).doInContext(
stopOnCompletion,
behaviourContextReceiver
behaviourContextReceiver = behaviourContextReceiver
)
}
}
/**

View File

@ -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)
}