WIP #738 - Further refactor (remove loop)

This is pretty trivial really, it just cuts down on
indentation and moves the looping into a Linq
map (Select) which builds a collection and then
reduces it (SelectMany) to a flattened collection.

Also remove some needless await'ing.
This commit is contained in:
Craig Fowler 2021-03-18 20:40:09 +00:00
parent d9724e5267
commit f00f02041a
2 changed files with 29 additions and 23 deletions

View file

@ -45,11 +45,11 @@ namespace Elsa.Triggers
.ToListAsync(cancellationToken);
var tasksOfListsOfTriggers = supportedBookmarkProviders
.Select(async bookmarkProvider => await GetTriggersForBookmarkProvider(bookmarkProvider,
bookmarkProviderContext,
activityBlueprint,
workflowExecutionContext.WorkflowBlueprint,
cancellationToken));
.Select(bookmarkProvider => GetTriggersForBookmarkProvider(bookmarkProvider,
bookmarkProviderContext,
activityBlueprint,
workflowExecutionContext.WorkflowBlueprint,
cancellationToken));
return (await Task.WhenAll(tasksOfListsOfTriggers))
.SelectMany(x => x)
.ToList();

View file

@ -37,26 +37,32 @@ namespace Elsa.Triggers
public async Task<IEnumerable<WorkflowTrigger>> GetTriggersAsync(IEnumerable<IWorkflowBlueprint> workflowBlueprints,
CancellationToken cancellationToken = default)
{
var allTriggers = new List<WorkflowTrigger>();
var activityTypes = (await activityTypeService.GetActivityTypesAsync(cancellationToken)).ToDictionary(x => x.TypeName);
var activityTypes = (await activityTypeService.GetActivityTypesAsync(cancellationToken))
.ToDictionary(x => x.TypeName);
foreach (var workflowBlueprint in workflowBlueprints)
{
var startingActivityBlueprints = workflowBlueprint.GetStartActivities();
var workflowExecutionContext = await workflowExecutionContextFactory.CreateWorkflowExecutionContextAsync(workflowBlueprint,
cancellationToken);
var tasksOfCollectionsOfTriggers = startingActivityBlueprints
.Select(async activityBlueprint => await triggerProvider.GetTriggersForActivityBlueprintAsync(activityBlueprint,
workflowExecutionContext,
activityTypes,
cancellationToken));
var triggers = (await Task.WhenAll(tasksOfCollectionsOfTriggers))
.SelectMany(x => x)
.ToList();
allTriggers.AddRange(triggers);
}
var tasksOfListsOfTriggers = workflowBlueprints
.Select(workflowBlueprint => GetWorkflowTriggersForWorkflowBlueprintAsync(workflowBlueprint, activityTypes, cancellationToken));
return (await Task.WhenAll(tasksOfListsOfTriggers))
.SelectMany(x => x)
.ToList();
}
return allTriggers;
async Task<IList<WorkflowTrigger>> GetWorkflowTriggersForWorkflowBlueprintAsync(IWorkflowBlueprint workflowBlueprint,
IDictionary<string, ActivityType> activityTypes,
CancellationToken cancellationToken)
{
var startingActivityBlueprints = workflowBlueprint.GetStartActivities();
var workflowExecutionContext = await workflowExecutionContextFactory.CreateWorkflowExecutionContextAsync(workflowBlueprint,
cancellationToken);
var tasksOfCollectionsOfTriggers = startingActivityBlueprints
.Select(async activityBlueprint => await triggerProvider.GetTriggersForActivityBlueprintAsync(activityBlueprint,
workflowExecutionContext,
activityTypes,
cancellationToken));
return (await Task.WhenAll(tasksOfCollectionsOfTriggers))
.SelectMany(x => x)
.ToList();
}
}
}