using Elsa.Extensions;
using Elsa.Workflows.Core.Helpers;
using Elsa.Workflows.Core.Models;
using Elsa.Workflows.Runtime.Activities;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Models;
namespace Elsa.Workflows.Runtime.Services;
///
public class EventPublisher : IEventPublisher
{
private readonly IWorkflowRuntime _workflowRuntime;
private readonly IWorkflowDispatcher _workflowDispatcher;
///
/// Constructor.
///
public EventPublisher(IWorkflowRuntime workflowRuntime, IWorkflowDispatcher workflowDispatcher)
{
_workflowRuntime = workflowRuntime;
_workflowDispatcher = workflowDispatcher;
}
///
public async Task PublishAsync(string eventName, string? correlationId = default, string? workflowInstanceId = default, IDictionary? input = default, CancellationToken cancellationToken = default)
{
var eventBookmark = new EventBookmarkPayload(eventName);
var options = new TriggerWorkflowsRuntimeOptions(correlationId, workflowInstanceId, input);
await _workflowRuntime.TriggerWorkflowsAsync(eventBookmark, options, cancellationToken);
}
///
public async Task DispatchAsync(string eventName, string? correlationId = default, string? workflowInstanceId = default, IDictionary? input = default, CancellationToken cancellationToken = default)
{
var eventBookmark = new EventBookmarkPayload(eventName);
var activityTypeName = ActivityTypeNameHelper.GenerateTypeName();
var request = new DispatchTriggerWorkflowsRequest(activityTypeName, eventBookmark, correlationId, workflowInstanceId, input);
await _workflowDispatcher.DispatchAsync(request, cancellationToken);
}
}