thread safe message manager

pull/5/head
Evan Hemsley 2019-07-28 19:28:47 -07:00
parent 539d6de247
commit f3a7331ae9
1 changed files with 5 additions and 4 deletions

View File

@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -6,12 +7,12 @@ namespace Encompass
{ {
internal class MessageManager internal class MessageManager
{ {
private readonly Dictionary<Type, List<IMessage>> messageTypeToMessages = new Dictionary<Type, List<IMessage>>(); private readonly Dictionary<Type, ConcurrentStack<IMessage>> messageTypeToMessages = new Dictionary<Type, ConcurrentStack<IMessage>>();
internal void RegisterMessageType(Type messageType) internal void RegisterMessageType(Type messageType)
{ {
if (!messageTypeToMessages.ContainsKey(messageType)) { if (!messageTypeToMessages.ContainsKey(messageType)) {
messageTypeToMessages.Add(messageType, new List<IMessage>()); messageTypeToMessages.Add(messageType, new ConcurrentStack<IMessage>());
} }
} }
@ -19,12 +20,12 @@ namespace Encompass
{ {
var type = message.GetType(); var type = message.GetType();
messageTypeToMessages[type].Add(message); messageTypeToMessages[type].Push(message);
} }
internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage internal void AddMessage<TMessage>(TMessage message) where TMessage : struct, IMessage
{ {
messageTypeToMessages[typeof(TMessage)].Add(message); messageTypeToMessages[typeof(TMessage)].Push(message);
} }
internal void ClearMessages() internal void ClearMessages()