From ba65d3e8e0cf7ee0d81df7c1da9364f030b41ca6 Mon Sep 17 00:00:00 2001 From: Dmitriy Klinskiy Date: Wed, 25 Jun 2025 13:05:40 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20IDisposable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HubConnectionExtensions.cs | 42 +++++++++++++++---- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/SignalR.EasyUse.Client/HubConnectionExtensions.cs b/src/SignalR.EasyUse.Client/HubConnectionExtensions.cs index 1d3ccde..46e45cb 100644 --- a/src/SignalR.EasyUse.Client/HubConnectionExtensions.cs +++ b/src/SignalR.EasyUse.Client/HubConnectionExtensions.cs @@ -1,5 +1,7 @@ using System; +using System.Collections; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Microsoft.AspNetCore.SignalR.Client; using SignalR.EasyUse.Interface; @@ -38,9 +40,29 @@ public static T CreateNotInheritedHub(this HubConnection hubConnection) /// Type of client method /// Connection to the hub that we subscribe to /// The action that will be executed when you call the customer - public static void Subscribe(this HubConnection connection, Action action) where T : IClientMethod + public static IDisposable Subscribe(this HubConnection connection, Action action) where T : IClientMethod { - SubscribeNotInherited(connection, action); + return SubscribeNotInherited(connection, action); + } + + public static void Unsubscribe(this HubConnection connection) + { + var recieveMessage = typeof(T); + var methodName = recieveMessage.Name; + connection.Remove(methodName); + } + + public static void UnsubscribeAll(this HubConnection connection) + { + var type = connection.GetType(); + var fieldInfo = type.GetField("_handlers", BindingFlags.NonPublic | BindingFlags.Instance); + + dynamic handlers = fieldInfo.GetValue(connection); + + if (!(handlers is IDictionary dict)) return; + + var keys = dict.Keys; + foreach (string key in keys) connection.Remove(key); } /// @@ -50,7 +72,7 @@ public static void Subscribe(this HubConnection connection, Action action) /// Type of client method /// Connection to the hub that we subscribe to /// The action that will be executed when you call the customer - public static void SubscribeNotInherited(this HubConnection connection, Action action) + public static IDisposable SubscribeNotInherited(this HubConnection connection, Action action) { var recieveMessage = typeof(T); var methodName = recieveMessage.Name; @@ -58,12 +80,14 @@ public static void SubscribeNotInherited(this HubConnection connection, Actio .Select((info) => info.PropertyType) .ToArray(); - connection.On(methodName, paramsList, - objects => - { - var instance = objects.CreateInstance(); - return Task.FromResult(action.DynamicInvoke(instance)); - }); + IDisposable subscription = connection.On(methodName, paramsList, (object[] args) => + { + T instance = args.CreateInstance(); + action(instance); + return Task.CompletedTask; + }); + + return subscription; } ///