forked from NullTale/UnityEventBus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscriberBusWrapper.cs
More file actions
95 lines (84 loc) · 2.98 KB
/
SubscriberBusWrapper.cs
File metadata and controls
95 lines (84 loc) · 2.98 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace UnityEventBus
{
internal sealed class SubscriberBusWrapper : IDisposable, ISubscriberWrapper
{
internal static Stack<SubscriberBusWrapper> s_WrappersPool = new Stack<SubscriberBusWrapper>(512);
private ISubscriberName m_Name;
private ISubscriberPriority m_Priority;
internal bool IsActive;
public IEventBus Bus;
public string Name => m_Name.Name;
public ISubscriber Target => Bus;
public int Order => m_Priority.Priority;
public int Index { get; private set; }
// =======================================================================
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Invoke<TEvent, TInvoker>(in TEvent e, in TInvoker invoker) where TInvoker : IEventInvoker
{
#if DEBUG
Bus.Send(in e, in invoker);
#else
try
{
Bus.Send(in e, in invoker);
}
catch (Exception exception)
{
UnityEngine.Debug.LogError($"{this}; Exception: {exception}");
}
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public SubscriberBusWrapper(in IEventBus bus, int index)
{
Setup(bus, index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Setup(in IEventBus bus, int index)
{
Index = index;
IsActive = true;
Bus = bus;
m_Name = bus as ISubscriberName ?? Extensions.s_DefaultSubscriberName;
m_Priority = bus as ISubscriberPriority ?? Extensions.s_DefaultSubscriberPriority;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj)
{
return Bus == ((SubscriberBusWrapper)obj).Bus;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode()
{
return Bus.GetHashCode();
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
IsActive = false;
Bus = null;
m_Name = null;
m_Priority = null;
s_WrappersPool.Push(this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SubscriberBusWrapper Create(in IEventBus bus, int index)
{
if (s_WrappersPool.Count > 0)
{
var wrapper = s_WrappersPool.Pop();
wrapper.Setup(in bus, index);
return wrapper;
}
else
return new SubscriberBusWrapper(in bus, index);
}
public override string ToString()
{
return $"Bus: <Name> {Name}, <Type> {Bus.GetType()}";
}
}
}