From 9ead87d35071bd1ad0150c0744b07e84d73a80ff Mon Sep 17 00:00:00 2001 From: JKorf Date: Thu, 4 Jan 2024 21:27:59 +0100 Subject: [PATCH] wip --- .../Converters/JTokenAccessor.cs | 21 +++++++++++++++++++ .../Interfaces/IMessageAccessor.cs | 1 + CryptoExchange.Net/Sockets/Query.cs | 14 ++++++++++--- .../Sockets/SocketListenerManager.cs | 6 +++--- CryptoExchange.Net/Sockets/Subscription.cs | 2 +- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/CryptoExchange.Net/Converters/JTokenAccessor.cs b/CryptoExchange.Net/Converters/JTokenAccessor.cs index 61d20f9..182bc5b 100644 --- a/CryptoExchange.Net/Converters/JTokenAccessor.cs +++ b/CryptoExchange.Net/Converters/JTokenAccessor.cs @@ -112,8 +112,29 @@ namespace CryptoExchange.Net.Converters return item.Type == JTokenType.Array; } + public bool IsEmptyArray(IEnumerable indexes) + { + var item = _token; + foreach (var index in indexes) + { + if (item.Type != JTokenType.Array) + return false; + + var arr = ((JArray)item); + if (arr.Count <= index) + return false; + + item = arr[index]; + } + + return item.Type == JTokenType.Array && !item.HasValues; + } + private JToken? GetToken(string key) { + if (key == null) + return _token; + if (_cache.TryGetValue(key, out var token)) return token; diff --git a/CryptoExchange.Net/Interfaces/IMessageAccessor.cs b/CryptoExchange.Net/Interfaces/IMessageAccessor.cs index d994118..f9c67ae 100644 --- a/CryptoExchange.Net/Interfaces/IMessageAccessor.cs +++ b/CryptoExchange.Net/Interfaces/IMessageAccessor.cs @@ -9,6 +9,7 @@ namespace CryptoExchange.Net.Interfaces { bool IsObject(string? key); bool IsArray(IEnumerable indexes); + bool IsEmptyArray(IEnumerable indexes); string? GetStringValue(string key); int? GetIntValue(string key); public int? GetCount(string key); diff --git a/CryptoExchange.Net/Sockets/Query.cs b/CryptoExchange.Net/Sockets/Query.cs index b21fbd1..daa5b54 100644 --- a/CryptoExchange.Net/Sockets/Query.cs +++ b/CryptoExchange.Net/Sockets/Query.cs @@ -30,7 +30,7 @@ namespace CryptoExchange.Net.Sockets /// /// Strings to identify this subscription with /// - public abstract List StreamIdentifiers { get; } + public abstract List StreamIdentifiers { get; set; } /// /// The query request object @@ -47,7 +47,7 @@ namespace CryptoExchange.Net.Sockets /// public int Weight { get; } - public abstract Dictionary TypeMapping { get; } + public abstract Dictionary TypeMapping { get; set; } /// /// ctor @@ -108,10 +108,18 @@ namespace CryptoExchange.Net.Sockets /// Response object type public abstract class Query : BaseQuery { - public override Dictionary TypeMapping => new Dictionary + private Dictionary _typeMapping = new Dictionary { { "", typeof(TResponse) } }; + public override Dictionary TypeMapping + { + get => _typeMapping; + set + { + _typeMapping = value; + } + } /// /// The typed call result diff --git a/CryptoExchange.Net/Sockets/SocketListenerManager.cs b/CryptoExchange.Net/Sockets/SocketListenerManager.cs index 71c1d30..d1ffb2b 100644 --- a/CryptoExchange.Net/Sockets/SocketListenerManager.cs +++ b/CryptoExchange.Net/Sockets/SocketListenerManager.cs @@ -35,8 +35,8 @@ namespace CryptoExchange.Net.Sockets if (listeners == null) return null; - listeners.First().TypeMapping.TryGetValue(typeIdentifier ?? "", out var type); - return type; + var result = listeners.SelectMany(l => l.TypeMapping).FirstOrDefault(x => x.Key == (typeIdentifier ?? "")); + return result.Value; } } @@ -85,7 +85,7 @@ namespace CryptoExchange.Net.Sockets if (!_listeners.TryGetValue(id, out var idListeners)) return false; - listeners = idListeners.ToList(); + listeners = idListeners.Where(i => data.TypeIdentifier == null || i.TypeMapping.ContainsKey(data.TypeIdentifier)).ToList(); } foreach (var listener in listeners) diff --git a/CryptoExchange.Net/Sockets/Subscription.cs b/CryptoExchange.Net/Sockets/Subscription.cs index 3726af9..e2185c5 100644 --- a/CryptoExchange.Net/Sockets/Subscription.cs +++ b/CryptoExchange.Net/Sockets/Subscription.cs @@ -57,7 +57,7 @@ namespace CryptoExchange.Net.Sockets /// /// Strings to identify this subscription with /// - public abstract List StreamIdentifiers { get; } + public abstract List StreamIdentifiers { get; set; } /// /// Cancellation token registration