forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Event.cs
88 lines (74 loc) · 3.23 KB
/
Event.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Runtime.CompilerServices;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Memory;
using Elsa.Workflows.Models;
using Elsa.Workflows.Runtime.Bookmarks;
using JetBrains.Annotations;
namespace Elsa.Workflows.Runtime.Activities;
/// <summary>
/// Wait for an event to be triggered.
/// </summary>
[Activity("Elsa", "Primitives", "Wait for an event to be published.")]
[UsedImplicitly]
public class Event : Trigger<object?>
{
internal const string EventPayloadWorkflowInputKey = "__EventPayloadWorkflowInput";
/// <inheritdoc />
internal Event([CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : base(source, line)
{
}
/// <inheritdoc />
public Event(string eventName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default)
: this(new Literal<string>(eventName), source, line)
{
}
/// <inheritdoc />
public Event(Func<string> eventName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default)
: this(new Input<string>(Expression.DelegateExpression(eventName), new MemoryBlockReference()), source, line)
{
}
/// <inheritdoc />
public Event(Func<ExpressionExecutionContext, string?> eventName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default)
: this(new Input<string>(Expression.DelegateExpression(eventName), new MemoryBlockReference()), source, line)
{
}
/// <inheritdoc />
public Event(Variable<string> variable, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line) =>
EventName = new Input<string>(variable);
/// <inheritdoc />
public Event(Literal<string> literal, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line) =>
EventName = new Input<string>(literal);
/// <inheritdoc />
public Event(Input<string> eventName, [CallerFilePath] string? source = default, [CallerLineNumber] int? line = default) : this(source, line) => EventName = eventName;
/// <summary>
/// The name of the event to listen for.
/// </summary>
[Input(Description = "The name of the event to listen for.")]
public Input<string> EventName { get; set; } = default!;
/// <inheritdoc />
protected override object GetTriggerPayload(TriggerIndexingContext context)
{
var eventName = EventName.Get(context.ExpressionExecutionContext);
return new EventBookmarkPayload(eventName);
}
/// <inheritdoc />
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var eventName = context.Get(EventName)!;
if (!context.IsTriggerOfWorkflow())
{
var options = new CreateBookmarkArgs
{
Payload = new EventBookmarkPayload(eventName),
IncludeActivityInstanceId = false
};
context.CreateBookmark(options);
return;
}
var input = context.GetWorkflowInput<object?>(EventPayloadWorkflowInputKey);
context.SetResult(input);
await context.CompleteActivityAsync();
}
}