Make state messages thread safe

This commit is contained in:
Frank A. Krueger 2017-06-15 17:28:49 -07:00
parent f05a816ce5
commit abab07b8e6
1 changed files with 15 additions and 7 deletions

View File

@ -18,7 +18,13 @@ namespace Ooui
public event Action<Message> MessageSent; public event Action<Message> MessageSent;
public IEnumerable<Message> StateMessages => stateMessages; public IEnumerable<Message> StateMessages {
get {
lock (stateMessages) {
return new List<Message> (stateMessages);
}
}
}
protected EventTarget (string tagName) protected EventTarget (string tagName)
{ {
@ -128,10 +134,10 @@ namespace Ooui
protected void SaveStateMessage (Message message) protected void SaveStateMessage (Message message)
{ {
stateMessages.Add (message); lock (stateMessages) stateMessages.Add (message);
} }
protected void ReplaceStateMessage (Message old, Message message) void LockedReplaceStateMessage (Message old, Message message)
{ {
if (old != null) { if (old != null) {
stateMessages.Remove (old); stateMessages.Remove (old);
@ -147,10 +153,12 @@ namespace Ooui
break; break;
case MessageType.Set: case MessageType.Set:
{ {
lock (stateMessages) {
var old = stateMessages.FirstOrDefault ( var old = stateMessages.FirstOrDefault (
x => x.MessageType == MessageType.Set && x => x.MessageType == MessageType.Set &&
x.Key == message.Key); x.Key == message.Key);
ReplaceStateMessage (old, message); LockedReplaceStateMessage (old, message);
}
} }
break; break;
case MessageType.Listen: case MessageType.Listen: