mirror of
				https://github.com/InsanusMokrassar/TelegramBotAPI.git
				synced 2025-10-25 17:20:07 +00:00 
			
		
		
		
	improvements in CombinedSubcontextInitialAction
This commit is contained in:
		| @@ -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() | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user