Skip to content

Commit

Permalink
Merge pull request #292 from jbogard/remove-when-all
Browse files Browse the repository at this point in the history
Making all enumerable tasks foreach; fixes #288
  • Loading branch information
jbogard authored Jul 17, 2018
2 parents c1ad66e + 22fd25b commit 8907530
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>5.0.1</VersionPrefix>
<VersionPrefix>5.1.0</VersionPrefix>
</PropertyGroup>
</Project>
9 changes: 6 additions & 3 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ public Task Publish<TNotification>(TNotification notification, CancellationToken
}

/// <summary>
/// Override in a derived class to control how the tasks are awaited. By default the implementation is <see cref="Task.WhenAll(IEnumerable{Task})" />
/// Override in a derived class to control how the tasks are awaited. By default the implementation is a foreach and await of each handler
/// </summary>
/// <param name="allHandlers">Enumerable of tasks representing invoking each notification handler</param>
/// <returns>A task representing invoking all handlers</returns>
protected virtual Task PublishCore(IEnumerable<Task> allHandlers)
protected virtual async Task PublishCore(IEnumerable<Task> allHandlers)
{
return Task.WhenAll(allHandlers);
foreach (var handler in allHandlers)
{
await handler.ConfigureAwait(false);
}
}
}
}
6 changes: 4 additions & 2 deletions src/MediatR/Pipeline/RequestPostProcessorBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace MediatR.Pipeline
{
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -23,7 +22,10 @@ public async Task<TResponse> Handle(TRequest request, CancellationToken cancella
{
var response = await next().ConfigureAwait(false);

await Task.WhenAll(_postProcessors.Select(p => p.Process(request, response))).ConfigureAwait(false);
foreach (var processor in _postProcessors)
{
await processor.Process(request, response).ConfigureAwait(false);
}

return response;
}
Expand Down
6 changes: 4 additions & 2 deletions src/MediatR/Pipeline/RequestPreProcessorBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace MediatR.Pipeline
{
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -21,7 +20,10 @@ public RequestPreProcessorBehavior(IEnumerable<IRequestPreProcessor<TRequest>> p

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
await Task.WhenAll(_preProcessors.Select(p => p.Process(request, cancellationToken))).ConfigureAwait(false);
foreach (var processor in _preProcessors)
{
await processor.Process(request, cancellationToken).ConfigureAwait(false);
}

return await next().ConfigureAwait(false);
}
Expand Down

0 comments on commit 8907530

Please sign in to comment.