-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventsBus.cs
45 lines (41 loc) · 1.29 KB
/
EventsBus.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
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace DIY
{
/// <summary>
/// Represents dictionary with events, linked between publishers and subscribers.
/// </summary>
public class EventsBus
{
private ConcurrentDictionary<Type, IList> _events = new ConcurrentDictionary<Type, IList>();
/// <summary>
/// Adds a given subscriber to a chain of event-handlers.
/// </summary>
public void Subscribe<TEvent>(Action<TEvent> handler)
{
var handlers = GetOrAdd<TEvent>();
handlers.Add(handler);
}
/// <summary>
/// Triggers all subscribers of a given event.
/// </summary>
public void Publish<TEvent>(TEvent e)
{
var handlers = GetOrAdd<TEvent>();
foreach (var handler in handlers)
{
handler(e);
}
}
/// <summary>
/// Returns a list of event-handlers, subscribed to a given event.
/// </summary>
private List<Action<TEvent>> GetOrAdd<TEvent>()
{
var handlers = _events.GetOrAdd(typeof(TEvent), _ => new List<Action<TEvent>>());
return handlers as List<Action<TEvent>>;
}
}
}