| | | 1 | | using Elsa.Workflows.Models; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Workflows; |
| | | 4 | | |
| | | 5 | | /// <inheritdoc /> |
| | | 6 | | public abstract class Behavior : IBehavior |
| | | 7 | | { |
| | 15637 | 8 | | 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> |
| | 15637 | 14 | | protected Behavior(IActivity owner) |
| | | 15 | | { |
| | 15637 | 16 | | Owner = owner; |
| | 15637 | 17 | | } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The activity that owns this behavior. |
| | | 21 | | /// </summary> |
| | 6 | 22 | | 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> |
| | 13228 | 29 | | 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> |
| | 25400 | 36 | | 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 | | { |
| | 128 | 45 | | OnSignalReceived<T>((signal, context) => |
| | 128 | 46 | | { |
| | 1 | 47 | | handler(signal, context); |
| | 1 | 48 | | return ValueTask.CompletedTask; |
| | 128 | 49 | | }); |
| | 128 | 50 | | } |
| | | 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 | | { |
| | 12941 | 59 | | OnSignalReceived(signal, context); |
| | 12941 | 60 | | 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 | | { |
| | 12941 | 70 | | } |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="context"></param> |
| | | 76 | | /// <returns></returns> |
| | | 77 | | protected virtual ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 78 | | { |
| | 3417 | 79 | | Execute(context); |
| | 3417 | 80 | | 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 | | { |
| | 3417 | 89 | | } |
| | | 90 | | |
| | | 91 | | async ValueTask ISignalHandler.ReceiveSignalAsync(object signal, SignalContext context) |
| | | 92 | | { |
| | | 93 | | // Give derived activity a chance to do something with the signal. |
| | 12941 | 94 | | await OnSignalReceivedAsync(signal, context); |
| | | 95 | | |
| | | 96 | | // Invoke registered signal delegates for this particular type of signal. |
| | 12941 | 97 | | var signalType = signal.GetType(); |
| | 25349 | 98 | | var handlers = _signalReceivedHandlers.Where(x => x.SignalType == signalType); |
| | | 99 | | |
| | 50226 | 100 | | foreach (var registration in handlers) |
| | 12172 | 101 | | await registration.Handler(signal, context); |
| | 12941 | 102 | | } |
| | | 103 | | |
| | 4061 | 104 | | async ValueTask IBehavior.ExecuteAsync(ActivityExecutionContext context) => await ExecuteAsync(context); |
| | | 105 | | } |