-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
260 additions
and
293 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
namespace Sample; | ||
|
||
public record MyMessageRequest(string Arg, bool FireAndForgetEvents, bool ParallelEvents) : IRequest<MyMessageResponse>; | ||
public record MyMessageRequest(string Arg, bool FireAndForgetEvents) : IRequest<MyMessageResponse>; | ||
|
||
public record MyMessageResponse(string Response); | ||
|
||
public record MyMessageEvent(string Arg, bool FireAndForgetEvents, bool ParallelEvents) : IEvent; | ||
public record MyMessageEvent(string Arg, bool FireAndForgetEvents) : IEvent; |
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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,16 @@ | ||
namespace Shiny.Mediator; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <typeparam name="TEvent"></typeparam> | ||
public interface IEventHandler<in TEvent> where TEvent : IEvent | ||
{ | ||
/// <summary> | ||
/// / | ||
/// </summary> | ||
/// <param name="event"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task Handle(TEvent @event, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,8 @@ | ||
namespace Shiny.Mediator; | ||
using Shiny.Mediator.Infrastructure; | ||
|
||
namespace Shiny.Mediator; | ||
|
||
public interface IMediator | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task Send<TRequest>( | ||
TRequest request, | ||
CancellationToken cancellationToken = default | ||
) where TRequest : IRequest; | ||
|
||
|
||
// Task Send(object arg, CancellationToken cancellationToken = default) | ||
// Task<object?> Send(object arg, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <typeparam name="TResult"></typeparam> | ||
/// <returns></returns> | ||
Task<TResult> Send<TResult>( | ||
IRequest<TResult> request, | ||
CancellationToken cancellationToken = default | ||
); | ||
|
||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="event"></param> | ||
/// <param name="fireAndForget"></param> | ||
/// <param name="executeInParallel"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <typeparam name="TEvent"></typeparam> | ||
/// <returns></returns> | ||
Task Publish<TEvent>( | ||
TEvent @event, | ||
bool fireAndForget = true, | ||
bool executeInParallel = true, | ||
CancellationToken cancellationToken = default | ||
) where TEvent : IEvent; | ||
|
||
// Task Publish(object arg, CancellationToken cancellationToken = default) | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="action"></param> | ||
/// <typeparam name="TEvent"></typeparam> | ||
/// <returns></returns> | ||
IDisposable Subscribe<TEvent>( | ||
Func<TEvent, CancellationToken, Task> action | ||
) where TEvent : IEvent; | ||
public interface IMediator : IRequestSender, IEventPublisher | ||
{ | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
using Shiny.Mediator; | ||
|
||
// TODO: how do I register an "ALL" middleware | ||
|
||
// TODO: execution duration timer | ||
|
||
// TODO: catch all could be IRequest or IRequest<T>? Could use an IRequest<Void>? | ||
public interface IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
{ | ||
// intercept with connectivity, if offline go to cache, if online go to remote | ||
// if went to remote, post execute stores to cache | ||
Task<TResult> Process(TRequest request, IRequestHandler<TRequest, TResult> handler); | ||
} | ||
// using Shiny.Mediator; | ||
// | ||
// // TODO: how do I register an "ALL" middleware | ||
// | ||
// // TODO: execution duration timer | ||
// | ||
// // TODO: catch all could be IRequest or IRequest<T>? Could use an IRequest<Void>? | ||
// public interface IRequestMiddleware<TRequest, TResult> where TRequest : IRequest<TResult> | ||
// { | ||
// // intercept with connectivity, if offline go to cache, if online go to remote | ||
// // if went to remote, post execute stores to cache | ||
// Task<TResult> Process(TRequest request, IRequestMiddleware<TRequest, TResult> next, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Shiny.Mediator.Infrastructure; | ||
|
||
namespace Shiny.Mediator.Impl; | ||
|
||
public class DefaultEventPublisher(IServiceProvider services, IEnumerable<IEventCollector> collectors) : IEventPublisher | ||
{ | ||
readonly SubscriptionEventCollector subscriptions = new(); | ||
|
||
|
||
public async Task Publish<TEvent>( | ||
TEvent @event, | ||
CancellationToken cancellationToken = default | ||
) where TEvent : IEvent | ||
{ | ||
// allow registered services to be transient/scoped/singleton | ||
using var scope = services.CreateScope(); | ||
var handlers = scope.ServiceProvider.GetServices<IEventHandler<TEvent>>().ToList(); | ||
//var globalHandlers = scope.ServiceProvider.GetServices<IEventHandler<IEvent>>().ToList(); | ||
|
||
AppendHandlersIf(handlers, this.subscriptions); | ||
foreach (var collector in collectors) | ||
AppendHandlersIf(handlers, collector); | ||
|
||
if (handlers.Count == 0) | ||
return; | ||
|
||
await Task | ||
.WhenAll( | ||
handlers | ||
.Select(x => x.Handle(@event, cancellationToken)) | ||
.ToList() | ||
) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
|
||
public IDisposable Subscribe<TEvent>(Func<TEvent, CancellationToken, Task> action) where TEvent : IEvent | ||
{ | ||
var handler = new SubscriptionEventHandler<TEvent>(this.subscriptions); | ||
handler.OnHandle = action; | ||
return handler; | ||
} | ||
|
||
|
||
static void AppendHandlersIf<TEvent>(List<IEventHandler<TEvent>> list, IEventCollector collector) where TEvent : IEvent | ||
{ | ||
var handlers = collector.GetHandlers<TEvent>(); | ||
foreach (var handler in handlers) | ||
{ | ||
if (!list.Contains(handler)) | ||
list.Add(handler); | ||
} | ||
} | ||
} |
Oops, something went wrong.