1
0
mirror of https://github.com/bitwarden/mobile synced 2026-01-06 02:23:57 +00:00

re-worked message sending

This commit is contained in:
Kyle Spearrin
2019-04-19 12:29:37 -04:00
parent 4b7366e9b3
commit 7c1549bb95
8 changed files with 79 additions and 21 deletions

View File

@@ -1,11 +1,12 @@
using System;
using Bit.Core.Models.Domain;
using System;
namespace Bit.Core.Abstractions
{
public interface IBroadcasterService
{
void Send<T>(T message, string id = null);
void Subscribe<T>(string id, Action<T> messageCallback);
void Send(Message message, string id = null);
void Subscribe(string id, Action<Message> messageCallback);
void Unsubscribe(string id);
}
}

View File

@@ -2,6 +2,6 @@
{
public interface IMessagingService
{
void Send<T>(string subscriber, T arg = default(T));
void Send(string subscriber, object arg = null);
}
}

View File

@@ -0,0 +1,8 @@
namespace Bit.Core.Models.Domain
{
public class Message
{
public string Command { get; set; }
public object Data { get; set; }
}
}

View File

@@ -140,7 +140,7 @@ namespace Bit.Core.Services
public void LogOut(Action callback)
{
callback.Invoke();
_messagingService.Send<object>("loggedOut");
_messagingService.Send("loggedOut");
}
public List<TwoFactorProvider> GetSupportedTwoFactorProviders()
@@ -312,7 +312,7 @@ namespace Bit.Core.Services
await _cryptoService.SetEncPrivateKeyAsync(tokenResponse.PrivateKey);
}
_messagingService.Send<object>("loggedIn");
_messagingService.Send("loggedIn");
return result;
}

View File

@@ -0,0 +1,45 @@
using Bit.Core.Abstractions;
using Bit.Core.Models.Domain;
using System;
using System.Collections.Generic;
namespace Bit.App.Services
{
public class BroadcasterService : IBroadcasterService
{
private readonly Dictionary<string, Action<Message>> _subscribers = new Dictionary<string, Action<Message>>();
public void Send(Message message, string id = null)
{
if(!string.IsNullOrWhiteSpace(id))
{
if(_subscribers.ContainsKey(id))
{
_subscribers[id].Invoke(message);
}
return;
}
foreach(var sub in _subscribers)
{
sub.Value.Invoke(message);
}
}
public void Subscribe(string id, Action<Message> messageCallback)
{
if(_subscribers.ContainsKey(id))
{
return;
}
_subscribers.Add(id, messageCallback);
}
public void Unsubscribe(string id)
{
if(_subscribers.ContainsKey(id))
{
_subscribers.Remove(id);
}
}
}
}

View File

@@ -255,7 +255,7 @@ namespace Bit.Core.Services
private void SyncStarted()
{
SyncInProgress = true;
_messagingService.Send<object>("syncStarted");
_messagingService.Send("syncStarted");
}
private bool SyncCompleted(bool successfully)