| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | using Elsa.Extensions; |
| | | 4 | | using Elsa.Workflows.Attributes; |
| | | 5 | | using Elsa.Workflows.Memory; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Workflows.Runtime.Activities; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Wait for an event to be triggered. |
| | | 13 | | /// </summary> |
| | | 14 | | [Activity("Elsa", "Primitives", "Wait for an event to be published.")] |
| | | 15 | | [UsedImplicitly] |
| | | 16 | | public class Event : Trigger<object?> |
| | | 17 | | { |
| | | 18 | | public const string EventInputWorkflowInputKey = "__EventPayloadWorkflowInput"; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | 121 | 21 | | internal Event([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 22 | | { |
| | 121 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public Event(string eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | 114 | 27 | | : this(new Literal<string>(eventName), source, line) |
| | | 28 | | { |
| | 114 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public Event(Func<string> eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | 0 | 33 | | : this(new Input<string>(Expression.DelegateExpression(eventName), new()), source, line) |
| | | 34 | | { |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public Event(Func<ExpressionExecutionContext, string?> eventName, [CallerFilePath] string? source = null, [CallerLin |
| | 0 | 39 | | : this(new Input<string>(Expression.DelegateExpression(eventName), new()), source, line) |
| | | 40 | | { |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | 0 | 44 | | public Event(Variable<string> variable, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) |
| | | 45 | | { |
| | 0 | 46 | | EventName = new(variable); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc /> |
| | 114 | 50 | | public Event(Literal<string> literal, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : |
| | | 51 | | { |
| | 114 | 52 | | EventName = new(literal); |
| | 114 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <inheritdoc /> |
| | 0 | 56 | | public Event(Input<string> eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : |
| | | 57 | | { |
| | 0 | 58 | | EventName = eventName; |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// The name of the event to listen for. |
| | | 63 | | /// </summary> |
| | | 64 | | [Input(Description = "The name of the event to listen for.")] |
| | 507 | 65 | | public Input<string> EventName { get; set; } = null!; |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | protected override object GetTriggerPayload(TriggerIndexingContext context) |
| | | 69 | | { |
| | 16 | 70 | | var eventName = EventName.Get(context.ExpressionExecutionContext); |
| | 16 | 71 | | return context.GetEventStimulus(eventName); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | protected override void Execute(ActivityExecutionContext context) |
| | | 76 | | { |
| | 22 | 77 | | var eventName = context.Get(EventName)!; |
| | 22 | 78 | | context.WaitForEvent(eventName, CompleteInternalAsync); |
| | 22 | 79 | | } |
| | | 80 | | |
| | | 81 | | private async ValueTask CompleteInternalAsync(ActivityExecutionContext context) |
| | | 82 | | { |
| | 15 | 83 | | context.SetResult(context.GetEventInput()); |
| | 15 | 84 | | await context.CompleteActivityAsync(); |
| | 15 | 85 | | } |
| | | 86 | | } |