| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Activities; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Represents an inline code activity that can be used to execute arbitrary .NET code from a workflow. |
| | | 11 | | /// </summary> |
| | | 12 | | [Browsable(false)] |
| | | 13 | | [Activity("Elsa", "Primitives", "Evaluate a Boolean condition to determine which path to execute next.")] |
| | | 14 | | [PublicAPI] |
| | | 15 | | public class Inline : CodeActivity |
| | | 16 | | { |
| | | 17 | | private readonly Func<ActivityExecutionContext, ValueTask> _activity = _ => default; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public Inline([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 21 | | { |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public Inline(Func<ActivityExecutionContext, ValueTask> activity, [CallerFilePath] string? source = null, [CallerLin |
| | | 26 | | { |
| | | 27 | | _activity = activity; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public Inline(Func<ValueTask> activity, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 32 | | { |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public Inline(Action<ActivityExecutionContext> activity, [CallerFilePath] string? source = null, [CallerLineNumber] |
| | | 37 | | { |
| | | 38 | | activity(c); |
| | | 39 | | return new ValueTask(); |
| | | 40 | | }, source, line) |
| | | 41 | | { |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public Inline(Action activity, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(c |
| | | 46 | | { |
| | | 47 | | activity(); |
| | | 48 | | return new ValueTask(); |
| | | 49 | | }, source, line) |
| | | 50 | | { |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) => await _activity(context); |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 58 | | /// </summary> |
| | | 59 | | public static Inline From(Func<ActivityExecutionContext, ValueTask> activity) => new(activity); |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 63 | | /// </summary> |
| | | 64 | | public static Inline From(Func<ValueTask> activity) => new(activity); |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 68 | | /// </summary> |
| | | 69 | | public static Inline From(Action<ActivityExecutionContext> activity) => new(activity); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 73 | | /// </summary> |
| | | 74 | | public static Inline From(Action activity) => new(activity); |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 78 | | /// </summary> |
| | | 79 | | public static Inline<T> From<T>(Func<ActivityExecutionContext, ValueTask<T>> activity) => new(activity); |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 83 | | /// </summary> |
| | | 84 | | public static Inline<T> From<T>(Func<ValueTask<T>> activity) => new(activity); |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 88 | | /// </summary> |
| | | 89 | | public static Inline<T> From<T>(Func<ActivityExecutionContext, T> activity) => new(activity); |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// Creates a new <see cref="Inline"/> activity from the specified delegate. |
| | | 93 | | /// </summary> |
| | | 94 | | public static Inline<T> From<T>(Func<T> activity) => new(activity); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Represents an inline code activity that can be used to execute arbitrary .NET code from a workflow and return a valu |
| | | 99 | | /// </summary> |
| | | 100 | | [PublicAPI] |
| | | 101 | | public class Inline<T> : CodeActivity<T> |
| | | 102 | | { |
| | | 103 | | private readonly Func<ActivityExecutionContext, ValueTask<T>> _activity; |
| | | 104 | | |
| | | 105 | | /// <inheritdoc /> |
| | | 106 | | public Inline(Func<ActivityExecutionContext, ValueTask<T>> activity, MemoryBlockReference? output = null, [CallerFil |
| | 0 | 107 | | : base(output, source, line) |
| | | 108 | | { |
| | 0 | 109 | | _activity = activity; |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | /// <inheritdoc /> |
| | | 113 | | public Inline(Func<ValueTask<T>> activity, MemoryBlockReference? output = null, [CallerFilePath] string? source = nu |
| | 0 | 114 | | : this(_ => activity(), output, source, line) |
| | | 115 | | { |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public Inline(Func<ActivityExecutionContext, T> activity, MemoryBlockReference? output = null, [CallerFilePath] stri |
| | 0 | 120 | | : this(c => |
| | 0 | 121 | | { |
| | 0 | 122 | | var result = activity(c); |
| | 0 | 123 | | return new ValueTask<T>(result); |
| | 0 | 124 | | }, output, source, line) |
| | | 125 | | { |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | /// <inheritdoc /> |
| | | 129 | | public Inline(Func<T> activity, MemoryBlockReference? output = null, [CallerFilePath] string? source = null, [Caller |
| | 0 | 130 | | : this(c => |
| | 0 | 131 | | { |
| | 0 | 132 | | var result = activity(); |
| | 0 | 133 | | return new ValueTask<T>(result); |
| | 0 | 134 | | }, output, source, line) |
| | | 135 | | { |
| | 0 | 136 | | } |
| | | 137 | | |
| | | 138 | | /// <inheritdoc /> |
| | | 139 | | protected override async ValueTask ExecuteAsync(ActivityExecutionContext context) |
| | | 140 | | { |
| | 0 | 141 | | var result = await _activity(context); |
| | 0 | 142 | | context.Set(Result, result); |
| | 0 | 143 | | } |
| | | 144 | | } |