Skip to content

Commit 748694a

Browse files
[+] Some comments
[+] EventMediator Unsubscribe CactuseSecurity#3145
1 parent bb7a116 commit 748694a

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed
Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
using FWO.Data;
21
using FWO.Services.EventMediator.Interfaces;
32

43
namespace FWO.Services.EventMediator;
54

65
public class EventMediator : IEventMediator
76
{
8-
private readonly Dictionary<Type, List<Action<IEvent>>> _handlers = [];
7+
private readonly Dictionary<Type, Dictionary<string, List<Action<IEvent>>>> _handlers = [];
98

10-
public void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEvent
9+
/// <summary>
10+
/// Adds the handler as an invokeable action.
11+
/// </summary>
12+
/// <typeparam name="TEvent"></typeparam>
13+
/// <param name="name"></param>
14+
/// <param name="handler"></param>
15+
public void Subscribe<TEvent>(string name, Action<TEvent> handler) where TEvent : class, IEvent
1116
{
1217
if(!_handlers.ContainsKey(typeof(TEvent)))
1318
{
1419
_handlers[typeof(TEvent)] = [];
1520
}
1621

17-
_handlers[typeof(TEvent)].Add(e => handler((TEvent)e));
22+
if(!_handlers[typeof(TEvent)].TryGetValue(name, out List<Action<IEvent>>? value))
23+
{
24+
value = [];
25+
_handlers[typeof(TEvent)][name] = value;
26+
}
27+
28+
value.Add(e => handler((TEvent)e));
1829
}
1930

20-
public void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent
31+
/// <summary>
32+
/// If matching subscription handler was found it will be invoked.
33+
/// </summary>
34+
/// <typeparam name="TEvent"></typeparam>
35+
/// <param name="name"></param>
36+
/// <param name="event"></param>
37+
public void Publish<TEvent>(string name, TEvent @event) where TEvent : class, IEvent
2138
{
22-
if(_handlers.TryGetValue(typeof(TEvent), out List<Action<IEvent>>? handlers))
39+
if(_handlers.TryGetValue(typeof(TEvent), out Dictionary<string, List<Action<IEvent>>>? actions)
40+
&& actions.TryGetValue(name, out List<Action<IEvent>>? handlers))
2341
{
2442
foreach(Action<IEvent> handler in handlers)
2543
{
@@ -28,9 +46,20 @@ public void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent
2846
}
2947
}
3048

31-
public void Unsubscribe<TEvent>(TEvent @event)
49+
/// <summary>
50+
/// Unsubscribe of ALL events of the given type matching the name.
51+
/// </summary>
52+
/// <typeparam name="TEvent"></typeparam>
53+
/// <param name="event"></param>
54+
/// <returns>True/False if remove was successfull</returns>
55+
public bool Unsubscribe<TEvent>(string name) where TEvent : class, IEvent
3256
{
57+
if(_handlers.ContainsKey(typeof(TEvent)))
58+
{
59+
return _handlers.Remove(typeof(TEvent));
60+
}
3361

62+
return false;
3463
}
3564
}
3665

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ namespace FWO.Services.EventMediator.Interfaces;
22

33
public interface IEventMediator
44
{
5-
void Subscribe<TEvent>(Action<TEvent> handler) where TEvent : class, IEvent;
6-
void Publish<TEvent>(TEvent @event) where TEvent : class, IEvent;
5+
void Subscribe<TEvent>(string name, Action<TEvent> handler) where TEvent : class, IEvent;
6+
void Publish<TEvent>(string name, TEvent @event) where TEvent : class, IEvent;
7+
bool Unsubscribe<TEvent>(string name) where TEvent : class, IEvent;
78
}
89

roles/ui/files/FWO.UI/Pages/NetworkModelling/EditAppRole.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@
296296
workInProgress = true;
297297
await AppRoleHandler.InitAppRole(newArea);
298298

299-
EventMediator.Publish<CollectionChangedEvent>(OnCollectionChanged);
299+
EventMediator.Publish<CollectionChangedEvent>(nameof(EditAppRole), OnCollectionChanged);
300300

301301
workInProgress = false;
302302
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@using System.Net
99
@using System.Reflection
1010
@inject IEventMediator EventMediator
11+
@implements IDisposable
1112

1213
@typeparam TCollectionItem
1314

@@ -118,7 +119,8 @@
118119
CssClass = "";
119120
}
120121

121-
EventMediator.Subscribe<CollectionChangedEvent>(new Action<CollectionChangedEvent>(_ => ReorderCollection()));
122+
EventMediator.Subscribe<CollectionChangedEvent>(nameof(Pages.NetworkModelling.EditAppRole),
123+
new Action<CollectionChangedEvent>(_ => ReorderCollection()));
122124

123125
initialized = true;
124126
}
@@ -195,7 +197,7 @@
195197
{
196198
throw new InvalidOperationException();
197199
}
198-
200+
199201
MemberExpression propertyAccess = Expression.Property(param, propertyInfo);
200202
UnaryExpression converted = Expression.Convert(propertyAccess, typeof(object));
201203

@@ -239,4 +241,10 @@
239241
throw new NotImplementedException();
240242
}
241243
}
244+
245+
246+
public void Dispose()
247+
{
248+
EventMediator.Unsubscribe<CollectionChangedEvent>(nameof(Pages.NetworkModelling.EditAppRole));
249+
}
242250
}

0 commit comments

Comments
 (0)