| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | using Elsa.Workflows.Behaviors; |
| | | 6 | | using Elsa.Workflows.Memory; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Activities; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// A strongly-typed for-each construct where <typeparamref name="T"/> is the item type. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <typeparam name="T">The type of items in the collection to iterate over.</typeparam> |
| | | 15 | | public class ForEach<T> : Activity |
| | | 16 | | { |
| | | 17 | | private const string CurrentIndexProperty = "CurrentIndex"; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | 0 | 20 | | public ForEach(Func<ExpressionExecutionContext, ICollection<T>> @delegate, [CallerFilePath] string? source = null, [ |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | 0 | 25 | | public ForEach(Func<ICollection<T>> @delegate, [CallerFilePath] string? source = null, [CallerLineNumber] int? line |
| | | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | 25 | 30 | | public ForEach(ICollection<T> items, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : |
| | | 31 | | { |
| | 25 | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <inheritdoc /> |
| | 28 | 35 | | public ForEach(Input<ICollection<T>> items, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = n |
| | | 36 | | { |
| | 28 | 37 | | Items = items; |
| | 28 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | 28 | 41 | | public ForEach([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 42 | | { |
| | 28 | 43 | | Behaviors.Add<BreakBehavior>(this); |
| | 28 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// The set of values to iterate. |
| | | 48 | | /// </summary> |
| | | 49 | | [Input(Description = "The set of values to iterate.")] |
| | 148 | 50 | | public Input<ICollection<T>> Items { get; set; } = new(Array.Empty<T>()); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// The activity to execute for each iteration. |
| | | 54 | | /// </summary> |
| | | 55 | | [Port] |
| | 139 | 56 | | public IActivity? Body { get; set; } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// The current value being iterated will be assigned to the specified <see cref="MemoryBlockReference"/>. |
| | | 60 | | /// </summary> |
| | | 61 | | [Output(Description = "Assign the current value to the specified variable.")] |
| | 72 | 62 | | public Output<T>? CurrentValue { get; set; } |
| | | 63 | | |
| | | 64 | | /// <inheritdoc /> |
| | | 65 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 66 | | { |
| | | 67 | | // Execute first iteration. |
| | 11 | 68 | | await HandleIteration(context); |
| | 10 | 69 | | } |
| | | 70 | | |
| | | 71 | | private async Task HandleIteration(ActivityExecutionContext context) |
| | | 72 | | { |
| | 35 | 73 | | var isBreaking = context.GetIsBreaking(); |
| | | 74 | | |
| | 35 | 75 | | if (isBreaking) |
| | | 76 | | { |
| | 1 | 77 | | await context.CompleteActivityAsync(); |
| | 1 | 78 | | return; |
| | | 79 | | } |
| | | 80 | | |
| | 34 | 81 | | var currentIndex = context.GetProperty<int>(CurrentIndexProperty); |
| | 34 | 82 | | var items = context.Get(Items)!.ToList(); |
| | | 83 | | |
| | 33 | 84 | | if (currentIndex >= items.Count) |
| | | 85 | | { |
| | 8 | 86 | | await context.CompleteActivityAsync(); |
| | 8 | 87 | | return; |
| | | 88 | | } |
| | | 89 | | |
| | 25 | 90 | | var currentValue = items[currentIndex]; |
| | 25 | 91 | | context.Set(CurrentValue, currentValue); |
| | | 92 | | |
| | 25 | 93 | | if (Body != null) |
| | | 94 | | { |
| | 25 | 95 | | var variables = new[] |
| | 25 | 96 | | { |
| | 25 | 97 | | new Variable("CurrentIndex", currentIndex), |
| | 25 | 98 | | new Variable("CurrentValue", currentValue) |
| | 25 | 99 | | }; |
| | 25 | 100 | | await context.ScheduleActivityAsync(Body, OnChildCompleted, variables: variables); |
| | | 101 | | } |
| | | 102 | | else |
| | 0 | 103 | | await context.CompleteActivityAsync(); |
| | | 104 | | |
| | | 105 | | // Increment index. |
| | 50 | 106 | | context.UpdateProperty<int>(CurrentIndexProperty, x => x + 1); |
| | 34 | 107 | | } |
| | | 108 | | |
| | | 109 | | private async ValueTask OnChildCompleted(ActivityCompletedContext context) |
| | | 110 | | { |
| | 24 | 111 | | await HandleIteration(context.TargetContext); |
| | 24 | 112 | | } |
| | | 113 | | } |