| | | 1 | | using Elsa.Expressions.Helpers; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Expressions.Models; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides context to workflow expressions. |
| | | 8 | | /// </summary> |
| | 2895 | 9 | | public class ExpressionExecutionContext( |
| | 2895 | 10 | | IServiceProvider serviceProvider, |
| | 2895 | 11 | | MemoryRegister memory, |
| | 2895 | 12 | | ExpressionExecutionContext? parentContext = null, |
| | 2895 | 13 | | IDictionary<object, object>? transientProperties = null, |
| | 2895 | 14 | | Action? onChange = null, |
| | 2895 | 15 | | CancellationToken cancellationToken = default) |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// A scoped service provider. |
| | | 19 | | /// </summary> |
| | 2912 | 20 | | public IServiceProvider ServiceProvider { get; } = serviceProvider; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// A shared register of computer memory. |
| | | 24 | | /// </summary> |
| | 15923 | 25 | | public MemoryRegister Memory { get; } = memory; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// A dictionary of transient properties. |
| | | 29 | | /// </summary> |
| | 7304 | 30 | | public IDictionary<object, object> TransientProperties { get; set; } = transientProperties ?? new Dictionary<object, |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Provides access to the parent <see cref="ExpressionExecutionContext"/>, if there is any. |
| | | 34 | | /// </summary> |
| | 10480 | 35 | | public ExpressionExecutionContext? ParentContext { get; set; } = parentContext; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// A cancellation token. |
| | | 39 | | /// </summary> |
| | 2909 | 40 | | public CancellationToken CancellationToken { get; } = cancellationToken; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Returns the <see cref="MemoryBlock"/> pointed to by the specified memory block reference. |
| | | 44 | | /// </summary> |
| | 217 | 45 | | public MemoryBlock GetBlock(Func<MemoryBlockReference> blockReference) => GetBlock(blockReference()); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Returns the <see cref="MemoryBlock"/> pointed to by the specified memory block reference. |
| | | 49 | | /// </summary> |
| | 243 | 50 | | public MemoryBlock GetBlock(MemoryBlockReference blockReference) => GetBlockInternal(blockReference) ?? throw new Ex |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Returns the <see cref="MemoryBlock"/> pointed to by the specified memory block reference. |
| | | 54 | | /// </summary> |
| | | 55 | | public bool TryGetBlock(MemoryBlockReference blockReference, out MemoryBlock block) |
| | | 56 | | { |
| | 946 | 57 | | var b = GetBlockInternal(blockReference); |
| | 946 | 58 | | block = b ?? null!; |
| | 946 | 59 | | return b != null; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Returns the value of the memory block pointed to by the specified memory block reference. |
| | | 64 | | /// </summary> |
| | 0 | 65 | | public object? Get(Func<MemoryBlockReference> blockReference) => Get(blockReference()); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Returns the value of the memory block pointed to by the specified memory block reference. |
| | | 69 | | /// </summary> |
| | 26 | 70 | | public object? Get(MemoryBlockReference blockReference) => GetBlock(blockReference).Value; |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Returns the value of the memory block pointed to by the specified memory block reference. |
| | | 74 | | /// </summary> |
| | | 75 | | public bool TryGet(MemoryBlockReference blockReference, out object? value) |
| | | 76 | | { |
| | 2 | 77 | | if (TryGetBlock(blockReference, out var block)) |
| | | 78 | | { |
| | 2 | 79 | | value = block.Value; |
| | 2 | 80 | | return true; |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | value = null; |
| | 0 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Returns the value of the memory block pointed to by the specified memory block reference. |
| | | 89 | | /// </summary> |
| | 0 | 90 | | public T? Get<T>(Func<MemoryBlockReference> blockReference) => Get<T>(blockReference()); |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Returns the value of the memory block pointed to by the specified memory block reference. |
| | | 94 | | /// </summary> |
| | 0 | 95 | | public T? Get<T>(MemoryBlockReference blockReference) => Get(blockReference).ConvertTo<T>(); |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Sets the value of the memory block pointed to by the specified memory block reference. |
| | | 99 | | /// </summary> |
| | 0 | 100 | | public void Set(Func<MemoryBlockReference> blockReference, object? value, Action<MemoryBlock>? configure = null) => |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Sets the value of the memory block pointed to by the specified memory block reference. |
| | | 104 | | /// </summary> |
| | | 105 | | public void Set(MemoryBlockReference blockReference, object? value, Action<MemoryBlock>? configure = null) |
| | | 106 | | { |
| | 1615 | 107 | | var block = GetBlockInternal(blockReference) ?? Memory.Declare(blockReference); |
| | 1615 | 108 | | block.Value = value; |
| | 1615 | 109 | | configure?.Invoke(block); |
| | 1615 | 110 | | onChange?.Invoke(); |
| | 1413 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Returns the service of the specified type. |
| | | 115 | | /// </summary> |
| | 17 | 116 | | public T GetRequiredService<T>() where T : notnull => ServiceProvider.GetRequiredService<T>(); |
| | | 117 | | |
| | | 118 | | private MemoryBlock? GetBlockInternal(MemoryBlockReference blockReference) |
| | | 119 | | { |
| | 2804 | 120 | | if (blockReference.Id == null!) |
| | 0 | 121 | | return null; |
| | | 122 | | |
| | 2804 | 123 | | var currentContext = this; |
| | | 124 | | |
| | 9926 | 125 | | while (currentContext != null) |
| | | 126 | | { |
| | 8338 | 127 | | var register = currentContext.Memory; |
| | | 128 | | |
| | 8338 | 129 | | if (register.TryGetBlock(blockReference.Id, out var block)) |
| | 1216 | 130 | | return block; |
| | | 131 | | |
| | 7122 | 132 | | currentContext = currentContext.ParentContext; |
| | | 133 | | } |
| | | 134 | | |
| | 1588 | 135 | | return null; |
| | | 136 | | } |
| | | 137 | | } |