register message types at init time instead of runtime

pull/5/head
Evan Hemsley 2019-07-28 19:17:00 -07:00
parent 36621c2fe7
commit 539d6de247
3 changed files with 15 additions and 11 deletions

2
TODO
View File

@ -2,3 +2,5 @@
- docs
- emit two packages: one for dev, which includes expensive runtime validation checks, and one for release, which disables them
- thread safety

View File

@ -8,25 +8,22 @@ namespace Encompass
{
private readonly Dictionary<Type, List<IMessage>> messageTypeToMessages = new Dictionary<Type, List<IMessage>>();
internal void RegisterMessageType(Type messageType)
{
if (!messageTypeToMessages.ContainsKey(messageType)) {
messageTypeToMessages.Add(messageType, new List<IMessage>());
}
}
internal void AddMessage(IMessage message)
{
var type = message.GetType();
if (!messageTypeToMessages.ContainsKey(type))
{
messageTypeToMessages.Add(type, new List<IMessage>());
}
messageTypeToMessages[type].Add(message);
}
internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{
if (!messageTypeToMessages.ContainsKey(typeof(TMessage)))
{
messageTypeToMessages.Add(typeof(TMessage), new List<IMessage>());
}
messageTypeToMessages[typeof(TMessage)].Add(message);
}

View File

@ -91,6 +91,11 @@ namespace Encompass
var messageReceiveTypes = engine.receiveTypes;
var messageSendTypes = engine.sendTypes;
foreach (var messageType in messageReceiveTypes.Union(messageSendTypes))
{
messageManager.RegisterMessageType(messageType);
}
foreach (var messageType in messageReceiveTypes.Intersect(messageSendTypes))
{
if ((messageType.IsGenericType && messageType.GetGenericTypeDefinition() == typeof(PendingComponentMessage<>)))