Skip to content

Commit 5a277ac

Browse files
.
1 parent 931f918 commit 5a277ac

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

roles/lib/files/FWO.Services/EventMediator/EventMediator.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using FWO.Data;
12
using FWO.Services.EventMediator.Interfaces;
23

34
namespace FWO.Services.EventMediator;
45

56
public class EventMediator : IEventMediator
67
{
78
private readonly Dictionary<Type, List<Action<IEvent>>> _handlers = [];
9+
private readonly Dictionary<object, List<Action<IEvent>>> ObjectHandlers = [];
810

911
public void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEvent
1012
{
@@ -18,13 +20,40 @@ public void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEve
1820

1921
public void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent
2022
{
21-
if(_handlers.TryGetValue(typeof(TEvent), out var handlers))
23+
if(_handlers.TryGetValue(typeof(TEvent), out List<Action<IEvent>>? handlers))
2224
{
23-
foreach(var handler in handlers)
25+
foreach(Action<IEvent> handler in handlers)
2426
{
2527
handler(@event);
2628
}
2729
}
2830
}
31+
32+
public void Subscribe<TEvent>(object sender, Action<TEvent> handler) where TEvent : class, IEvent
33+
{
34+
if(!ObjectHandlers.TryGetValue(sender, out List<Action<IEvent>>? value))
35+
{
36+
value = [];
37+
ObjectHandlers[sender] = value;
38+
}
39+
40+
value.Add(e => handler((TEvent)e));
41+
}
42+
43+
public void Publish<TEvent>(object sender, TEvent @event) where TEvent : class, IEvent
44+
{
45+
if(ObjectHandlers.TryGetValue(sender, out List<Action<IEvent>>? handlers))
46+
{
47+
foreach(Action<IEvent> handler in handlers)
48+
{
49+
handler(@event);
50+
}
51+
}
52+
}
53+
54+
public void Unsubscribe<TEvent>(TEvent @event)
55+
{
56+
57+
}
2958
}
3059

roles/lib/files/FWO.Services/EventMediator/Events/CollectionChangedEvent.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
namespace FWO.Services.EventMediator.Events;
44

5-
public class CollectionChangedEvent : IEvent { }
5+
public class CollectionChangedEvent() : IEvent
6+
{
7+
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace FWO.Services.EventMediator.Events;
2+
3+
public class CollectionChangedEventArgs(object? Sender, IEnumerable<dynamic>? Collection) : EventArgs
4+
{
5+
6+
}
7+

roles/lib/files/FWO.Services/EventMediator/Interfaces/IEventMediator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ public interface IEventMediator
44
{
55
void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEvent;
66
void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent;
7+
void Subscribe<TEvent>(object sender, Action<TEvent> handler) where TEvent : class, IEvent;
8+
void Publish<TEvent>(object sender, TEvent @event) where TEvent : class, IEvent;
79
}
810

roles/ui/files/FWO.UI/Shared/OrderByDropdown.razor

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
CssClass = "";
119119
}
120120

121-
EventMediator.Subscribe<CollectionChangedEvent>(new Action<CollectionChangedEvent>(_ => ReorderCollection()));
121+
EventMediator.Subscribe<CollectionChangedEvent>(this, new Action<CollectionChangedEvent>(_ => ReorderCollection()));
122122

123123
initialized = true;
124124
}
@@ -149,7 +149,6 @@
149149
if (initialized)
150150
{
151151
InvokeAsync(StateHasChanged);
152-
153152
}
154153

155154
CollectionReordered.InvokeAsync(Collection);

0 commit comments

Comments
 (0)