forked from NullTale/UnityEventBus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
72 lines (61 loc) · 2.91 KB
/
Extensions.cs
File metadata and controls
72 lines (61 loc) · 2.91 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace UnityEventBus
{
public static class Extensions
{
internal static Dictionary<Type, Type[]> s_SubscribersTypeCache = new Dictionary<Type, Type[]>();
internal static readonly DefaultSubscriberName s_DefaultSubscriberName = new DefaultSubscriberName();
internal static readonly DefaultSubscriberPriority s_DefaultSubscriberPriority = new DefaultSubscriberPriority();
public static readonly DefaultInvoker s_DefaultInvoker = new DefaultInvoker();
// =======================================================================
public class DefaultInvoker : IEventInvoker
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Invoke<TEvent>(in TEvent e, in ISubscriber listener)
{
((IListener<TEvent>)listener).React(in e);
}
}
public class DefaultInvokerConditional : IEventInvoker
{
public Func<ISubscriber, bool> m_Filter;
// =======================================================================
public void Invoke<TEvent>(in TEvent e, in ISubscriber listener)
{
if (m_Filter(listener))
((IListener<TEvent>)listener).React(in e);
}
}
// =======================================================================
public static void Send<TEvent>(this IEventBus bus, in TEvent e)
{
bus.Send(in e, in s_DefaultInvoker);
}
public static void Send<TEvent>(this IEventBus bus, in TEvent e, in Func<ISubscriber, bool> check)
{
bus.Send(in e, new DefaultInvokerConditional() { m_Filter = check });
}
public static void Send<TEvent>(this IListener<TEvent> listener, in TEvent e)
{
s_DefaultInvoker.Invoke(in e, listener);
}
public static IEnumerable<SubscriberWrapper> ExtractWrappers(this ISubscriber listener)
{
var listenerType = listener.GetType();
// try get cache
if (s_SubscribersTypeCache.TryGetValue(listenerType, out var types))
return types.Select(type => SubscriberWrapper.Create(listener, type));
// extract, get type arguments from implemented ISubscriber<> interfaces
types = listenerType.GetInterfaces()
.Where(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(ISubscriber<>))
.Select(n => n.GenericTypeArguments[0])
.ToArray();
// add to cache
s_SubscribersTypeCache.Add(listenerType, types);
return types.Select(type => SubscriberWrapper.Create(listener, type));
}
}
}