< Summary

Information
Class: Elsa.Workflows.Behavior
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Abstractions/Behavior.cs
Line coverage
100%
Covered lines: 26
Uncovered lines: 0
Coverable lines: 26
Total lines: 105
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Owner()100%11100%
OnSignalReceived(...)100%11100%
OnSignalReceived(...)100%11100%
OnSignalReceived(...)100%11100%
OnSignalReceivedAsync(...)100%11100%
OnSignalReceived(...)100%11100%
ExecuteAsync(...)100%11100%
Execute(...)100%11100%
Elsa-Workflows-ISignalHandler-ReceiveSignalAsync()100%22100%
Elsa-Workflows-IBehavior-ExecuteAsync()100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Abstractions/Behavior.cs

#LineLine coverage
 1using Elsa.Workflows.Models;
 2
 3namespace Elsa.Workflows;
 4
 5/// <inheritdoc />
 6public abstract class Behavior : IBehavior
 7{
 156378    private readonly ICollection<SignalHandlerRegistration> _signalReceivedHandlers = new List<SignalHandlerRegistration
 9
 10    /// <summary>
 11    /// Initializes a new instance of the <see cref="Behavior"/> class.
 12    /// </summary>
 13    /// <param name="owner">The activity that owns this behavior.</param>
 1563714    protected Behavior(IActivity owner)
 15    {
 1563716        Owner = owner;
 1563717    }
 18
 19    /// <summary>
 20    /// The activity that owns this behavior.
 21    /// </summary>
 622    public IActivity Owner { get; }
 23
 24    /// <summary>
 25    /// Registers a delegate to be invoked when a signal of the specified type is received.
 26    /// </summary>
 27    /// <param name="signalType">The type of signal to register a handler for.</param>
 28    /// <param name="handler">The delegate to invoke when a signal of the specified type is received.</param>
 1322829    protected void OnSignalReceived(Type signalType, Func<object, SignalContext, ValueTask> handler) => _signalReceivedH
 30
 31    /// <summary>
 32    /// Registers a delegate to be invoked when a signal of the specified type is received.
 33    /// </summary>
 34    /// <param name="handler">The delegate to invoke when a signal of the specified type is received.</param>
 35    /// <typeparam name="T">The type of signal to register a handler for.</typeparam>
 2540036    protected void OnSignalReceived<T>(Func<T, SignalContext, ValueTask> handler) => OnSignalReceived(typeof(T), (signal
 37
 38    /// <summary>
 39    /// Registers a delegate to be invoked when a signal of the specified type is received.
 40    /// </summary>
 41    /// <param name="handler">The delegate to invoke when a signal of the specified type is received.</param>
 42    /// <typeparam name="T">The type of signal to register a handler for.</typeparam>
 43    protected void OnSignalReceived<T>(Action<T, SignalContext> handler)
 44    {
 12845        OnSignalReceived<T>((signal, context) =>
 12846        {
 147            handler(signal, context);
 148            return ValueTask.CompletedTask;
 12849        });
 12850    }
 51
 52    /// <summary>
 53    /// Registers a delegate to be invoked when a signal of the specified type is received.
 54    /// </summary>
 55    /// <param name="signal">The type of signal to register a handler for.</param>
 56    /// <param name="context">The signal context.</param>
 57    protected virtual ValueTask OnSignalReceivedAsync(object signal, SignalContext context)
 58    {
 1294159        OnSignalReceived(signal, context);
 1294160        return ValueTask.CompletedTask;
 61    }
 62
 63    /// <summary>
 64    /// Registers a delegate to be invoked when a signal of the specified type is received.
 65    /// </summary>
 66    /// <param name="signal">The signal to register a handler for.</param>
 67    /// <param name="context">The signal context.</param>
 68    protected virtual void OnSignalReceived(object signal, SignalContext context)
 69    {
 1294170    }
 71
 72    /// <summary>
 73    ///
 74    /// </summary>
 75    /// <param name="context"></param>
 76    /// <returns></returns>
 77    protected virtual ValueTask ExecuteAsync(ActivityExecutionContext context)
 78    {
 341779        Execute(context);
 341780        return ValueTask.CompletedTask;
 81    }
 82
 83    /// <summary>
 84    /// Invoked when the activity executes.
 85    /// </summary>
 86    /// <param name="context"></param>
 87    protected virtual void Execute(ActivityExecutionContext context)
 88    {
 341789    }
 90
 91    async ValueTask ISignalHandler.ReceiveSignalAsync(object signal, SignalContext context)
 92    {
 93        // Give derived activity a chance to do something with the signal.
 1294194        await OnSignalReceivedAsync(signal, context);
 95
 96        // Invoke registered signal delegates for this particular type of signal.
 1294197        var signalType = signal.GetType();
 2534998        var handlers = _signalReceivedHandlers.Where(x => x.SignalType == signalType);
 99
 50226100        foreach (var registration in handlers)
 12172101            await registration.Handler(signal, context);
 12941102    }
 103
 4061104    async ValueTask IBehavior.ExecuteAsync(ActivityExecutionContext context) => await ExecuteAsync(context);
 105}