| | | 1 | | using Elsa.Extensions; |
| | | 2 | | using Elsa.Workflows.Activities; |
| | | 3 | | using Elsa.Workflows.Signals; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Behaviors; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Implements a "break" behavior that handles the <see cref="BreakSignal"/> signal. |
| | | 9 | | /// Stops propagation of the signal, which is useful for looping activities such as <see cref="While"/>, <see cref="For" |
| | | 10 | | /// </summary> |
| | | 11 | | public class BreakBehavior : Behavior |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Constructor. |
| | | 15 | | /// </summary> |
| | 128 | 16 | | public BreakBehavior(IActivity owner) : base(owner) |
| | | 17 | | { |
| | 128 | 18 | | OnSignalReceived<BreakSignal>(OnBreak); |
| | 128 | 19 | | OnSignalReceived<CompleteCompositeSignal>(OnCompleteCompositeAsync); |
| | 128 | 20 | | } |
| | | 21 | | |
| | | 22 | | private async ValueTask OnCompleteCompositeAsync(CompleteCompositeSignal signal, SignalContext context) |
| | | 23 | | { |
| | | 24 | | // Cancel each descendant to clear bookmarks and cancel jobs etc. |
| | 0 | 25 | | await CancelDescendantsAsync(context); |
| | | 26 | | |
| | | 27 | | // Mark this activity as completed. |
| | 0 | 28 | | await context.ReceiverActivityExecutionContext.CompleteActivityAsync(); |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | private void OnBreak(BreakSignal signal, SignalContext context) |
| | | 32 | | { |
| | | 33 | | // Prevent bubbling. |
| | 1 | 34 | | context.StopPropagation(); |
| | | 35 | | |
| | | 36 | | // Set the IsBreaking property to true. |
| | 1 | 37 | | context.ReceiverActivityExecutionContext.SetProperty("IsBreaking", true); |
| | 1 | 38 | | } |
| | | 39 | | |
| | | 40 | | private async Task CancelDescendantsAsync(SignalContext context) |
| | | 41 | | { |
| | 0 | 42 | | await context.ReceiverActivityExecutionContext.CancelActivityAsync(); |
| | 0 | 43 | | } |
| | | 44 | | } |