using System.Threading;
using System.Threading.Tasks;
using Elsa.Attributes;
using Elsa.Expressions;
using Elsa.Models;
using Elsa.Scripting.JavaScript.Services;
using Elsa.Services;
using Elsa.Services.Models;
namespace Elsa.Activities.Workflows.Activities
{
///
/// Triggers the specified signal.
///
[ActivityDefinition(
Category = "Workflows",
Description = "Trigger the specified signal.",
Icon = "fas fa-broadcast-tower"
)]
public class TriggerSignal : Activity
{
private readonly IWorkflowRunner workflowRunner;
public TriggerSignal(IWorkflowRunner workflowRunner)
{
this.workflowRunner = workflowRunner;
}
[ActivityProperty(Hint = "An expression that evaluates to the name of the signal to trigger.")]
public WorkflowExpression Signal
{
get => GetState>();
set => SetState(value);
}
[ActivityProperty(
Hint = "An expression that evaluates to a dictionary to be provided as input when signaling."
)]
public WorkflowExpression Input
{
get => GetState(() => new WorkflowExpression(JavaScriptExpressionEvaluator.SyntaxName, "{}"));
set => SetState(value);
}
[ActivityProperty(Hint = "An expression that evaluates to the correlation ID to use when signaling.")]
public WorkflowExpression CorrelationId
{
get => GetState(() => new WorkflowExpression(LiteralEvaluator.SyntaxName, ""));
set => SetState(value);
}
protected override async Task OnExecuteAsync(WorkflowExecutionContext context, CancellationToken cancellationToken)
{
var signal = await context.EvaluateAsync(Signal, cancellationToken);
var input = (await context.EvaluateAsync(Input, cancellationToken)) ?? new Variables();
var correlationId = await context.EvaluateAsync(CorrelationId, cancellationToken);
input.SetVariable("Signal", signal);
await workflowRunner.TriggerAsync(
nameof(Signaled),
input,
correlationId,
cancellationToken: cancellationToken
);
return Done();
}
}
}