using Elsa.Workflows; using Elsa.Workflows.Models; using Elsa.Workflows.Runtime; using Elsa.Workflows.Runtime.Activities; using Elsa.Workflows.Runtime.Stimuli; // ReSharper disable once CheckNamespace namespace Elsa.Extensions; public static class ActivityExecutionContextEventExtensions { /// /// Suspends the current activity's execution and waits for a specified event to occur before continuing. /// /// The activity execution context associated with the current activity execution. /// The name of the event to wait for. /// An optional delegate to execute when the event occurs. public static void WaitForEvent(this ActivityExecutionContext context, string eventName, ExecuteActivityDelegate? callback = null) { if (context.IsTriggerOfWorkflow()) { callback?.Invoke(context); return; } var options = new CreateBookmarkArgs { Stimulus = new EventStimulus(eventName), IncludeActivityInstanceId = false, BookmarkName = RuntimeStimulusNames.Event, Callback = callback }; context.CreateBookmark(options); } public static EventStimulus GetEventStimulus(this TriggerIndexingContext context, string eventName) { context.TriggerName = RuntimeStimulusNames.Event; return new(eventName); } public static object? GetEventInput(this ActivityExecutionContext context) { return context.TryGetWorkflowInput(Event.EventInputWorkflowInputKey, out var input) ? input : null; } }