| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | using Elsa.Workflows.Behaviors; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using Elsa.Workflows.UIHints; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Activities; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Execute an activity while a given condition evaluates to true. |
| | | 14 | | /// </summary> |
| | | 15 | | [Activity("Elsa", "Looping", "Execute an activity while a given condition evaluates to true.")] |
| | | 16 | | [PublicAPI] |
| | | 17 | | public class While : Activity |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates a <see cref="While"/> activity that loops forever. |
| | | 21 | | /// </summary> |
| | 1 | 22 | | public static While True(IActivity body) => new(body) |
| | 1 | 23 | | { |
| | 1 | 24 | | Condition = new(true) |
| | 1 | 25 | | }; |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | 50 | 28 | | public While([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 29 | | { |
| | 50 | 30 | | Behaviors.Add<BreakBehavior>(this); |
| | 50 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | 50 | 34 | | public While(IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : |
| | | 35 | | { |
| | 50 | 36 | | Body = body; |
| | 50 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc /> |
| | 47 | 40 | | public While(Input<bool> condition, IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumbe |
| | | 41 | | { |
| | 47 | 42 | | Condition = condition; |
| | 47 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public While(Func<ExpressionExecutionContext, ValueTask<bool>> condition, IActivity? body = null, [CallerFilePath] s |
| | 0 | 47 | | : this(new Input<bool>(condition), body, source, line) |
| | | 48 | | { |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc /> |
| | | 52 | | public While(Func<ExpressionExecutionContext, bool> condition, IActivity? body = null, [CallerFilePath] string? sour |
| | 36 | 53 | | : this(new Input<bool>(condition), body, source, line) |
| | | 54 | | { |
| | 36 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public While(Func<ValueTask<bool>> condition, IActivity? body = null, [CallerFilePath] string? source = null, [Calle |
| | 0 | 59 | | : this(new Input<bool>(condition), body, source, line) |
| | | 60 | | { |
| | 0 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | public While(Func<bool> condition, IActivity? body = null, [CallerFilePath] string? source = null, [CallerLineNumber |
| | 0 | 65 | | : this(new Input<bool>(condition), body, source, line) |
| | | 66 | | { |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// The condition to evaluate. |
| | | 71 | | /// </summary> |
| | | 72 | | [Input(AutoEvaluate = false, UIHint = InputUIHints.SingleLine)] |
| | 253 | 73 | | public Input<bool> Condition { get; set; } = new(false); |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// The <see cref="IActivity"/> to execute on every iteration. |
| | | 77 | | /// </summary> |
| | | 78 | | [Port] |
| | 245 | 79 | | public IActivity? Body { get; set; } |
| | | 80 | | |
| | | 81 | | /// <inheritdoc /> |
| | 31 | 82 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) => await HandleIterationAsync(cont |
| | | 83 | | |
| | | 84 | | private async ValueTask OnBodyCompleted(ActivityCompletedContext context) |
| | | 85 | | { |
| | 6 | 86 | | await HandleIterationAsync(context.TargetContext); |
| | 6 | 87 | | } |
| | | 88 | | |
| | | 89 | | private async ValueTask HandleIterationAsync(ActivityExecutionContext context) |
| | | 90 | | { |
| | 37 | 91 | | var isBreaking = context.GetIsBreaking(); |
| | 37 | 92 | | var loop = !isBreaking && await context.EvaluateInputPropertyAsync<While, bool>(x => x.Condition); |
| | | 93 | | |
| | 36 | 94 | | if (loop) |
| | 28 | 95 | | await context.ScheduleActivityAsync(Body, OnBodyCompleted); |
| | | 96 | | else |
| | 8 | 97 | | await context.CompleteActivityAsync(); |
| | 36 | 98 | | } |
| | | 99 | | } |