forked from NullTale/UnityEventBus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericListener.cs
More file actions
52 lines (44 loc) · 1.3 KB
/
GenericListener.cs
File metadata and controls
52 lines (44 loc) · 1.3 KB
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
using System;
using System.Collections.Generic;
namespace UnityEventBus
{
public sealed class GenericListener<T> : IListener<T>, ISubscriberOptions, IDisposable
{
private string m_Name;
private int m_Order;
public string Name => m_Name;
public int Priority => m_Order;
private List<IEventBus> m_Subscriptions = new List<IEventBus>(1);
private Action<T> m_Reaction;
// =======================================================================
public GenericListener(Action<T> reaction, int order, string name)
{
m_Reaction = reaction;
m_Order = order;
m_Name = name;
}
public void Subscribe(IEventBus bus)
{
if (m_Subscriptions.Contains(bus))
return;
m_Subscriptions.Add(bus);
bus.Subscribe(this);
}
public void UnSubscribe(IEventBus bus)
{
if (m_Subscriptions.Remove(bus))
bus.UnSubscribe(this);
}
public void Dispose()
{
foreach (var bus in m_Subscriptions)
{
bus.UnSubscribe(this);
}
}
public void React(in T e)
{
m_Reaction(e);
}
}
}