-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #838 from jbogard/publish-strategies
Adding notification publisher strategies
- Loading branch information
Showing
14 changed files
with
437 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
|
||
namespace MediatR; | ||
|
||
public interface INotificationPublisher | ||
{ | ||
Task Publish(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification, | ||
CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MediatR; | ||
|
||
public record NotificationHandlerExecutor(object HandlerInstance, Func<INotification, CancellationToken, Task> HandlerCallback); |
24 changes: 24 additions & 0 deletions
24
src/MediatR/NotificationPublishers/ForeachAwaitPublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MediatR.NotificationPublishers; | ||
|
||
/// <summary> | ||
/// Awaits each notification handler in a single foreach loop: | ||
/// <code> | ||
/// foreach (var handler in handlers) { | ||
/// await handler(notification, cancellationToken); | ||
/// } | ||
/// </code> | ||
/// </summary> | ||
public class ForeachAwaitPublisher : INotificationPublisher | ||
{ | ||
public async Task Publish(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification, CancellationToken cancellationToken) | ||
{ | ||
foreach (var handler in handlerExecutors) | ||
{ | ||
await handler.HandlerCallback(notification, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/MediatR/NotificationPublishers/TaskWhenAllPublisher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace MediatR.NotificationPublishers; | ||
|
||
/// <summary> | ||
/// Uses Task.WhenAll with the list of Handler tasks: | ||
/// <code> | ||
/// var tasks = handlers | ||
/// .Select(handler => handler.Handle(notification, cancellationToken)) | ||
/// .ToList(); | ||
/// | ||
/// return Task.WhenAll(tasks); | ||
/// </code> | ||
/// </summary> | ||
public class TaskWhenAllPublisher : INotificationPublisher | ||
{ | ||
public Task Publish(IEnumerable<NotificationHandlerExecutor> handlerExecutors, INotification notification, CancellationToken cancellationToken) | ||
{ | ||
var tasks = handlerExecutors | ||
.Select(handler => handler.HandlerCallback(notification, cancellationToken)) | ||
.ToArray(); | ||
|
||
return Task.WhenAll(tasks); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.