< Summary

Information
Class: Elsa.Workflows.Activities.Sequence
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Sequence.cs
Line coverage
67%
Covered lines: 29
Uncovered lines: 14
Coverable lines: 43
Total lines: 98
Line coverage: 67.4%
Branch coverage
50%
Covered branches: 8
Total branches: 16
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ScheduleChildrenAsync()100%11100%
HandleItemAsync()100%44100%
OnChildCompleted()100%44100%
OnScheduleChildActivityAsync()0%7280%
OnBreakSignalReceived(...)100%210%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Activities/Sequence.cs

#LineLine coverage
 1using System.ComponentModel;
 2using System.Runtime.CompilerServices;
 3using Elsa.Workflows.Attributes;
 4using Elsa.Workflows.Options;
 5using Elsa.Workflows.Signals;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Workflows.Activities;
 9
 10/// <summary>
 11/// Execute a set of activities in sequence.
 12/// </summary>
 13/// <remarks>
 14/// Rescheduled direct children retain completion callback ownership by this sequence.
 15/// </remarks>
 16[Category("Workflows")]
 17[Activity("Elsa", "Workflows", "Execute a set of activities in sequence.")]
 18[PublicAPI]
 19[Browsable(false)]
 20public class Sequence : Container
 21{
 22    private const string CurrentIndexProperty = "CurrentIndex";
 23
 24    /// <inheritdoc />
 997925    public Sequence([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
 26    {
 997927        OnSignalReceived<BreakSignal>(OnBreakSignalReceived);
 997928        OnSignalReceived<ScheduleChildActivity>(OnScheduleChildActivityAsync);
 997929    }
 30
 31    /// <inheritdoc />
 32    protected override async ValueTask ScheduleChildrenAsync(ActivityExecutionContext context)
 33    {
 16734        await HandleItemAsync(context);
 16735    }
 36
 37    private async ValueTask HandleItemAsync(ActivityExecutionContext context, ActivityExecutionContext? completedChildCo
 38    {
 45239        var currentIndex = context.GetProperty<int>(CurrentIndexProperty);
 45240        var childActivities = Activities.ToList();
 41
 45242        if (currentIndex >= childActivities.Count)
 43        {
 12144            await context.CompleteActivityAsync();
 12145            return;
 46        }
 47
 33148        var nextActivity = childActivities.ElementAt(currentIndex);
 33149        var options = new ScheduleWorkOptions
 33150        {
 33151            CompletionCallback = OnChildCompleted,
 33152            SchedulingActivityExecutionId = completedChildContext?.Id
 33153        };
 33154        await context.ScheduleActivityAsync(nextActivity, options);
 66255        context.UpdateProperty<int>(CurrentIndexProperty, x => x + 1);
 45256    }
 57
 58    private async ValueTask OnChildCompleted(ActivityCompletedContext context)
 59    {
 29660        var targetContext = context.TargetContext;
 29661        var childContext = context.ChildContext;
 29662        var isBreaking = targetContext.GetIsBreaking();
 29663        var completedActivity = childContext.Activity;
 64
 65        // If the complete activity is a terminal node, complete the sequence immediately.
 29666        if (isBreaking || completedActivity is ITerminalNode)
 67        {
 1168            await targetContext.CompleteActivityAsync();
 1169            return;
 70        }
 71
 28572        await HandleItemAsync(targetContext, childContext);
 29673    }
 74
 75    private async ValueTask OnScheduleChildActivityAsync(ScheduleChildActivity signal, SignalContext context)
 76    {
 077        var sequenceContext = context.ReceiverActivityExecutionContext;
 078        var childActivity = signal.ActivityExecutionContext?.Activity ?? signal.Activity;
 79
 080        if (childActivity == null || !Activities.Contains(childActivity))
 81        {
 082            return;
 83        }
 84
 085        context.StopPropagation();
 086        await sequenceContext.ScheduleActivityAsync(childActivity, new ScheduleWorkOptions
 087        {
 088            ExistingActivityExecutionContext = signal.ActivityExecutionContext,
 089            CompletionCallback = OnChildCompleted,
 090            Input = signal.Input
 091        });
 092    }
 93
 94    private void OnBreakSignalReceived(BreakSignal signal, SignalContext signalContext)
 95    {
 096        signalContext.ReceiverActivityExecutionContext.SetIsBreaking();
 097    }
 98}