Skip to content

Commit 1b41260

Browse files
authored
Merge pull request #5 from KlinskiyDD/master
feat: Добавил IDisposable
2 parents 2c0759e + ba65d3e commit 1b41260

1 file changed

Lines changed: 33 additions & 9 deletions

File tree

src/SignalR.EasyUse.Client/HubConnectionExtensions.cs

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
2+
using System.Collections;
23
using System.Linq;
4+
using System.Reflection;
35
using System.Threading.Tasks;
46
using Microsoft.AspNetCore.SignalR.Client;
57
using SignalR.EasyUse.Interface;
@@ -38,9 +40,29 @@ public static T CreateNotInheritedHub<T>(this HubConnection hubConnection)
3840
/// <typeparam name="T">Type of client method</typeparam>
3941
/// <param name="connection">Connection to the hub that we subscribe to</param>
4042
/// <param name="action">The action that will be executed when you call the customer</param>
41-
public static void Subscribe<T>(this HubConnection connection, Action<T> action) where T : IClientMethod
43+
public static IDisposable Subscribe<T>(this HubConnection connection, Action<T> action) where T : IClientMethod
4244
{
43-
SubscribeNotInherited<T>(connection, action);
45+
return SubscribeNotInherited<T>(connection, action);
46+
}
47+
48+
public static void Unsubscribe<T>(this HubConnection connection)
49+
{
50+
var recieveMessage = typeof(T);
51+
var methodName = recieveMessage.Name;
52+
connection.Remove(methodName);
53+
}
54+
55+
public static void UnsubscribeAll(this HubConnection connection)
56+
{
57+
var type = connection.GetType();
58+
var fieldInfo = type.GetField("_handlers", BindingFlags.NonPublic | BindingFlags.Instance);
59+
60+
dynamic handlers = fieldInfo.GetValue(connection);
61+
62+
if (!(handlers is IDictionary dict)) return;
63+
64+
var keys = dict.Keys;
65+
foreach (string key in keys) connection.Remove(key);
4466
}
4567

4668
/// <summary>
@@ -50,20 +72,22 @@ public static void Subscribe<T>(this HubConnection connection, Action<T> action)
5072
/// <typeparam name="T">Type of client method</typeparam>
5173
/// <param name="connection">Connection to the hub that we subscribe to</param>
5274
/// <param name="action">The action that will be executed when you call the customer</param>
53-
public static void SubscribeNotInherited<T>(this HubConnection connection, Action<T> action)
75+
public static IDisposable SubscribeNotInherited<T>(this HubConnection connection, Action<T> action)
5476
{
5577
var recieveMessage = typeof(T);
5678
var methodName = recieveMessage.Name;
5779
var paramsList = recieveMessage.GetProperties()
5880
.Select((info) => info.PropertyType)
5981
.ToArray();
6082

61-
connection.On(methodName, paramsList,
62-
objects =>
63-
{
64-
var instance = objects.CreateInstance<T>();
65-
return Task.FromResult(action.DynamicInvoke(instance));
66-
});
83+
IDisposable subscription = connection.On(methodName, paramsList, (object[] args) =>
84+
{
85+
T instance = args.CreateInstance<T>();
86+
action(instance);
87+
return Task.CompletedTask;
88+
});
89+
90+
return subscription;
6791
}
6892

6993
/// <summary>

0 commit comments

Comments
 (0)