| | | 1 | | using Elsa.Workflows.Helpers; |
| | | 2 | | using Elsa.Workflows.Runtime.Activities; |
| | | 3 | | using Elsa.Workflows.Runtime.Stimuli; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | 6 | 8 | | public class EventPublisher(IStimulusSender stimulusSender, IStimulusDispatcher stimulusDispatcher) : IEventPublisher |
| | | 9 | | { |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public async Task PublishAsync( |
| | | 12 | | string eventName, |
| | | 13 | | string? correlationId = null, |
| | | 14 | | string? workflowInstanceId = null, |
| | | 15 | | string? activityInstanceId = null, |
| | | 16 | | object? payload = null, |
| | | 17 | | bool asynchronous = false, |
| | | 18 | | CancellationToken cancellationToken = default) |
| | | 19 | | { |
| | 5 | 20 | | var stimulus = new EventStimulus(eventName); |
| | 5 | 21 | | var workflowInput = new Dictionary<string, object> |
| | 5 | 22 | | { |
| | 5 | 23 | | [Event.EventInputWorkflowInputKey] = payload ?? new Dictionary<string, object>() |
| | 5 | 24 | | }; |
| | 5 | 25 | | var metadata = new StimulusMetadata |
| | 5 | 26 | | { |
| | 5 | 27 | | CorrelationId = correlationId, |
| | 5 | 28 | | ActivityInstanceId = activityInstanceId, |
| | 5 | 29 | | WorkflowInstanceId = workflowInstanceId, |
| | 5 | 30 | | Input = workflowInput |
| | 5 | 31 | | }; |
| | 5 | 32 | | var triggerName = ActivityTypeNameHelper.GenerateTypeName<Event>(); |
| | 5 | 33 | | if (asynchronous) |
| | | 34 | | { |
| | 3 | 35 | | await stimulusDispatcher.SendAsync(new() |
| | 3 | 36 | | { |
| | 3 | 37 | | ActivityTypeName = triggerName, |
| | 3 | 38 | | Stimulus = stimulus, |
| | 3 | 39 | | Metadata = metadata |
| | 3 | 40 | | }, cancellationToken); |
| | | 41 | | } |
| | | 42 | | else |
| | 2 | 43 | | await stimulusSender.SendAsync(triggerName, stimulus, metadata, cancellationToken); |
| | 5 | 44 | | } |
| | | 45 | | } |