Skip to content

Commit

Permalink
async abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
petar-m committed Sep 10, 2020
1 parent 1d8f019 commit ba7b9f6
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/M.EventBroker/IEventBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ public interface IEventBroker : IDisposable
/// <param name="event">An <typeparamref name="TEvent"/> instance to be passed to all handlers of the event.</param>
void Publish<TEvent>(TEvent @event);
}
}
}
49 changes: 49 additions & 0 deletions src/M.EventBroker/IEventBrokerAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Threading.Tasks;

namespace M.EventBroker
{
/// <summary>
/// Represents an event broker.
/// </summary>
public interface IEventBrokerAsync : IDisposable
{
/// <summary>
/// Adds subscription for events of type <typeparamref name="TEvent"/>.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <param name="handler">A delegate that will be invoked when event is published.</param>
/// <param name="filter">A delegate used to perform filtering of events before invoking the handler.</param>
/// <param name="onError">Called when an error is caught during execution.</param>
void Subscribe<TEvent>(Func<TEvent, Task> handler, Func<TEvent, Task<bool>> filter = null, Func<Exception, TEvent, Task> onError = null);

/// <summary>
/// Adds subscription for events of type <typeparamref name="TEvent"/>.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <param name="handler">An instance of IEventHandler&lt;TEvent&gt; which Handle method will be invoked when event is published.</param>
void Subscribe<TEvent>(IEventHandlerAsync<TEvent> handler);

/// <summary>
/// Removes subscription for events of type <typeparamref name="TEvent"/>.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <param name="handler">A delegate to remove form subscribers.</param>
void Unsubscribe<TEvent>(Func<TEvent, Task> handler);

/// <summary>
/// Removes subscription for events of type <typeparamref name="TEvent"/>.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <param name="handler">An instance of IEventHandler&lt;TEvent&gt; to remove form subscribers.</param>
void Unsubscribe<TEvent>(IEventHandlerAsync<TEvent> handler);

/// <summary>
/// Publishes an event of type <typeparamref name="TEvent"/>.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <param name="event">An <typeparamref name="TEvent"/> instance to be passed to all handlers of the event.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
Task Publish<TEvent>(TEvent @event);
}
}
2 changes: 1 addition & 1 deletion src/M.EventBroker/IEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ public interface IEventHandler<TEvent>
/// <param name="event">The event instance which handling caused the exception.</param>
void OnError(Exception exception, TEvent @event);
}
}
}
34 changes: 34 additions & 0 deletions src/M.EventBroker/IEventHandlerAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;

namespace M.EventBroker
{
/// <summary>
/// Represents a logic for handling events.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
public interface IEventHandlerAsync<TEvent>
{
/// <summary>
/// Handles the event.
/// </summary>
/// <param name="event">An instance of TEvent representing the event.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
Task HandleAsync(TEvent @event);

/// <summary>
/// Returns a value indicating whether the event handler should be executed.
/// </summary>
/// <param name="event">An instance of TEvent representing the event.</param>
/// <returns>A value indicating whether the event handler should be executed.</returns>
Task<bool> ShouldHandleAsync(TEvent @event);

/// <summary>
/// Called when an error is caught during execution.
/// </summary>
/// <param name="exception">The exception caught.</param>
/// <param name="event">The event instance which handling caused the exception.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
Task OnErrorAsync(Exception exception, TEvent @event);
}
}
17 changes: 17 additions & 0 deletions src/M.EventBroker/IEventHandlerAsyncFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;

namespace M.EventBroker
{
/// <summary>
/// Represents a provider for event handling instances.
/// </summary>
public interface IEventHandlerAsyncFactory
{
/// <summary>
/// Returns an event handling instances.
/// </summary>
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <returns>An IEnumerable of event handlers. Null is valid return value.</returns>
IEnumerable<IEventHandlerAsync<TEvent>> AsyncHandlersFor<TEvent>();
}
}
2 changes: 1 addition & 1 deletion src/M.EventBroker/IEventHandlerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public interface IEventHandlerFactory
/// <returns>An IEnumerable of event handlers. Null is valid return value.</returns>
IEnumerable<IEventHandler<TEvent>> HandlersFor<TEvent>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ public interface IEventHandlerRunner : IDisposable
/// <param name="handlers">The event handlers to run.</param>
void Run(params Action[] handlers);
}
}
}
18 changes: 18 additions & 0 deletions src/M.EventBroker/IEventHandlerRunnerAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;

namespace M.EventBroker
{
/// <summary>
/// Represents runner of event handlers.
/// </summary>
public interface IEventHandlerRunnerAsync : IDisposable
{
/// <summary>
/// Runs event handlers.
/// </summary>
/// <param name="handlers">The event handlers to run.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
Task RunAsync(params Func<Task>[] handlers);
}
}

0 comments on commit ba7b9f6

Please sign in to comment.