// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR.Client
{
///
/// Extension methods for .
///
public static partial class HubConnectionExtensions
{
private static IDisposable On(this HubConnection hubConnetion, string methodName, Type[] parameterTypes, Action handler)
{
return hubConnetion.On(methodName, parameterTypes, (parameters, state) =>
{
var currentHandler = (Action)state;
currentHandler(parameters);
return Task.CompletedTask;
}, handler);
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName, Type.EmptyTypes, args => handler());
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1) },
args => handler((T1)args[0]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2) },
args => handler((T1)args[0], (T2)args[1]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The third argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2), typeof(T3) },
args => handler((T1)args[0], (T2)args[1], (T3)args[2]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The third argument type.
/// The fourth argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4) },
args => handler((T1)args[0], (T2)args[1], (T3)args[2], (T4)args[3]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The third argument type.
/// The fourth argument type.
/// The fifth argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5) },
args => handler((T1)args[0], (T2)args[1], (T3)args[2], (T4)args[3], (T5)args[4]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The third argument type.
/// The fourth argument type.
/// The fifth argument type.
/// The sixth argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6) },
args => handler((T1)args[0], (T2)args[1], (T3)args[2], (T4)args[3], (T5)args[4], (T6)args[5]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The third argument type.
/// The fourth argument type.
/// The fifth argument type.
/// The sixth argument type.
/// The seventh argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7) },
args => handler((T1)args[0], (T2)args[1], (T3)args[2], (T4)args[3], (T5)args[4], (T6)args[5], (T7)args[6]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The first argument type.
/// The second argument type.
/// The third argument type.
/// The fourth argument type.
/// The fifth argument type.
/// The sixth argument type.
/// The seventh argument type.
/// The eighth argument type.
/// The hub connection.
/// The name of the hub method to define.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Action handler)
{
if (hubConnection == null)
{
throw new ArgumentNullException(nameof(hubConnection));
}
return hubConnection.On(methodName,
new[] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6), typeof(T7), typeof(T8) },
args => handler((T1)args[0], (T2)args[1], (T3)args[2], (T4)args[3], (T5)args[4], (T6)args[5], (T7)args[6], (T8)args[7]));
}
///
/// Registers a handler that will be invoked when the hub method with the specified method name is invoked.
///
/// The hub connection.
/// The name of the hub method to define.
/// The parameters types expected by the hub method.
/// The handler that will be raised when the hub method is invoked.
/// A subscription that can be disposed to unsubscribe from the hub method.
public static IDisposable On(this HubConnection hubConnection, string methodName, Type[] parameterTypes, Func handler)
{
return hubConnection.On(methodName, parameterTypes, (parameters, state) =>
{
var currentHandler = (Func)state;
return currentHandler(parameters);
}, handler);
}
}
}