Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow task cancellation #5

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions MediatR.EventAggregator/EventAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace MediatR.EventAggregator
{
using System.Threading;

public class EventAggregator : IEventAggregator
{
private readonly IMediator mediator;
Expand All @@ -24,21 +26,23 @@ public EventAggregator(IMediator mediator)
}

/// <inheritdoc />
public Task PublishAsync<TEvent>(TEvent ev)
public Task PublishAsync<TEvent>(TEvent ev, CancellationToken cancellationToken = default(CancellationToken))
where TEvent : IRequest
{
var notification = ev as INotification;
if (notification != null)
{
this.mediator.Publish(notification);
this.mediator.Publish(notification, cancellationToken);
}

Task sendTask = null;
var request = ev as IRequest;
if (request != null)
{
sendTask = this.mediator.Send(request);
sendTask = this.mediator.Send(request, cancellationToken);
}

cancellationToken.ThrowIfCancellationRequested();

this.HandleCallbacks(ev);

Expand All @@ -47,10 +51,12 @@ public Task PublishAsync<TEvent>(TEvent ev)

// Note: can we make it more elegant to use , with better type inference ?
/// <inheritdoc />
public Task<TResponse> PublishAsync<TResponse, TEvent>(TEvent ev)
public Task<TResponse> PublishAsync<TResponse, TEvent>(TEvent ev, CancellationToken cancellationToken = default(CancellationToken))
where TEvent : IRequest<TResponse>
{
var sendTask = this.mediator.Send(ev);
var sendTask = this.mediator.Send(ev, cancellationToken);

cancellationToken.ThrowIfCancellationRequested();

this.HandleCallbacks(ev);

Expand Down
6 changes: 4 additions & 2 deletions MediatR.EventAggregator/IEventPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MediatR.EventAggregator
{
using System.Threading;

/// <summary>
/// A service providing mechanisms to publish events.
/// </summary>
Expand All @@ -13,7 +15,7 @@ public interface IEventPublisher
/// <typeparam name="TEvent">The type of the event.</typeparam>
/// <param name="ev">The event.</param>
/// <returns>A promise of completion</returns>
Copy link
Member

@ylerjen ylerjen Apr 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, also update the xml doc

Task PublishAsync<TEvent>(TEvent ev) where TEvent : IRequest;
Task PublishAsync<TEvent>(TEvent ev, CancellationToken cancellationToken = default(CancellationToken)) where TEvent : IRequest;

/// <summary>
/// Publishes the specified event, expecting an asynchronous response.
Expand All @@ -24,6 +26,6 @@ public interface IEventPublisher
/// <returns>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, also update the xml doc

/// A promise of a response to the published event.
/// </returns>
Task<TResponse> PublishAsync<TResponse, TEvent>(TEvent ev) where TEvent : IRequest<TResponse>;
Task<TResponse> PublishAsync<TResponse, TEvent>(TEvent ev, CancellationToken cancellationToken = default(CancellationToken)) where TEvent : IRequest<TResponse>;
}
}