Update Behavior API to receive owner
This commit is contained in:
parent
8fc9fb15b6
commit
2fbc6d748c
|
|
@ -20,7 +20,7 @@ public class For : Activity
|
|||
[JsonConstructor]
|
||||
public For()
|
||||
{
|
||||
Behaviors.Add<BreakBehavior>();
|
||||
Behaviors.Add<BreakBehavior>(this);
|
||||
Behaviors.Remove<AutoCompleteBehavior>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ public class ForEach : Activity
|
|||
|
||||
public ForEach()
|
||||
{
|
||||
Behaviors.Add<BreakBehavior>();
|
||||
Behaviors.Add<BreakBehavior>(this);
|
||||
Behaviors.Remove<AutoCompleteBehavior>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ public class While : Activity
|
|||
public While(IActivity? body = default)
|
||||
{
|
||||
Body = body!;
|
||||
Behaviors.Add<BreakBehavior>();
|
||||
Behaviors.Add<BreakBehavior>(this);
|
||||
Behaviors.Remove<AutoCompleteBehavior>();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using Elsa.Workflows.Core.Models;
|
||||
using Elsa.Workflows.Core.Services;
|
||||
|
||||
namespace Elsa.Workflows.Core.Behaviors;
|
||||
|
||||
|
|
@ -7,6 +8,10 @@ namespace Elsa.Workflows.Core.Behaviors;
|
|||
/// </summary>
|
||||
public class AutoCompleteBehavior : Behavior
|
||||
{
|
||||
public AutoCompleteBehavior(IActivity owner) : base(owner)
|
||||
{
|
||||
}
|
||||
|
||||
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
|
||||
{
|
||||
// If the activity created any bookmarks, do not complete.
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ namespace Elsa.Workflows.Core.Behaviors;
|
|||
/// </summary>
|
||||
public class BreakBehavior : Behavior
|
||||
{
|
||||
public BreakBehavior()
|
||||
public BreakBehavior(IActivity owner) : base(owner)
|
||||
{
|
||||
OnSignalReceived<BreakSignal>(OnBreakAsync);
|
||||
}
|
||||
|
|
@ -23,12 +23,6 @@ public class BreakBehavior : Behavior
|
|||
var childActivityExecutionContexts = context.ActivityExecutionContext.GetChildren().ToList();
|
||||
context.ActivityExecutionContext.WorkflowExecutionContext.RemoveActivityExecutionContexts(childActivityExecutionContexts);
|
||||
|
||||
// Remove bookmarks.
|
||||
// foreach (var childActivityExecutionContext in childActivityExecutionContexts)
|
||||
// {
|
||||
// childActivityExecutionContext.Bookmarks
|
||||
// }
|
||||
|
||||
// Mark this activity as completed.
|
||||
await context.ActivityExecutionContext.CompleteActivityAsync();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace Elsa.Workflows.Core;
|
|||
|
||||
public static class BehaviorCollectionExtensions
|
||||
{
|
||||
public static void Add<T>(this ICollection<IBehavior> behaviors) where T : IBehavior => behaviors.Add((T)Activator.CreateInstance(typeof(T))!);
|
||||
public static void Add<T>(this ICollection<IBehavior> behaviors, IActivity owner) where T : IBehavior => behaviors.Add((T)Activator.CreateInstance(typeof(T), owner)!);
|
||||
|
||||
public static void Remove<T>(this ICollection<IBehavior> behaviors) where T : IBehavior
|
||||
{
|
||||
|
|
|
|||
|
|
@ -14,8 +14,9 @@ public abstract class Activity : IActivity, ISignalHandler
|
|||
protected Activity()
|
||||
{
|
||||
TypeName = ActivityTypeNameHelper.GenerateTypeName(GetType());
|
||||
Behaviors.Add<ScheduledChildCallbackBehavior>();
|
||||
Behaviors.Add<AutoCompleteBehavior>();
|
||||
Behaviors.Add<ScheduledChildCallbackBehavior>(this);
|
||||
Behaviors.Add<AutoCompleteBehavior>(this);
|
||||
Behaviors.Add<CreateCompletedLogRecord>(this);
|
||||
}
|
||||
|
||||
protected Activity(string activityType) : this()
|
||||
|
|
|
|||
|
|
@ -6,6 +6,12 @@ public abstract class Behavior : IBehavior
|
|||
{
|
||||
private readonly ICollection<SignalHandlerRegistration> _signalHandlers = new List<SignalHandlerRegistration>();
|
||||
|
||||
public Behavior(IActivity owner)
|
||||
{
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
public IActivity Owner { get; }
|
||||
protected void OnSignalReceived(Type signalType, Func<object, SignalContext, ValueTask> handler) => _signalHandlers.Add(new SignalHandlerRegistration(signalType, handler));
|
||||
protected void OnSignalReceived<T>(Func<T, SignalContext, ValueTask> handler) => OnSignalReceived(typeof(T), (signal, context) => handler((T)signal, context));
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,11 @@ namespace Elsa.Workflows.Core.Services;
|
|||
|
||||
public interface IBehavior : ISignalHandler
|
||||
{
|
||||
/// <summary>
|
||||
/// The owner of this behavior.
|
||||
/// </summary>
|
||||
IActivity Owner { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Invoked when the activity executes.
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Reference in a new issue