1
0
mirror of https://github.com/bitwarden/mobile.git synced 2024-11-04 08:50:18 +01:00

add locking to BroadcasterService

This commit is contained in:
Kyle Spearrin 2019-06-04 21:28:50 -04:00
parent f6f6253eab
commit 6c88d25941

View File

@ -9,37 +9,47 @@ namespace Bit.App.Services
public class BroadcasterService : IBroadcasterService
{
private readonly Dictionary<string, Action<Message>> _subscribers = new Dictionary<string, Action<Message>>();
private object _myLock = new object();
public void Send(Message message, string id = null)
{
if(!string.IsNullOrWhiteSpace(id))
lock(_myLock)
{
if(_subscribers.ContainsKey(id))
if(!string.IsNullOrWhiteSpace(id))
{
Task.Run(() => _subscribers[id].Invoke(message));
if(_subscribers.ContainsKey(id))
{
Task.Run(() => _subscribers[id].Invoke(message));
}
return;
}
foreach(var sub in _subscribers)
{
Task.Run(() => sub.Value.Invoke(message));
}
return;
}
foreach(var sub in _subscribers)
{
Task.Run(() => sub.Value.Invoke(message));
}
}
public void Subscribe(string id, Action<Message> messageCallback)
{
if(_subscribers.ContainsKey(id))
lock(_myLock)
{
return;
if(_subscribers.ContainsKey(id))
{
return;
}
_subscribers.Add(id, messageCallback);
}
_subscribers.Add(id, messageCallback);
}
public void Unsubscribe(string id)
{
if(_subscribers.ContainsKey(id))
lock(_myLock)
{
_subscribers.Remove(id);
if(_subscribers.ContainsKey(id))
{
_subscribers.Remove(id);
}
}
}
}