mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-16 13:23:47 +00:00
rewrite createSubContextAndDoWithUpdatesFilter and doInContext to work with supervisor scope
This commit is contained in:
parent
1a8cfefa4d
commit
97836c5f4e
@ -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 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 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 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 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;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 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;ZLkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)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 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 stop (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;)V
|
public static final fun stop (Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContext;)V
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,40 +132,34 @@ fun <BC : BehaviourContext> BC.createSubContext(
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Launch [behaviourContextReceiver] in context of [this] as [BehaviourContext] and as [kotlin.coroutines.CoroutineContext]
|
* 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(
|
suspend fun <T, BC : BehaviourContext> BC.doInContext(
|
||||||
stopOnCompletion: Boolean = false,
|
|
||||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||||
): T {
|
): T {
|
||||||
return withContext(coroutineContext) {
|
return withContext(coroutineContext) {
|
||||||
behaviourContextReceiver().also { if (stopOnCompletion) stop() }
|
behaviourContextReceiver()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
* Creates new one [BehaviourContext] using [createSubContext] and launches [behaviourContextReceiver] in a new context
|
||||||
* using [doInContext]
|
* using [doInContext]
|
||||||
*
|
|
||||||
* @param stopOnCompletion ___TRUE BY DEFAULT___
|
|
||||||
*/
|
*/
|
||||||
suspend fun <T, BC : BehaviourContext> BC.createSubContextAndDoWithUpdatesFilter(
|
suspend fun <T, BC : BehaviourContext> BC.createSubContextAndDoWithUpdatesFilter(
|
||||||
scope: CoroutineScope = LinkedSupervisorScope(),
|
|
||||||
triggersHolder: TriggersHolder = this.triggersHolder,
|
triggersHolder: TriggersHolder = this.triggersHolder,
|
||||||
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
updatesUpstreamFlow: Flow<Update> = allUpdatesFlow,
|
||||||
stopOnCompletion: Boolean = true,
|
|
||||||
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
behaviourContextReceiver: CustomBehaviourContextReceiver<BC, T>
|
||||||
): T {
|
): T {
|
||||||
return createSubContext(
|
return supervisorScope {
|
||||||
scope,
|
createSubContext(
|
||||||
triggersHolder,
|
scope = this,
|
||||||
updatesUpstreamFlow
|
triggersHolder = triggersHolder,
|
||||||
|
updatesUpstreamFlow = updatesUpstreamFlow
|
||||||
).doInContext(
|
).doInContext(
|
||||||
stopOnCompletion,
|
behaviourContextReceiver = behaviourContextReceiver
|
||||||
behaviourContextReceiver
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method will cancel ALL subsequent contexts, expectations and waiters
|
* This method will cancel ALL subsequent contexts, expectations and waiters
|
||||||
|
@ -30,9 +30,7 @@ internal suspend inline fun <BC : BehaviourContext, reified T> BC.on(
|
|||||||
scope,
|
scope,
|
||||||
{ markerFactory(it.second) }
|
{ markerFactory(it.second) }
|
||||||
) { (update, triggerData) ->
|
) { (update, triggerData) ->
|
||||||
createSubContextAndDoWithUpdatesFilter(
|
createSubContextAndDoWithUpdatesFilter {
|
||||||
stopOnCompletion = false
|
|
||||||
) {
|
|
||||||
if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) {
|
if (subcontextUpdatesFilter ?.invoke(this, triggerData, update) != false) {
|
||||||
scenarioReceiver(triggerData)
|
scenarioReceiver(triggerData)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user