| | | 1 | | using Elsa.Workflows; |
| | | 2 | | using Elsa.Workflows.Models; |
| | | 3 | | using Elsa.Workflows.Runtime; |
| | | 4 | | using Elsa.Workflows.Runtime.Activities; |
| | | 5 | | using Elsa.Workflows.Runtime.Stimuli; |
| | | 6 | | |
| | | 7 | | // ReSharper disable once CheckNamespace |
| | | 8 | | namespace Elsa.Extensions; |
| | | 9 | | |
| | | 10 | | public static class ActivityExecutionContextEventExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Suspends the current activity's execution and waits for a specified event to occur before continuing. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="context">The activity execution context associated with the current activity execution.</param> |
| | | 16 | | /// <param name="eventName">The name of the event to wait for.</param> |
| | | 17 | | /// <param name="callback">An optional delegate to execute when the event occurs.</param> |
| | | 18 | | public static void WaitForEvent(this ActivityExecutionContext context, string eventName, ExecuteActivityDelegate? ca |
| | | 19 | | { |
| | 22 | 20 | | if (context.IsTriggerOfWorkflow()) |
| | | 21 | | { |
| | 5 | 22 | | callback?.Invoke(context); |
| | 5 | 23 | | return; |
| | | 24 | | } |
| | | 25 | | |
| | 17 | 26 | | var options = new CreateBookmarkArgs |
| | 17 | 27 | | { |
| | 17 | 28 | | Stimulus = new EventStimulus(eventName), |
| | 17 | 29 | | IncludeActivityInstanceId = false, |
| | 17 | 30 | | BookmarkName = RuntimeStimulusNames.Event, |
| | 17 | 31 | | Callback = callback |
| | 17 | 32 | | }; |
| | 17 | 33 | | context.CreateBookmark(options); |
| | 17 | 34 | | } |
| | | 35 | | |
| | | 36 | | public static EventStimulus GetEventStimulus(this TriggerIndexingContext context, string eventName) |
| | | 37 | | { |
| | 16 | 38 | | context.TriggerName = RuntimeStimulusNames.Event; |
| | 16 | 39 | | return new(eventName); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public static object? GetEventInput(this ActivityExecutionContext context) |
| | | 43 | | { |
| | 15 | 44 | | return context.TryGetWorkflowInput<object?>(Event.EventInputWorkflowInputKey, out var input) ? input : null; |
| | | 45 | | } |
| | | 46 | | } |