Skip to content
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
4 changes: 2 additions & 2 deletions Src/Coravel.Mailer/MailServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public static IServiceCollection AddMailer(this IServiceCollection services, ICo
{
string mailerType = config.GetValue<string>("Coravel:Mail:Driver", "FileLog");

var strategies = new Dictionary<string, Action>();
var strategies = new Dictionary<string, Action>(StringComparer.OrdinalIgnoreCase);
strategies.Add("SMTP", () => AddSmtpMailer(services, config));
strategies.Add("FILELOG", () => AddFileLogMailer(services, config));
strategies[mailerType.ToUpper()].Invoke();
strategies[mailerType].Invoke();
return services;
}

Expand Down
19 changes: 7 additions & 12 deletions Src/Coravel/Events/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace Coravel.Events
/// </summary>
public class Dispatcher : IDispatcher, IEventRegistration
{
private IServiceScopeFactory _scopeFactory;
private Dictionary<Type, List<Type>> _events;
private readonly IServiceScopeFactory _scopeFactory;
private readonly Dictionary<Type, List<Type>> _events;

public Dispatcher(IServiceScopeFactory scopeFactory)
{
Expand All @@ -29,16 +29,11 @@ public Dispatcher(IServiceScopeFactory scopeFactory)
/// <returns></returns>
public IEventSubscription<TEvent> Register<TEvent>() where TEvent : IEvent
{
var listeners = new List<Type>();
var eventType = typeof(TEvent);

if (this._events.ContainsKey(eventType))
if(!this._events.TryGetValue(eventType, out var listeners))
{
listeners = this._events[eventType];
}
else
{
this._events.Add(eventType, listeners);
this._events.Add(eventType, listeners = new List<Type>());
}

return new EventSubscription<TEvent>(listeners);
Expand All @@ -48,9 +43,8 @@ public IEventSubscription<TEvent> Register<TEvent>() where TEvent : IEvent
/// Broadcasts an event to be handled by its subscribed listeners.
/// </summary>
/// <param name="toBroadcast"></param>
/// <typeparam name="TEvent"></typeparam>
/// <returns></returns>
public async Task Broadcast<TEvent>(TEvent toBroadcast) where TEvent : IEvent
public async Task Broadcast(IEvent toBroadcast)
{
if (this._events.TryGetValue(toBroadcast.GetType(), out var listeners))
{
Expand All @@ -59,10 +53,11 @@ public async Task Broadcast<TEvent>(TEvent toBroadcast) where TEvent : IEvent
await using (var scope = this._scopeFactory.CreateAsyncScope())
{
var obj = scope.ServiceProvider.GetService(listenerType);
if (obj is IListener<TEvent> listener)
if (obj is IListener listener)
{
await listener.HandleAsync(toBroadcast);
}
// can delete
else {
// Depending on what assemblies the events, listeners and calling assmebly are - the cast
// above doesn't work (even though the type really does implement the interface).
Expand Down
3 changes: 1 addition & 2 deletions Src/Coravel/Events/Interfaces/IDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public interface IDispatcher
/// <summary>
/// Dispatches and broadcasts the event to all subscribed listeners.
/// </summary>
/// <typeparam name="TEvent"></typeparam>
Task Broadcast<TEvent>(TEvent toBroadcast) where TEvent : IEvent;
Task Broadcast(IEvent toBroadcast);
}
}
11 changes: 10 additions & 1 deletion Src/Coravel/Events/Interfaces/IListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

namespace Coravel.Events.Interfaces
{
public interface IListener<TEvent> where TEvent : IEvent
public interface IListener<TEvent> : IListener
where TEvent : IEvent
{
Task HandleAsync(TEvent broadcasted);

Task IListener.HandleAsync(IEvent broadcasted)
=> HandleAsync((TEvent)broadcasted);
}

public interface IListener
{
internal Task HandleAsync(IEvent broadcasted);
}
}
3 changes: 1 addition & 2 deletions Src/Coravel/Queuing/Interfaces/IQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public interface IQueue
/// <summary>
/// Queue an event to be broadcasted.
/// </summary>
/// <typeparam name="TEvent"></typeparam>
void QueueBroadcast<TEvent>(TEvent toBroadcast) where TEvent : IEvent;
void QueueBroadcast(IEvent toBroadcast);

/// <summary>
/// Queue an invocable that will be given the payload supplied to this method.
Expand Down
2 changes: 1 addition & 1 deletion Src/Coravel/Queuing/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Guid QueueAsyncTask(Func<Task> asyncTask)
return job.Guid;
}

public void QueueBroadcast<TEvent>(TEvent toBroadcast) where TEvent : IEvent
public void QueueBroadcast(IEvent toBroadcast)
{
this.QueueAsyncTask(async () => await this._dispatcher.Broadcast(toBroadcast));
}
Expand Down
3 changes: 2 additions & 1 deletion Src/Coravel/Scheduling/Schedule/Mutex/InMemoryMutex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ public class InMemoryMutex : IMutex
{
private IUtcTime _utcTime;
private readonly object _lock = new object();
private Dictionary<string, MutexItem> _mutexCollection = new Dictionary<string, MutexItem>();
private readonly Dictionary<string, MutexItem> _mutexCollection;

public InMemoryMutex() {
this._utcTime = new SystemUtcTime();
_mutexCollection = new Dictionary<string, MutexItem>();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CoravelUnitTests.Scheduling.Stubs
{
public class DispatcherStub : IDispatcher
{
public Task Broadcast<TEvent>(TEvent toBroadcast) where TEvent : IEvent
public Task Broadcast(IEvent toBroadcast)
{
return Task.CompletedTask;
}
Expand Down