-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHandlerCollection.cs
66 lines (54 loc) · 1.9 KB
/
HandlerCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Salday.EventBus.Interfaces;
using System.Collections.Generic;
using System.Linq;
namespace Salday.EventBus
{
internal class HandlerCollection<TEvent> : IHandlerCollection<TEvent> where TEvent : EventBase
{
public IEventBus EvBus { get; }
IList<IHandler<TEvent>> _handlers = new List<IHandler<TEvent>>();
public HandlerCollection(IEventBus evBus)
{
this.EvBus = evBus;
}
public HandlerCollection(IEventBus evBus, IList<IHandler<TEvent>> handlers) : this(evBus)
{
this._handlers = handlers;
}
public void Handle(TEvent eventObject)
{
foreach (var t in _handlers)
{
t?.Handle(eventObject);
}
}
public void RemoveSubscription(ISubscription supscription)
{
var handlersToRemove = supscription.Handlers[typeof(TEvent)];
//Copy handlers over, to prevent handler collection change upon removal
var modifiedHandlerCollection = new List<IHandler<TEvent>>(_handlers);
for (var i = 0; i < modifiedHandlerCollection.Count; i++)
{
foreach (var t in handlersToRemove)
{
if (t as IHandler<TEvent> == _handlers[i])
{
modifiedHandlerCollection.RemoveAt(i);
}
}
}
if (_handlers.Count == 0) EvBus.RemoveType(typeof(TEvent));
else
_handlers = modifiedHandlerCollection;
}
public void AddHandlers(IList<IHandler> list)
{
var casted = list.Cast<IHandler<TEvent>>().ToList();
foreach (var handler in casted)
{
_handlers.Add(handler);
}
_handlers = _handlers.OrderByDescending(h => h.Priority).ToList();
}
}
}