| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | using Elsa.Workflows.Signals; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Activities; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Execute a set of activities in sequence. |
| | | 11 | | /// </summary> |
| | | 12 | | [Category("Workflows")] |
| | | 13 | | [Activity("Elsa", "Workflows", "Execute a set of activities in sequence.")] |
| | | 14 | | [PublicAPI] |
| | | 15 | | [Browsable(false)] |
| | | 16 | | public class Sequence : Container |
| | | 17 | | { |
| | | 18 | | private const string CurrentIndexProperty = "CurrentIndex"; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | 4031 | 21 | | public Sequence([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 22 | | { |
| | 4031 | 23 | | OnSignalReceived<BreakSignal>(OnBreakSignalReceived); |
| | 4031 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | protected override async ValueTask ScheduleChildrenAsync(ActivityExecutionContext context) |
| | | 28 | | { |
| | 106 | 29 | | await HandleItemAsync(context); |
| | 106 | 30 | | } |
| | | 31 | | |
| | | 32 | | private async ValueTask HandleItemAsync(ActivityExecutionContext context) |
| | | 33 | | { |
| | 269 | 34 | | var currentIndex = context.GetProperty<int>(CurrentIndexProperty); |
| | 269 | 35 | | var childActivities = Activities.ToList(); |
| | | 36 | | |
| | 269 | 37 | | if (currentIndex >= childActivities.Count) |
| | | 38 | | { |
| | 68 | 39 | | await context.CompleteActivityAsync(); |
| | 68 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 201 | 43 | | var nextActivity = childActivities.ElementAt(currentIndex); |
| | 201 | 44 | | await context.ScheduleActivityAsync(nextActivity, OnChildCompleted); |
| | 402 | 45 | | context.UpdateProperty<int>(CurrentIndexProperty, x => x + 1); |
| | 269 | 46 | | } |
| | | 47 | | |
| | | 48 | | private async ValueTask OnChildCompleted(ActivityCompletedContext context) |
| | | 49 | | { |
| | 175 | 50 | | var targetContext = context.TargetContext; |
| | 175 | 51 | | var childContext = context.ChildContext; |
| | 175 | 52 | | var isBreaking = targetContext.GetIsBreaking(); |
| | 175 | 53 | | var completedActivity = childContext.Activity; |
| | | 54 | | |
| | | 55 | | // If the complete activity is a terminal node, complete the sequence immediately. |
| | 175 | 56 | | if (isBreaking || completedActivity is ITerminalNode) |
| | | 57 | | { |
| | 12 | 58 | | await targetContext.CompleteActivityAsync(); |
| | 12 | 59 | | return; |
| | | 60 | | } |
| | | 61 | | |
| | 163 | 62 | | await HandleItemAsync(targetContext); |
| | 175 | 63 | | } |
| | | 64 | | |
| | | 65 | | private void OnBreakSignalReceived(BreakSignal signal, SignalContext signalContext) |
| | | 66 | | { |
| | 0 | 67 | | signalContext.ReceiverActivityExecutionContext.SetIsBreaking(); |
| | 0 | 68 | | } |
| | | 69 | | } |