From b933def16d165e5ac154569168c5d1b6067a8a2b Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Fri, 26 Jan 2024 21:21:21 +0100 Subject: [PATCH] Simplify iteration logic in ForEachT.cs Removed the ItemSource property and related logic in ForEachT.cs. Instead of considering two sources (Items and ItemSource) for iterations, the code now only iterates over the Items collection. Additional logic has been added to initialize Items to an empty collection when it is not set. --- .../Activities/ForEachT.cs | 47 ++----------------- 1 file changed, 5 insertions(+), 42 deletions(-) diff --git a/src/modules/Elsa.Workflows.Core/Activities/ForEachT.cs b/src/modules/Elsa.Workflows.Core/Activities/ForEachT.cs index 1ef5e49ea..c954f50b7 100644 --- a/src/modules/Elsa.Workflows.Core/Activities/ForEachT.cs +++ b/src/modules/Elsa.Workflows.Core/Activities/ForEachT.cs @@ -47,13 +47,7 @@ public class ForEach : Activity /// The set of values to iterate. /// [Input(Description = "The set of values to iterate.")] - public Input>? Items { get; set; } - - /// - /// The source of values to iterate. - /// - [Input(Description = "The set of values to iterate.")] - public Input>? ItemSource { get; set; } + public Input> Items { get; set; } = new(Array.Empty()); /// /// The activity to execute for each iteration. @@ -85,16 +79,15 @@ public class ForEach : Activity } var currentIndex = context.GetProperty(CurrentIndexProperty); - var currentValueTuple = await GetCurrentValueAsync(context, currentIndex); + var items = context.Get(Items)!.ToList(); - if (!currentValueTuple.Exists) + if (currentIndex >= items.Count) { await context.CompleteActivityAsync(); return; } - - var currentValue = currentValueTuple.Value; - + + var currentValue = items[currentIndex]; context.Set(CurrentValue, currentValue); if (Body != null) @@ -113,36 +106,6 @@ public class ForEach : Activity context.UpdateProperty(CurrentIndexProperty, x => x + 1); } - private async Task<(T Value, bool Exists)> GetCurrentValueAsync(ActivityExecutionContext context, int currentIndex) - { - var items = context.Get(Items)?.ToList(); - - if (items != null) - { - return (currentIndex >= items.Count ? (default, false) : (items[currentIndex], true))!; - } - - var itemSource = context.Get(ItemSource); - - if(itemSource != null) - { - await using var enumerator = itemSource.GetAsyncEnumerator(); - - // Move the cursor to the current index. - for (var i = 0; i < currentIndex; i++) - await enumerator.MoveNextAsync(); - - var hasNext = await enumerator.MoveNextAsync(); - - if(!hasNext) - return (default, false)!; - - return (enumerator.Current, true); - } - - return (default, false)!; - } - private async ValueTask OnChildCompleted(ActivityCompletedContext context) { await HandleIteration(context.TargetContext);