1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 00:27:14 +00:00

improvements in CombinedSubcontextInitialAction

This commit is contained in:
InsanusMokrassar 2024-12-03 09:38:23 +06:00
parent 85539a9d58
commit 8b63ed7fa9

View File

@ -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,14 +32,30 @@ class CombinedSubcontextInitialAction(
} }
} }
val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = { update -> val subcontextInitialAction: CustomBehaviourContextAndTypeReceiver<BehaviourContext, Unit, Update> = { update ->
subactions.forEach { subaction -> val leftSubActions = subactions.toMutableSet()
with(subaction) { val successSubActions = mutableSetOf<SubAction>()
runCatching { while (leftSubActions.isNotEmpty()) {
invoke(update) leftSubActions.forEach { subaction ->
}.onFailure { with(subaction) {
logger.error("Unable to execute $subaction for update $update", it) runCatching {
invoke(update)
}.onFailure {
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()
} }
} }
} }