From e2c8cfe7954d06ae46c85e1c027eeefe26dfa89a Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 12 Jan 2021 13:42:48 +0100 Subject: [PATCH] Improve IfElse scope --- .../Activities/ControlFlow/IfElse/IfElse.cs | 25 +++++++++++-------- .../HappinessWorkflow.cs | 20 +++++---------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/core/Elsa.Core/Activities/ControlFlow/IfElse/IfElse.cs b/src/core/Elsa.Core/Activities/ControlFlow/IfElse/IfElse.cs index 0651c461f..1e453097d 100644 --- a/src/core/Elsa.Core/Activities/ControlFlow/IfElse/IfElse.cs +++ b/src/core/Elsa.Core/Activities/ControlFlow/IfElse/IfElse.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using Elsa.ActivityResults; using Elsa.Attributes; using Elsa.Services; @@ -19,7 +21,7 @@ namespace Elsa.Activities.ControlFlow [ActivityProperty(Hint = "The condition to evaluate.")] public bool Condition { get; set; } - + private bool EnteredScope { get => GetState(); @@ -28,17 +30,20 @@ namespace Elsa.Activities.ControlFlow protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context) { - if (!EnteredScope) + if (!context.WorkflowInstance.Scopes.Contains(Id)) { - context.WorkflowInstance.Scopes.Push(Id); - EnteredScope = true; + if (!EnteredScope) + { + context.WorkflowInstance.Scopes.Push(Id); + EnteredScope = true; + } + else + { + EnteredScope = false; + return Done(); + } } - else - { - EnteredScope = false; - return Done(); - } - + var outcome = Condition ? True : False; return Outcome(outcome); } diff --git a/src/samples/console/Elsa.Samples.IfElseConsole/HappinessWorkflow.cs b/src/samples/console/Elsa.Samples.IfElseConsole/HappinessWorkflow.cs index f77a18fa6..aa6df0746 100644 --- a/src/samples/console/Elsa.Samples.IfElseConsole/HappinessWorkflow.cs +++ b/src/samples/console/Elsa.Samples.IfElseConsole/HappinessWorkflow.cs @@ -19,21 +19,13 @@ namespace Elsa.Samples.IfElseConsole workflow .WriteLine("--POND OF HAPPINESS--") .WriteLine("Throw some Rupees in and your wishes will surely come true.") - .WriteLine("Do you want to throw Rupees?").WithName("Start") + .WriteLine("Do you want to throw Rupees?") .ReadLine() - .IfElse(context => IsYes(context.Input), - ifElse => - { - ifElse - .When(OutcomeNames.True) - .WriteLine(GetCurse) - .WriteLine("...") - .Then("Start"); - - ifElse - .When(OutcomeNames.False) - .WriteLine("Keep your rupees."); - }) + .While(context => IsYes(context.Input), iteration => iteration + .WriteLine(GetCurse) + .WriteLine("...") + .WriteLine("Do you want to throw Rupees?") + .ReadLine()) .WriteLine("--END--"); }