elsa-core/src/activities/Flowsharp.Activities.Console/Handlers/ReadLineHandler.cs

51 lines
1.8 KiB
C#
Raw Normal View History

2018-11-18 21:43:11 +00:00
using System.IO;
2018-10-15 12:15:36 +00:00
using System.Threading;
using System.Threading.Tasks;
2018-10-21 21:01:31 +00:00
using Flowsharp.Activities.Console.Activities;
2018-10-16 09:52:55 +00:00
using Flowsharp.Handlers;
2018-10-15 12:15:36 +00:00
using Flowsharp.Models;
using Flowsharp.Results;
2018-11-03 09:21:44 +00:00
using Microsoft.Extensions.Localization;
2018-10-15 12:15:36 +00:00
2018-10-21 21:01:31 +00:00
namespace Flowsharp.Activities.Console.Handlers
2018-10-15 12:15:36 +00:00
{
public class ReadLineHandler : ActivityHandler<ReadLine>
{
private readonly TextReader input;
2018-12-24 11:31:18 +00:00
2018-11-03 09:21:44 +00:00
public ReadLineHandler(IStringLocalizer<ReadLineHandler> localizer)
2018-10-15 12:15:36 +00:00
{
2018-11-03 09:21:44 +00:00
T = localizer;
2018-10-15 12:15:36 +00:00
}
2018-11-03 09:21:44 +00:00
public ReadLineHandler(IStringLocalizer<ReadLineHandler> localizer, TextReader input) : this(localizer)
2018-10-15 12:15:36 +00:00
{
this.input = input;
}
2018-11-03 09:21:44 +00:00
private IStringLocalizer<ReadLineHandler> T { get; }
2018-12-24 11:31:18 +00:00
2018-11-03 09:21:44 +00:00
protected override LocalizedString GetEndpoint() => T["Done"];
2018-12-24 11:31:18 +00:00
public override LocalizedString Category => T["Console"];
public override LocalizedString DisplayText => T["Read Line"];
public override LocalizedString Description => T["Read a line from the console"];
2018-10-15 12:15:36 +00:00
protected override async Task<ActivityExecutionResult> OnExecuteAsync(ReadLine activity, WorkflowExecutionContext workflowContext, CancellationToken cancellationToken)
{
2018-12-24 11:31:18 +00:00
if (input == null)
2018-10-15 12:15:36 +00:00
return Halt();
2018-12-24 11:31:18 +00:00
2018-10-15 12:15:36 +00:00
var value = await input.ReadLineAsync();
workflowContext.SetLastResult(value);
2018-11-03 09:21:44 +00:00
return TriggerEndpoint("Done");
2018-10-15 12:15:36 +00:00
}
protected override ActivityExecutionResult OnResume(ReadLine activity, WorkflowExecutionContext workflowContext)
{
var receivedInput = workflowContext.Workflow.Arguments[activity.ArgumentName];
workflowContext.SetLastResult(receivedInput);
2018-11-03 09:21:44 +00:00
return TriggerEndpoint("Done");
2018-10-15 12:15:36 +00:00
}
}
}