Skip to content

Commit eaaa63e

Browse files
committed
feat: Добавил IDisposable
1 parent 2c0759e commit eaaa63e

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

src/SignalR.EasyUse.Client/HubConnectionExtensions.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Microsoft.AspNetCore.SignalR.Client;
@@ -38,9 +39,9 @@ public static T CreateNotInheritedHub<T>(this HubConnection hubConnection)
3839
/// <typeparam name="T">Type of client method</typeparam>
3940
/// <param name="connection">Connection to the hub that we subscribe to</param>
4041
/// <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
42+
public static IDisposable Subscribe<T>(this HubConnection connection, Action<T> action) where T : IClientMethod
4243
{
43-
SubscribeNotInherited<T>(connection, action);
44+
return SubscribeNotInherited<T>(connection, action);
4445
}
4546

4647
/// <summary>
@@ -50,20 +51,22 @@ public static void Subscribe<T>(this HubConnection connection, Action<T> action)
5051
/// <typeparam name="T">Type of client method</typeparam>
5152
/// <param name="connection">Connection to the hub that we subscribe to</param>
5253
/// <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)
54+
public static IDisposable SubscribeNotInherited<T>(this HubConnection connection, Action<T> action)
5455
{
5556
var recieveMessage = typeof(T);
5657
var methodName = recieveMessage.Name;
5758
var paramsList = recieveMessage.GetProperties()
5859
.Select((info) => info.PropertyType)
5960
.ToArray();
6061

61-
connection.On(methodName, paramsList,
62-
objects =>
63-
{
64-
var instance = objects.CreateInstance<T>();
65-
return Task.FromResult(action.DynamicInvoke(instance));
66-
});
62+
IDisposable subscription = connection.On(methodName, paramsList, (object[] args) =>
63+
{
64+
T instance = args.CreateInstance<T>();
65+
action(instance);
66+
return Task.CompletedTask;
67+
});
68+
69+
return subscription;
6770
}
6871

6972
/// <summary>

0 commit comments

Comments
 (0)