mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-22 08:37:12 +00:00
improvements in CombinedSubcontextInitialAction
This commit is contained in:
parent
85539a9d58
commit
8b63ed7fa9
@ -6,6 +6,13 @@ import dev.inmo.tgbotapi.types.update.abstracts.Update
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains [SubAction]s which will be used in [subcontextInitialAction] in order they has been passed in [subactions].
|
* Contains [SubAction]s which will be used in [subcontextInitialAction] in order they has been passed in [subactions].
|
||||||
|
*
|
||||||
|
* Its [subcontextInitialAction] will iterate over incoming [subactions] until all will be completed successfully OR
|
||||||
|
* none will be successful during round of running:
|
||||||
|
*
|
||||||
|
* * Run all
|
||||||
|
* * Exclude from current running successful items
|
||||||
|
* * Do again if there are some items to run
|
||||||
*/
|
*/
|
||||||
class CombinedSubcontextInitialAction(
|
class CombinedSubcontextInitialAction(
|
||||||
val subactions: List<SubAction>,
|
val subactions: List<SubAction>,
|
||||||
@ -25,15 +32,31 @@ class CombinedSubcontextInitialAction(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = { update ->
|
val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = { update ->
|
||||||
subactions.forEach { subaction ->
|
val leftSubActions = subactions.toMutableSet()
|
||||||
|
val successSubActions = mutableSetOf<SubAction>()
|
||||||
|
while (leftSubActions.isNotEmpty()) {
|
||||||
|
leftSubActions.forEach { subaction ->
|
||||||
with(subaction) {
|
with(subaction) {
|
||||||
runCatching {
|
runCatching {
|
||||||
invoke(update)
|
invoke(update)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
logger.error("Unable to execute $subaction for update $update", it)
|
logger.error(it) {
|
||||||
|
"Unable to execute $subaction for update $update. Will try on next round"
|
||||||
|
}
|
||||||
|
}.onSuccess {
|
||||||
|
successSubActions.add(subaction)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
leftSubActions.removeAll(successSubActions)
|
||||||
|
if (successSubActions.isEmpty()) {
|
||||||
|
logger.error {
|
||||||
|
"Some SubActions have been unable to complete successfully:${leftSubActions.joinToString("\n") { it.toString() }}"
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
successSubActions.clear()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user