Skip to content

Commit 6418515

Browse files
committed
Fix LiteDb connections management, update version to 1.0.25
1 parent 7c15715 commit 6418515

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

Beacon.Sdk/Beacon.Sdk.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Authors>Mikhail Tatarenko</Authors>
1515
<Product>Beacon.Sdk</Product>
1616
<Description>Beacon .NET SDK for Tezos wallet / dApps developers.</Description>
17-
<Version>1.0.24</Version>
17+
<Version>1.0.25</Version>
1818
<Copyright>Copyright © Baking Bad 2019-2022</Copyright>
1919
<Nullable>enable</Nullable>
2020
<TargetFramework>netstandard2.1</TargetFramework>

Beacon.Sdk/BeaconClients/Abstract/IBaseBeaconClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IBaseBeaconClient
99
{
1010
event EventHandler<BeaconMessageEventArgs> OnBeaconMessageReceived;
1111
event EventHandler<ConnectedClientsListChangedEventArgs?> OnConnectedClientsListChanged;
12+
event Action OnDisconnected;
1213

1314
Task InitAsync();
1415
void Connect();

Beacon.Sdk/BeaconClients/BaseBeaconClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public abstract class BaseBeaconClient : IBaseBeaconClient
2626
private readonly KeyPairService _keyPairService;
2727
public event EventHandler<BeaconMessageEventArgs> OnBeaconMessageReceived;
2828
public event EventHandler<ConnectedClientsListChangedEventArgs?> OnConnectedClientsListChanged;
29+
public event Action OnDisconnected;
2930

3031
protected void RaiseOnBeaconMessageReceived(BeaconMessageEventArgs e)
3132
{
@@ -122,6 +123,8 @@ public void Disconnect()
122123
P2PCommunicationService.OnP2PMessagesReceived -= OnP2PMessagesReceived;
123124
SerializeMessageHandler.OnPermissionsCreated -= ClientPermissionsCreatedHandler;
124125
Connected = P2PCommunicationService.Syncing;
126+
127+
OnDisconnected?.Invoke();
125128
}
126129

127130
private void ClientPermissionsCreatedHandler(object sender, ConnectedClientsListChangedEventArgs e)
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace Beacon.Sdk.BeaconClients
22
{
33
using Abstract;
4+
using Core.Infrastructure;
45
using Microsoft.Extensions.DependencyInjection;
56
using Microsoft.Extensions.Logging;
67

@@ -9,7 +10,6 @@ public static class BeaconClientFactory
910
public static T Create<T>(
1011
BeaconOptions? options,
1112
ILoggerProvider? loggerProvider = null) where T : notnull
12-
1313
{
1414
var beaconServices = new ServiceCollection();
1515

@@ -19,8 +19,23 @@ public static T Create<T>(
1919
if (typeof(T) == typeof(IDappBeaconClient))
2020
beaconServices.AddBeaconDappClient(options, loggerProvider);
2121

22-
ServiceProvider? beaconServicesProvider = beaconServices.BuildServiceProvider();
23-
return beaconServicesProvider.GetRequiredService<T>();
22+
var beaconServicesProvider = beaconServices.BuildServiceProvider();
23+
var service = beaconServicesProvider.GetRequiredService<T>();
24+
25+
if (service is IBaseBeaconClient client)
26+
{
27+
var connectionPool = beaconServicesProvider.GetRequiredService<ILiteDbConnectionPool>();
28+
29+
if (connectionPool != null)
30+
{
31+
client.OnDisconnected += () =>
32+
{
33+
connectionPool.CloseAllConnections(); // close all LiteDb connections after disconnected event
34+
};
35+
}
36+
}
37+
38+
return service;
2439
}
2540
}
2641
}

Beacon.Sdk/Core/Infrastructure/Repositories/BaseLiteDbRepository.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ namespace Beacon.Sdk.Core.Infrastructure.Repositories
99
public abstract class BaseLiteDbRepository<T>
1010
{
1111
private readonly ILogger<BaseLiteDbRepository<T>> _logger;
12-
private readonly ILiteDatabase _db;
12+
private readonly ILiteDbConnectionPool _connectionPool;
13+
private readonly RepositorySettings _settings;
1314

14-
protected BaseLiteDbRepository(ILiteDbConnectionPool connectionPool, ILogger<BaseLiteDbRepository<T>> logger, RepositorySettings settings)
15+
protected BaseLiteDbRepository(
16+
ILiteDbConnectionPool connectionPool,
17+
ILogger<BaseLiteDbRepository<T>> logger,
18+
RepositorySettings settings)
1519
{
20+
_connectionPool = connectionPool ?? throw new ArgumentNullException(nameof(connectionPool));
21+
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
1622
_logger = logger;
17-
_db = connectionPool.OpenConnection(new ConnectionString(settings.ConnectionString));
1823
}
1924

2025
protected Task InConnectionAction(string collectionName, Action<ILiteCollection<T>> func)
2126
{
2227
try
2328
{
24-
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
29+
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
30+
var col = db.GetCollection<T>(collectionName);
2531
func(col);
2632
}
2733
catch (Exception e)
@@ -36,7 +42,8 @@ protected Task<T> InConnection(string collectionName, Func<ILiteCollection<T>, T
3642
{
3743
try
3844
{
39-
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
45+
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
46+
var col = db.GetCollection<T>(collectionName);
4047
return func(col);
4148
}
4249
catch (Exception e)
@@ -50,8 +57,9 @@ protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCo
5057
{
5158
try
5259
{
53-
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
54-
func(_db, col);
60+
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
61+
var col = db.GetCollection<T>(collectionName);
62+
func(db, col);
5563
}
5664
catch (Exception e)
5765
{
@@ -65,7 +73,8 @@ protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCo
6573
{
6674
try
6775
{
68-
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
76+
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
77+
var col = db.GetCollection<T>(collectionName);
6978
return func(col);
7079

7180
}
@@ -81,7 +90,8 @@ protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCo
8190
{
8291
try
8392
{
84-
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
93+
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
94+
var col = db.GetCollection<T>(collectionName);
8595
return func(col);
8696

8797
}
@@ -97,7 +107,8 @@ protected Task<List<T>> InConnection(string collectionName, Func<ILiteCollection
97107
{
98108
try
99109
{
100-
ILiteCollection<T> col = _db.GetCollection<T>(collectionName);
110+
var db = _connectionPool.OpenConnection(new ConnectionString(_settings.ConnectionString));
111+
var col = db.GetCollection<T>(collectionName);
101112
return func(col);
102113
}
103114
catch (Exception e)

0 commit comments

Comments
 (0)