mirror of
https://github.com/bitwarden/android.git
synced 2024-12-24 01:48:25 +03:00
add locking to BroadcasterService
This commit is contained in:
parent
f6f6253eab
commit
6c88d25941
1 changed files with 23 additions and 13 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue