| | 1 | | using NostrSure.Infrastructure.Client.Abstractions; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | |
|
| | 4 | | namespace NostrSure.Infrastructure.Client.Implementation; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// In-memory subscription manager |
| | 8 | | /// </summary> |
| | 9 | | public class InMemorySubscriptionManager : ISubscriptionManager |
| | 10 | | { |
| 45 | 11 | | private readonly ConcurrentDictionary<string, DateTime> _subscriptions = new(); |
| 45 | 12 | | private int _subscriptionCounter = 0; |
| | 13 | |
|
| | 14 | | public string NewSubscriptionId() |
| 3 | 15 | | { |
| 3 | 16 | | var id = $"sub_{Interlocked.Increment(ref _subscriptionCounter)}_{DateTime.UtcNow:yyyyMMddHHmmss}"; |
| 3 | 17 | | return id; |
| 3 | 18 | | } |
| | 19 | |
|
| | 20 | | public void AddSubscription(string subscriptionId) |
| 2017 | 21 | | { |
| 2017 | 22 | | if (string.IsNullOrWhiteSpace(subscriptionId)) |
| 3 | 23 | | throw new ArgumentException("Subscription ID cannot be null or empty", nameof(subscriptionId)); |
| | 24 | |
|
| 2014 | 25 | | _subscriptions.TryAdd(subscriptionId, DateTime.UtcNow); |
| 2014 | 26 | | } |
| | 27 | |
|
| | 28 | | public void RemoveSubscription(string subscriptionId) |
| 1007 | 29 | | { |
| 1007 | 30 | | if (string.IsNullOrWhiteSpace(subscriptionId)) |
| 3 | 31 | | return; |
| | 32 | |
|
| 1004 | 33 | | _subscriptions.TryRemove(subscriptionId, out _); |
| 1007 | 34 | | } |
| | 35 | |
|
| | 36 | | public bool HasSubscription(string subscriptionId) |
| 11 | 37 | | { |
| 11 | 38 | | if (string.IsNullOrWhiteSpace(subscriptionId)) |
| 3 | 39 | | return false; |
| | 40 | |
|
| 8 | 41 | | return _subscriptions.ContainsKey(subscriptionId); |
| 11 | 42 | | } |
| | 43 | |
|
| | 44 | | public IEnumerable<string> GetActiveSubscriptions() |
| 6 | 45 | | { |
| 6 | 46 | | return _subscriptions.Keys.ToList(); |
| 6 | 47 | | } |
| | 48 | | } |