-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessagingSystem.cs
103 lines (90 loc) · 2.72 KB
/
MessagingSystem.cs
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
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
public static class Postbox
{
public partial class Key_Internal
{
public MessageTopics Topic { get; set; }
public Type PayloadType { get; set; }
public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}
return obj is Key_Internal otherKey
? otherKey.Topic.Equals(Topic) && otherKey.PayloadType.Equals(PayloadType)
: false;
}
public override int GetHashCode() => HashCode.Combine(Topic, PayloadType);
}
public partial class Value_Internal
{
public int Handle { get; set; }
public Action<object> Callback { get; set; }
}
// Main reason for this class is to avoid having to go through
// every entry inside the dictionary since the keys are made out of
// topics and the types
public partial class Subscriber
{
private Key_Internal _storedKey;
private int _handle;
static Subscriber()
{
// Yoinked from
// https://stackoverflow.com/questions/1664793/how-to-restrict-access-to-nested-class-member-to-enclosing-class
HiddenSubscriberConstructor = (x, y) => new Subscriber(x, y);
}
private Subscriber(Key_Internal key, int handle)
{
_storedKey = key;
_handle = handle;
}
public void Unsubscribe()
{
_subscribers[_storedKey].RemoveAll(x => x.Handle == _handle);
}
}
private static Dictionary<Key_Internal, List<Value_Internal>> _subscribers =
new Dictionary<Key_Internal, List<Value_Internal>>();
private static int _handleCounter = -1;
private static Func<Key_Internal, int, Subscriber> HiddenSubscriberConstructor;
public static void Publish<T>(MessageTopics topic, T payload)
{
// This should allow for different payloads to be reached on the same topic.
var key = new Key_Internal() { Topic = topic, PayloadType = typeof(T) };
if (_subscribers.ContainsKey(key))
{
_subscribers[key].ForEach(x => x.Callback(payload));
}
}
public static Subscriber Subscribe<T>(MessageTopics topic, Action<T> callback)
{
var key = new Key_Internal() { Topic = topic, PayloadType = typeof(T) };
// Wraps the callback into a lambda and stores that instead.
// Yoinked from https://stackoverflow.com/questions/3444246/convert-actiont-to-actionobject
var value = new Value_Internal()
{
Handle = ++_handleCounter,
Callback = (Action<object>)(o => callback((T)o))
};
if (!_subscribers.ContainsKey(key))
{
_subscribers.Add(key, new List<Value_Internal>() { value });
}
else
{
_subscribers[key].Add(value);
}
if (HiddenSubscriberConstructor is null)
{
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(Subscriber).TypeHandle);
}
return HiddenSubscriberConstructor(key, value.Handle);
}
}
public enum MessageTopics
{
DamageDealt,
}