| | | 1 | | using Elsa.Workflows.Memory; |
| | | 2 | | using JetBrains.Annotations; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A base class for implementing workflow definitions using the pipelineBuilder API. |
| | | 8 | | /// </summary> |
| | | 9 | | [UsedImplicitly(ImplicitUseTargetFlags.WithInheritors)] |
| | | 10 | | public abstract class WorkflowBase : IWorkflow |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Invokes the specified <see cref="IWorkflowBuilder"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | protected virtual ValueTask BuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default) |
| | | 16 | | { |
| | | 17 | | Build(builder); |
| | | 18 | | return ValueTask.CompletedTask; |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Invokes the specified <see cref="IWorkflowBuilder"/>. |
| | | 23 | | /// </summary> |
| | | 24 | | /// <param name="builder"></param> |
| | | 25 | | protected virtual void Build(IWorkflowBuilder builder) |
| | | 26 | | { |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gives derived types a chance to setup the <see cref="IWorkflowBuilder"/> before the BuildAsync method is invoked |
| | | 31 | | /// </summary> |
| | | 32 | | protected virtual ValueTask BeforeBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gives derived types a chance to setup the <see cref="IWorkflowBuilder"/> after the BuildAsync method was invoked |
| | | 36 | | /// </summary> |
| | | 37 | | protected virtual ValueTask AfterBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default) |
| | | 38 | | |
| | | 39 | | async ValueTask IWorkflow.BuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken) |
| | | 40 | | { |
| | | 41 | | await BeforeBuildAsync(builder, cancellationToken); |
| | | 42 | | await BuildAsync(builder, cancellationToken); |
| | | 43 | | await AfterBuildAsync(builder, cancellationToken); |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// A base class for implementing workflow definitions that can return a result using the pipelineBuilder API. |
| | | 49 | | /// </summary> |
| | | 50 | | public abstract class WorkflowBase<TResult> : WorkflowBase |
| | | 51 | | { |
| | | 52 | | /// <inheritdoc /> |
| | 0 | 53 | | protected WorkflowBase() |
| | | 54 | | { |
| | 0 | 55 | | Result = new("Result", default!); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Use this variable from your workflow to assign a result value. |
| | | 60 | | /// </summary> |
| | 0 | 61 | | protected Variable<TResult> Result { get; } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc /> |
| | | 64 | | protected override ValueTask BeforeBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = defaul |
| | | 65 | | { |
| | 0 | 66 | | builder.Result = Result; |
| | 0 | 67 | | return ValueTask.CompletedTask; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc /> |
| | | 71 | | protected override ValueTask AfterBuildAsync(IWorkflowBuilder builder, CancellationToken cancellationToken = default |
| | | 72 | | { |
| | 0 | 73 | | var variables = builder.Variables; |
| | | 74 | | |
| | 0 | 75 | | if(!variables.Contains(Result)) |
| | 0 | 76 | | variables.Add(Result); |
| | | 77 | | |
| | 0 | 78 | | builder.Variables = variables; |
| | | 79 | | |
| | 0 | 80 | | return ValueTask.CompletedTask; |
| | | 81 | | } |
| | | 82 | | } |