diff --git a/Beacon.Sdk/Beacon.Sdk.csproj b/Beacon.Sdk/Beacon.Sdk.csproj index 76e0d35..d94d4e0 100644 --- a/Beacon.Sdk/Beacon.Sdk.csproj +++ b/Beacon.Sdk/Beacon.Sdk.csproj @@ -14,7 +14,7 @@ Mikhail Tatarenko Beacon.Sdk Beacon .NET SDK for Tezos wallet / dApps developers. - 1.0.24 + 1.0.25 Copyright © Baking Bad 2019-2022 enable netstandard2.1 diff --git a/Beacon.Sdk/BeaconClients/Abstract/IBaseBeaconClient.cs b/Beacon.Sdk/BeaconClients/Abstract/IBaseBeaconClient.cs index cfaf8fd..5e6a22d 100644 --- a/Beacon.Sdk/BeaconClients/Abstract/IBaseBeaconClient.cs +++ b/Beacon.Sdk/BeaconClients/Abstract/IBaseBeaconClient.cs @@ -9,6 +9,7 @@ public interface IBaseBeaconClient { event EventHandler OnBeaconMessageReceived; event EventHandler OnConnectedClientsListChanged; + event Action OnDisconnected; Task InitAsync(); void Connect(); diff --git a/Beacon.Sdk/BeaconClients/BaseBeaconClient.cs b/Beacon.Sdk/BeaconClients/BaseBeaconClient.cs index b73ff3f..7abcf23 100644 --- a/Beacon.Sdk/BeaconClients/BaseBeaconClient.cs +++ b/Beacon.Sdk/BeaconClients/BaseBeaconClient.cs @@ -26,6 +26,7 @@ public abstract class BaseBeaconClient : IBaseBeaconClient private readonly KeyPairService _keyPairService; public event EventHandler OnBeaconMessageReceived; public event EventHandler OnConnectedClientsListChanged; + public event Action OnDisconnected; protected void RaiseOnBeaconMessageReceived(BeaconMessageEventArgs e) { @@ -122,6 +123,8 @@ public void Disconnect() P2PCommunicationService.OnP2PMessagesReceived -= OnP2PMessagesReceived; SerializeMessageHandler.OnPermissionsCreated -= ClientPermissionsCreatedHandler; Connected = P2PCommunicationService.Syncing; + + OnDisconnected?.Invoke(); } private void ClientPermissionsCreatedHandler(object sender, ConnectedClientsListChangedEventArgs e) diff --git a/Beacon.Sdk/BeaconClients/BeaconClientFactory.cs b/Beacon.Sdk/BeaconClients/BeaconClientFactory.cs index c258a96..184bb63 100644 --- a/Beacon.Sdk/BeaconClients/BeaconClientFactory.cs +++ b/Beacon.Sdk/BeaconClients/BeaconClientFactory.cs @@ -1,6 +1,7 @@ namespace Beacon.Sdk.BeaconClients { using Abstract; + using Core.Infrastructure; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -9,7 +10,6 @@ public static class BeaconClientFactory public static T Create( BeaconOptions? options, ILoggerProvider? loggerProvider = null) where T : notnull - { var beaconServices = new ServiceCollection(); @@ -19,8 +19,23 @@ public static T Create( if (typeof(T) == typeof(IDappBeaconClient)) beaconServices.AddBeaconDappClient(options, loggerProvider); - ServiceProvider? beaconServicesProvider = beaconServices.BuildServiceProvider(); - return beaconServicesProvider.GetRequiredService(); + var beaconServicesProvider = beaconServices.BuildServiceProvider(); + var service = beaconServicesProvider.GetRequiredService(); + + if (service is IBaseBeaconClient client) + { + var connectionPool = beaconServicesProvider.GetRequiredService(); + + if (connectionPool != null) + { + client.OnDisconnected += () => + { + connectionPool.CloseAllConnections(); // close all LiteDb connections after disconnected event + }; + } + } + + return service; } } } \ No newline at end of file diff --git a/Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs b/Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs index c65eb70..c23b44f 100644 --- a/Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs +++ b/Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs @@ -9,19 +9,25 @@ namespace Beacon.Sdk.Core.Infrastructure.Repositories public abstract class BaseLiteDbRepository { private readonly ILogger> _logger; - private readonly ILiteDatabase _db; + private readonly ILiteDbConnectionPool _connectionPool; + private readonly RepositorySettings _settings; - protected BaseLiteDbRepository(ILiteDbConnectionPool connectionPool, ILogger> logger, RepositorySettings settings) + protected BaseLiteDbRepository( + ILiteDbConnectionPool connectionPool, + ILogger> logger, + RepositorySettings settings) { + _connectionPool = connectionPool ?? throw new ArgumentNullException(nameof(connectionPool)); + _settings = settings ?? throw new ArgumentNullException(nameof(settings)); _logger = logger; - _db = connectionPool.OpenConnection(new ConnectionString(settings.ConnectionString)); } protected Task InConnectionAction(string collectionName, Action> func) { try { - ILiteCollection col = _db.GetCollection(collectionName); + var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString)); + var col = db.GetCollection(collectionName); func(col); } catch (Exception e) @@ -36,7 +42,8 @@ protected Task InConnection(string collectionName, Func, T { try { - ILiteCollection col = _db.GetCollection(collectionName); + var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString)); + var col = db.GetCollection(collectionName); return func(col); } catch (Exception e) @@ -50,8 +57,9 @@ protected Task InConnection(string collectionName, Action col = _db.GetCollection(collectionName); - func(_db, col); + var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString)); + var col = db.GetCollection(collectionName); + func(db, col); } catch (Exception e) { @@ -65,7 +73,8 @@ protected Task InConnection(string collectionName, Action col = _db.GetCollection(collectionName); + var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString)); + var col = db.GetCollection(collectionName); return func(col); } @@ -81,7 +90,8 @@ protected Task InConnection(string collectionName, Action col = _db.GetCollection(collectionName); + var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString)); + var col = db.GetCollection(collectionName); return func(col); } @@ -97,7 +107,8 @@ protected Task> InConnection(string collectionName, Func col = _db.GetCollection(collectionName); + var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString)); + var col = db.GetCollection(collectionName); return func(col); } catch (Exception e)