elsa-core/src/activities/Elsa.Activities.Workflows/Activities/TriggerSignal.cs
2019-11-29 13:32:05 +01:00

70 lines
2.4 KiB
C#

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
{
/// <summary>
/// Triggers the specified signal.
/// </summary>
[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<string> Signal
{
get => GetState<WorkflowExpression<string>>();
set => SetState(value);
}
[ActivityProperty(
Hint = "An expression that evaluates to a dictionary to be provided as input when signaling."
)]
public WorkflowExpression<Variables> Input
{
get => GetState(() => new WorkflowExpression<Variables>(JavaScriptExpressionEvaluator.SyntaxName, "{}"));
set => SetState(value);
}
[ActivityProperty(Hint = "An expression that evaluates to the correlation ID to use when signaling.")]
public WorkflowExpression<string> CorrelationId
{
get => GetState(() => new WorkflowExpression<string>(LiteralEvaluator.SyntaxName, ""));
set => SetState(value);
}
protected override async Task<IActivityExecutionResult> 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();
}
}
}