| | | 1 | | namespace Elsa.Workflows.Pipelines.WorkflowExecution; |
| | | 2 | | |
| | | 3 | | /// <inheritdoc /> |
| | | 4 | | public class WorkflowExecutionPipelineBuilder : IWorkflowExecutionPipelineBuilder |
| | | 5 | | { |
| | | 6 | | private const string ServicesKey = "workflow-execution.Services"; |
| | 425 | 7 | | private readonly IList<Func<WorkflowMiddlewareDelegate, WorkflowMiddlewareDelegate>> _components = new List<Func<Wor |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Constructor. |
| | | 11 | | /// </summary> |
| | 425 | 12 | | public WorkflowExecutionPipelineBuilder(IServiceProvider serviceProvider) |
| | | 13 | | { |
| | 425 | 14 | | ServiceProvider = serviceProvider; |
| | 425 | 15 | | } |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 2975 | 18 | | public IDictionary<object, object?> Properties { get; } = new Dictionary<object, object?>(); |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | 4 | 21 | | public IEnumerable<Func<WorkflowMiddlewareDelegate, WorkflowMiddlewareDelegate>> Components => _components.ToList(); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public IServiceProvider ServiceProvider |
| | | 25 | | { |
| | 2125 | 26 | | get => GetProperty<IServiceProvider>(ServicesKey)!; |
| | 425 | 27 | | set => SetProperty(ServicesKey, value); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public IWorkflowExecutionPipelineBuilder Use(Func<WorkflowMiddlewareDelegate, WorkflowMiddlewareDelegate> middleware |
| | | 32 | | { |
| | 2125 | 33 | | _components.Add(middleware); |
| | 2125 | 34 | | return this; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public WorkflowMiddlewareDelegate Build() |
| | | 39 | | { |
| | 861 | 40 | | WorkflowMiddlewareDelegate pipeline = _ => new ValueTask(); |
| | | 41 | | |
| | 5100 | 42 | | foreach (var component in _components.Reverse()) |
| | 2125 | 43 | | pipeline = component(pipeline); |
| | | 44 | | |
| | 425 | 45 | | return pipeline; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public IWorkflowExecutionPipelineBuilder Reset() |
| | | 50 | | { |
| | 425 | 51 | | _components.Clear(); |
| | 425 | 52 | | return this; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | | 56 | | public IWorkflowExecutionPipelineBuilder Insert(int index, Func<WorkflowMiddlewareDelegate, WorkflowMiddlewareDelega |
| | | 57 | | { |
| | 0 | 58 | | _components.Insert(index, middleware); |
| | 0 | 59 | | return this; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | public IWorkflowExecutionPipelineBuilder Replace(int index, Func<WorkflowMiddlewareDelegate, WorkflowMiddlewareDeleg |
| | | 64 | | { |
| | 4 | 65 | | _components[index] = middleware; |
| | 4 | 66 | | return this; |
| | | 67 | | } |
| | | 68 | | |
| | 2125 | 69 | | private T? GetProperty<T>(string key) => Properties.TryGetValue(key, out var value) ? (T?)value : default(T); |
| | 425 | 70 | | private void SetProperty<T>(string key, T value) => Properties[key] = value; |
| | | 71 | | } |