Skip to content

Commit fd64785

Browse files
committed
Merge pull request #3 from adamveld12/force_struct
force struct use for messaging closes #2
2 parents 7d877ea + 9842714 commit fd64785

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/UnityEventAggregator/EventAggregator.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public static class EventAggregator
1515
/// </summary>
1616
/// <typeparam name="T">The type of event being listened for.</typeparam>
1717
/// <param name="obj"></param>
18-
public static void Register<T>(this object obj)
18+
public static void Register<T>(this T obj) where T : struct
1919
{
20-
if (!_cache.ContainsKey(typeof(T))) _cache[typeof(T)] = new List<object>();
20+
if (!_cache.ContainsKey(typeof(T)))
21+
_cache[typeof(T)] = new List<object>();
22+
2123
_cache[typeof(T)].Add(obj);
2224
}
2325

@@ -26,7 +28,7 @@ public static void Register<T>(this object obj)
2628
/// </summary>
2729
/// <typeparam name="T">The type of event to no longer listen for.</typeparam>
2830
/// <param name="obj"></param>
29-
public static void UnRegister<T>(this object obj)
31+
public static void UnRegister<T>(this T obj) where T : struct
3032
{
3133
if (!_cache.ContainsKey(typeof(T))) return;
3234
_cache[typeof(T)].Remove(obj);
@@ -37,7 +39,7 @@ public static void UnRegister<T>(this object obj)
3739
/// </summary>
3840
/// <typeparam name="T">The type of event being notified.</typeparam>
3941
/// <param name="message"></param>
40-
public static void SendMessage<T>(T message)
42+
public static void SendMessage<T>(T message) where T : struct
4143
{
4244
_cache[message.GetType()].Each(x => ((IListener<T>)x).Handle(message));
4345
}
@@ -46,7 +48,7 @@ public static void SendMessage<T>(T message)
4648
/// Creates the cache for objects that listen to each event
4749
/// </summary>
4850
/// <typeparam name="T">Searches through all active GameObjects for those listening for event T and auto registers them.</typeparam>
49-
public static void UpdateCache<T>()
51+
public static void UpdateCache<T>() where T : struct
5052
{
5153
var type = typeof(IListener<T>);
5254
var list = new List<object>();

src/UnityEventAggregator/IListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace UnityEventAggregator
22
{
3-
public interface IListener<T>
3+
public interface IListener<T> where T : struct
44
{
55
void Handle(T message);
66
}

0 commit comments

Comments
 (0)