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