elsa-core/src/modules/Elsa.Workflows.Runtime/Activities/Event.cs
Sipke Schoorstra b613ff6b61
Addresses workflow identity reload conflict (#6909)
* Refactor `IndexTriggersAsync` to use `WorkflowDefinition` and update null assignment for serialization logic.

* Change default parameter value from `default` to `null` in `UseFluentStorageProvider` method signature.

* Add in-memory workflows provider and materializer for integration tests

Introduced `InMemoryWorkflowsProvider` and `InMemoryWorkflowMaterializer` to support integration testing scenarios for workflow definition population. Enhanced workflow handling with fluent method `WithId` for `WorkflowBuilder`. Updated event publishing and dependency injection logic.

* Remove extraneous whitespace in DefaultWorkflowDefinitionStorePopulator.

* Refine test class documentation for `WorkflowDefinitionStorePopulation` scenario.
2025-09-15 11:56:01 +02:00

86 lines
2.9 KiB
C#

using System.Runtime.CompilerServices;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Memory;
using Elsa.Workflows.Models;
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?>
{
public const string EventInputWorkflowInputKey = "__EventPayloadWorkflowInput";
/// <inheritdoc />
internal Event([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line)
{
}
/// <inheritdoc />
public Event(string eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null)
: this(new Literal<string>(eventName), source, line)
{
}
/// <inheritdoc />
public Event(Func<string> eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null)
: this(new Input<string>(Expression.DelegateExpression(eventName), new()), source, line)
{
}
/// <inheritdoc />
public Event(Func<ExpressionExecutionContext, string?> eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null)
: this(new Input<string>(Expression.DelegateExpression(eventName), new()), source, line)
{
}
/// <inheritdoc />
public Event(Variable<string> variable, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(source, line)
{
EventName = new(variable);
}
/// <inheritdoc />
public Event(Literal<string> literal, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : this(source, line)
{
EventName = new(literal);
}
/// <inheritdoc />
public Event(Input<string> eventName, [CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : 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; } = null!;
/// <inheritdoc />
protected override object GetTriggerPayload(TriggerIndexingContext context)
{
var eventName = EventName.Get(context.ExpressionExecutionContext);
return context.GetEventStimulus(eventName);
}
/// <inheritdoc />
protected override void Execute(ActivityExecutionContext context)
{
var eventName = context.Get(EventName)!;
context.WaitForEvent(eventName, CompleteInternalAsync);
}
private async ValueTask CompleteInternalAsync(ActivityExecutionContext context)
{
context.SetResult(context.GetEventInput());
await context.CompleteActivityAsync();
}
}