mirror of
https://github.com/bitwarden/mobile
synced 2026-01-09 03:53:15 +00:00
Improved BroadcastService and added try...catch on async void callbacks (#1917)
This commit is contained in:
committed by
GitHub
parent
dbc1e5ea3e
commit
448cce38e1
@@ -5,7 +5,8 @@ namespace Bit.Core.Abstractions
|
||||
{
|
||||
public interface IBroadcasterService
|
||||
{
|
||||
void Send(Message message, string id = null);
|
||||
void Send(Message message);
|
||||
void Send(Message message, string id);
|
||||
void Subscribe(string id, Action<Message> messageCallback);
|
||||
void Unsubscribe(string id);
|
||||
}
|
||||
|
||||
@@ -8,24 +8,58 @@ namespace Bit.App.Services
|
||||
{
|
||||
public class BroadcasterService : IBroadcasterService
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
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)
|
||||
public BroadcasterService(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void Send(Message message)
|
||||
{
|
||||
lock (_myLock)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
if (_subscribers.ContainsKey(id))
|
||||
{
|
||||
Task.Run(() => _subscribers[id].Invoke(message));
|
||||
}
|
||||
return;
|
||||
}
|
||||
foreach (var sub in _subscribers)
|
||||
{
|
||||
Task.Run(() => sub.Value.Invoke(message));
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
sub.Value(message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Exception(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Send(Message message, string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_myLock)
|
||||
{
|
||||
if (_subscribers.TryGetValue(id, out var action))
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
action(message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Exception(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,14 +68,7 @@ namespace Bit.App.Services
|
||||
{
|
||||
lock (_myLock)
|
||||
{
|
||||
if (_subscribers.ContainsKey(id))
|
||||
{
|
||||
_subscribers[id] = messageCallback;
|
||||
}
|
||||
else
|
||||
{
|
||||
_subscribers.Add(id, messageCallback);
|
||||
}
|
||||
_subscribers[id] = messageCallback;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user