port: Foreach: refactor from recursive to iterative implementation to keep stack depth constant (#6038)
The changes in Foreach: refactor from recursive to iterative implementation to keep stack depth constant (#6038) may need to be ported to maintain parity with microsoft/botbuilder-dotnet.
Fixes https://github.com/microsoft/botbuilder-dotnet/issues/6039Prior to this change, ForEach action is recursive, adding 180 frames to the dotnet call stack, resulting in foreach over large collections throwing StackOverflowException. This recursive nature is, in part, created by the nature of how DialogContext works.
This PR changes ForEach to be iterative. Inheriting from action scope makes it not possible to have an iterative version because when a foreach has multiple turns, continue goes straight to the active action and foreach cannot resume. Instead, we use a method similar to how adaptive calls actions, by creating a child dialog context and iterating.
Also, there were bugs for multi-turn that got fixed in this PR.
Before change: Stack depth = ~180 * ElementCount in collection. For 100 elements, stack depth = 180*100 = 18000 After change: Stack depth = < 203 in the same dataset. For 100 elements, stack depth = 203
Please review and, if necessary, port the changes.