Skip to content

Commit

Permalink
Fix errors with multiple consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed May 20, 2024
1 parent 0b96617 commit 047bbb2
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<PropertyGroup>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionEventing.Demo.Wpf/Events/BackgroundTicked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

namespace ReflectionEventing.Demo.Wpf.Events;

public sealed record BackgroundTicked(int Value);
public sealed record BackgroundTicked(int Value) : ITickedEvent;
11 changes: 11 additions & 0 deletions src/ReflectionEventing.Demo.Wpf/Events/ITickedEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors.
// All Rights Reserved.

namespace ReflectionEventing.Demo.Wpf.Events;

public interface ITickedEvent
{
int Value { get; }
}
8 changes: 8 additions & 0 deletions src/ReflectionEventing.Demo.Wpf/Events/OtherEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors.
// All Rights Reserved.

namespace ReflectionEventing.Demo.Wpf.Events;

public class OtherEvent;
13 changes: 11 additions & 2 deletions src/ReflectionEventing.Demo.Wpf/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

namespace ReflectionEventing.Demo.Wpf.ViewModels;

public partial class MainWindowViewModel : ObservableObject, IConsumer<BackgroundTicked>
public partial class MainWindowViewModel
: ObservableObject,
IConsumer<ITickedEvent>,
IConsumer<OtherEvent>
{
[ObservableProperty]
private int _currentTick = 0;

/// <inheritdoc />
public async Task ConsumeAsync(BackgroundTicked payload, CancellationToken cancellationToken)
public async Task ConsumeAsync(ITickedEvent payload, CancellationToken cancellationToken)
{
int tickValue = payload.Value;

Expand All @@ -23,4 +26,10 @@ await Application.Current.Dispatcher.InvokeAsync(() =>
CurrentTick = tickValue;
});
}

/// <inheritdoc />
public async Task ConsumeAsync(OtherEvent payload, CancellationToken cancellationToken)
{
await Task.CompletedTask;
}
}
22 changes: 22 additions & 0 deletions src/ReflectionEventing/ConsumerTypesProviderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and ReflectionEventing Contributors.
// All Rights Reserved.

namespace ReflectionEventing;

/// <summary>
/// Provides extension methods for the <see cref="IConsumerTypesProvider"/> class.
/// </summary>
public static class ConsumerTypesProviderExtensions
{
/// <summary>
/// Gets the consumers for the specified event type.
/// </summary>
/// <typeparam name="TEvent">The type of the event to get the consumers for.</typeparam>
/// <returns>A collection of consumer types that can handle the specified event type.</returns>
public static IEnumerable<Type> GetConsumerTypes<TEvent>(this IConsumerTypesProvider provider)
{
return provider.GetConsumerTypes(typeof(TEvent));
}
}
2 changes: 1 addition & 1 deletion src/ReflectionEventing/EventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void Publish<TEvent>(TEvent eventItem)
public async Task PublishAsync<TEvent>(TEvent eventItem, CancellationToken cancellationToken)
{
List<Task> tasks = new();
IEnumerable<Type> consumerTypes = consumerTypesProvider.GetConsumerTypes<TEvent>();
IEnumerable<Type> consumerTypes = consumerTypesProvider.GetConsumerTypes(typeof(TEvent));

foreach (Type consumerType in consumerTypes)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ReflectionEventing/EventBusBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Type consumerType
{
Type consumedEventType = consumerInterface.GetGenericArguments()[0];

if (!consumers.ContainsKey(consumedEventType))
if (!consumers.ContainsKey(consumerType))
{
consumers[consumerType] = new HashSet<Type>();
}
Expand Down
27 changes: 25 additions & 2 deletions src/ReflectionEventing/HashedConsumerTypesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,31 @@ public class HashedConsumerTypesProvider(IDictionary<Type, IEnumerable<Type>> co
: IConsumerTypesProvider
{
/// <inheritdoc />
public IEnumerable<Type> GetConsumerTypes<TEvent>()
public IEnumerable<Type> GetConsumerTypes(Type eventType)
{
return consumers.Where(x => x.Value.Contains(typeof(TEvent))).Select(x => x.Key);
foreach (KeyValuePair<Type, IEnumerable<Type>> consumer in consumers)
{
if (consumer.Value.Contains(eventType))

Check warning on line 22 in src/ReflectionEventing/HashedConsumerTypesProvider.cs

View workflow job for this annotation

GitHub Actions / deploy

Possible null reference argument for parameter 'source' in 'bool Enumerable.Contains<Type>(IEnumerable<Type> source, Type value)'.
{
yield return consumer.Key;

continue;
}

// Fallback reflection
if (!consumer.Value.Any(x => ExtendsEvent(x, eventType)))
{
continue;
}

_ = ((HashSet<Type>)consumer.Value)?.Add(eventType);

Check warning on line 35 in src/ReflectionEventing/HashedConsumerTypesProvider.cs

View workflow job for this annotation

GitHub Actions / deploy

Converting null literal or possible null value to non-nullable type.

Check warning on line 35 in src/ReflectionEventing/HashedConsumerTypesProvider.cs

View workflow job for this annotation

GitHub Actions / deploy

Converting null literal or possible null value to non-nullable type.

Check warning on line 35 in src/ReflectionEventing/HashedConsumerTypesProvider.cs

View workflow job for this annotation

GitHub Actions / deploy

Converting null literal or possible null value to non-nullable type.

yield return consumer.Key;
}
}

private static bool ExtendsEvent(Type consumerType, Type eventType)
{
return consumerType.IsAssignableFrom(eventType);
}
}
3 changes: 1 addition & 2 deletions src/ReflectionEventing/IConsumerTypesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public interface IConsumerTypesProvider
/// <summary>
/// Gets the consumers for the specified event type.
/// </summary>
/// <typeparam name="TEvent">The type of the event to get the consumers for.</typeparam>
/// <returns>A collection of consumer types that can handle the specified event type.</returns>
IEnumerable<Type> GetConsumerTypes<TEvent>();
IEnumerable<Type> GetConsumerTypes(Type eventType);
}

0 comments on commit 047bbb2

Please sign in to comment.