forked from NullTale/UnityEventBus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvent.cs
More file actions
56 lines (47 loc) · 1.5 KB
/
Event.cs
File metadata and controls
56 lines (47 loc) · 1.5 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
using System.Linq;
namespace UnityEventBus
{
/// <summary> Base event helper class </summary>
public interface IEventBase { }
/// <summary> Event with key only </summary>
/// <typeparam name="TKey"> Type of event key </typeparam>
public interface IEvent<out TKey> : IEventBase
{
TKey Key { get; }
}
/// <summary> Event with key and custom data </summary>
/// <typeparam name="TData"> Type of event data </typeparam>
public interface IEventData<out TData> : IEventBase
{
TData Data { get; }
}
/// <summary> Base event class </summary>
internal class Event<TKey> : IEvent<TKey>
{
public TKey Key { get; }
// =======================================================================
public Event(in TKey key)
{
Key = key;
}
public override string ToString()
{
return Key.ToString();
}
}
/// <summary> Event with data </summary>
internal class EventData<TKey, TData> : Event<TKey>, IEventData<TData>
{
public TData Data { get; }
// =======================================================================
public EventData(in TKey key, in TData data)
: base(in key)
{
Data = data;
}
public override string ToString()
{
return $"{Key} {(typeof(TData) == typeof(object[]) ? (Data as object[])?.Aggregate("", (s, o) => s + " " + o) : " " + Data)}";
}
}
}