Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public Task StartAsync(CancellationToken cancellationToken)
{
var client = services.GetRequiredService<GatewayClient>();

foreach (var handler in services.GetServices<IGatewayHandler>())
foreach (var handlerMetadata in services.GetServices<GatewayHandlerMetadata>())
{
if (handler is IDelegateGatewayHandlerBase delegateHandler)
RegisterDelegateHandler(client, delegateHandler);
if (handlerMetadata is ClassGatewayHandlerMetadata classHandlerMetadata)
RegisterClassHandler(services, client, classHandlerMetadata);
else
RegisterClassHandler(client, handler);
RegisterDelegateHandler(services, client, (DelegateGatewayHandlerMetadata)handlerMetadata);
}

var options = services.GetRequiredService<IOptions<GatewayClientOptions>>().Value;
Expand Down
12 changes: 6 additions & 6 deletions Hosting/NetCord.Hosting/Gateway/GatewayEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ namespace NetCord.Hosting.Gateway;

public partial class GatewayEvent
{
internal GatewayEvent(string name)
internal GatewayEvent(GatewayEventId id)
{
Name = name;
Id = id;
}

internal string Name { get; }
internal GatewayEventId Id { get; }
}

public class GatewayEvent<T>
{
internal GatewayEvent(string name)
internal GatewayEvent(GatewayEventId id)
{
Name = name;
Id = id;
}

internal string Name { get; }
internal GatewayEventId Id { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Reflection;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

using NetCord.Gateway;

namespace NetCord.Hosting.Gateway;

Expand All @@ -12,10 +15,13 @@ public static class GatewayHandlerServiceCollectionExtensions
/// </summary>
/// <typeparam name="T">The type of the <see cref="IGatewayHandler"/> to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IGatewayHandler"/> to.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddGatewayHandler<[DAM(DAMT.PublicConstructors)] T>(this IServiceCollection services) where T : class, IGatewayHandler
public static IServiceCollection AddGatewayHandler<[DAM(DAMT.PublicConstructors)] T>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : class, IGatewayHandler
{
services.AddSingleton<IGatewayHandler, T>();
services.TryAdd(ServiceDescriptor.Describe(typeof(T), typeof(T), lifetime));
services.AddSingleton<GatewayHandlerMetadata>(new ClassGatewayHandlerMetadata(typeof(T), lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -25,10 +31,13 @@ public static class GatewayHandlerServiceCollectionExtensions
/// <typeparam name="T">The type of the <see cref="IGatewayHandler"/> to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IGatewayHandler"/> to.</param>
/// <param name="implementationFactory">The factory that creates the <see cref="IGatewayHandler"/>.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddGatewayHandler<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory) where T : class, IGatewayHandler
public static IServiceCollection AddGatewayHandler<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : class, IGatewayHandler
{
services.AddSingleton<IGatewayHandler, T>(implementationFactory);
services.TryAdd(ServiceDescriptor.Describe(typeof(T), implementationFactory, lifetime));
services.AddSingleton<GatewayHandlerMetadata>(new ClassGatewayHandlerMetadata(typeof(T), lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -37,10 +46,13 @@ public static IServiceCollection AddGatewayHandler<T>(this IServiceCollection se
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IGatewayHandler"/> to.</param>
/// <param name="handlerType">The type of the <see cref="IGatewayHandler"/> to add.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddGatewayHandler(this IServiceCollection services, [DAM(DAMT.PublicConstructors)] Type handlerType)
public static IServiceCollection AddGatewayHandler(this IServiceCollection services, [DAM(DAMT.PublicConstructors)] Type handlerType, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddSingleton(typeof(IGatewayHandler), handlerType);
services.TryAdd(ServiceDescriptor.Describe(handlerType, handlerType, lifetime));
services.AddSingleton<GatewayHandlerMetadata>(new ClassGatewayHandlerMetadata(handlerType, lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -50,10 +62,15 @@ public static IServiceCollection AddGatewayHandler(this IServiceCollection servi
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IGatewayHandler"/> to.</param>
/// <param name="gatewayEvent">The gateway event.</param>
/// <param name="handler">The delegate that represents the handler.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddGatewayHandler(this IServiceCollection services, GatewayEvent gatewayEvent, Delegate handler)
public static IServiceCollection AddGatewayHandler(this IServiceCollection services, GatewayEvent gatewayEvent, Delegate handler, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddSingleton<IGatewayHandler>(services => new DelegateGatewayHandler(gatewayEvent.Name, services, handler));
services.AddSingleton<GatewayHandlerMetadata>(new DelegateGatewayHandlerMetadata(
DelegateHandlerHelper.CreateHandler<Func<IServiceProvider, ValueTask>>(handler, []),
gatewayEvent.Id,
lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -64,10 +81,15 @@ public static IServiceCollection AddGatewayHandler(this IServiceCollection servi
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IGatewayHandler"/> to.</param>
/// <param name="gatewayEvent">The gateway event.</param>
/// <param name="handler">The delegate that represents the handler.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddGatewayHandler<T>(this IServiceCollection services, GatewayEvent<T> gatewayEvent, Delegate handler)
public static IServiceCollection AddGatewayHandler<T>(this IServiceCollection services, GatewayEvent<T> gatewayEvent, Delegate handler, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddSingleton<IGatewayHandler>(services => new DelegateGatewayHandler<T>(gatewayEvent.Name, services, handler));
services.AddSingleton<GatewayHandlerMetadata>(new DelegateGatewayHandlerMetadata(
DelegateHandlerHelper.CreateHandler<Func<T, IServiceProvider, ValueTask>>(handler, [typeof(T)]),
gatewayEvent.Id,
lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -76,11 +98,14 @@ public static IServiceCollection AddGatewayHandler<T>(this IServiceCollection se
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IGatewayHandler"/> implementations to.</param>
/// <param name="assembly">The assembly to scan for <see cref="IGatewayHandler"/> implementations.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IGatewayHandler"/> implementations.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[RequiresUnreferencedCode("Types might be removed")]
public static IServiceCollection AddGatewayHandlers(this IServiceCollection services, Assembly assembly)
public static IServiceCollection AddGatewayHandlers(this IServiceCollection services, Assembly assembly, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
AddGatewayHandlers(services, typeof(IGatewayHandler), assembly);
foreach (var type in HandlerHelpers.GetHandlers(typeof(IGatewayHandler), assembly))
AddGatewayHandler(services, type, lifetime);

return services;
}

Expand All @@ -89,10 +114,13 @@ public static IServiceCollection AddGatewayHandlers(this IServiceCollection serv
/// </summary>
/// <typeparam name="T">The type of the <see cref="IShardedGatewayHandler"/> to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IShardedGatewayHandler"/> to.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IShardedGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddShardedGatewayHandler<[DAM(DAMT.PublicConstructors)] T>(this IServiceCollection services) where T : class, IShardedGatewayHandler
public static IServiceCollection AddShardedGatewayHandler<[DAM(DAMT.PublicConstructors)] T>(this IServiceCollection services, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : class, IShardedGatewayHandler
{
services.AddSingleton<IShardedGatewayHandler, T>();
services.TryAdd(ServiceDescriptor.Describe(typeof(T), typeof(T), lifetime));
services.AddSingleton<ShardedGatewayHandlerMetadata>(new ClassShardedGatewayHandlerMetadata(typeof(T), lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -102,10 +130,13 @@ public static IServiceCollection AddGatewayHandlers(this IServiceCollection serv
/// <typeparam name="T">The type of the <see cref="IShardedGatewayHandler"/> to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IShardedGatewayHandler"/> to.</param>
/// <param name="implementationFactory">The factory that creates the <see cref="IShardedGatewayHandler"/>.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IShardedGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddShardedGatewayHandler<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory) where T : class, IShardedGatewayHandler
public static IServiceCollection AddShardedGatewayHandler<T>(this IServiceCollection services, Func<IServiceProvider, T> implementationFactory, ServiceLifetime lifetime = ServiceLifetime.Singleton) where T : class, IShardedGatewayHandler
{
services.AddSingleton<IShardedGatewayHandler, T>(implementationFactory);
services.TryAdd(ServiceDescriptor.Describe(typeof(T), implementationFactory, lifetime));
services.AddSingleton<ShardedGatewayHandlerMetadata>(new ClassShardedGatewayHandlerMetadata(typeof(T), lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -114,10 +145,13 @@ public static IServiceCollection AddShardedGatewayHandler<T>(this IServiceCollec
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IShardedGatewayHandler"/> to.</param>
/// <param name="handlerType">The type of the <see cref="IShardedGatewayHandler"/> to add.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IShardedGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddShardedGatewayHandler(this IServiceCollection services, [DAM(DAMT.PublicConstructors)] Type handlerType)
public static IServiceCollection AddShardedGatewayHandler(this IServiceCollection services, [DAM(DAMT.PublicConstructors)] Type handlerType, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddSingleton(typeof(IShardedGatewayHandler), handlerType);
services.TryAdd(ServiceDescriptor.Describe(handlerType, handlerType, lifetime));
services.AddSingleton<GatewayHandlerMetadata>(new ClassGatewayHandlerMetadata(handlerType, lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -127,10 +161,15 @@ public static IServiceCollection AddShardedGatewayHandler(this IServiceCollectio
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IShardedGatewayHandler"/> to.</param>
/// <param name="gatewayEvent">The gateway event.</param>
/// <param name="handler">The delegate that represents the handler.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IShardedGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddShardedGatewayHandler(this IServiceCollection services, GatewayEvent gatewayEvent, Delegate handler)
public static IServiceCollection AddShardedGatewayHandler(this IServiceCollection services, GatewayEvent gatewayEvent, Delegate handler, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddSingleton<IShardedGatewayHandler>(services => new DelegateShardedGatewayHandler(gatewayEvent.Name, services, handler));
services.AddSingleton<ShardedGatewayHandlerMetadata>(new DelegateShardedGatewayHandlerMetadata(
DelegateHandlerHelper.CreateHandler<Func<GatewayClient, IServiceProvider, ValueTask>>(handler, [typeof(GatewayClient)]),
gatewayEvent.Id,
lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -141,10 +180,15 @@ public static IServiceCollection AddShardedGatewayHandler(this IServiceCollectio
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IShardedGatewayHandler"/> to.</param>
/// <param name="gatewayEvent">The gateway event.</param>
/// <param name="handler">The delegate that represents the handler.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IShardedGatewayHandler"/>.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddShardedGatewayHandler<T>(this IServiceCollection services, GatewayEvent<T> gatewayEvent, Delegate handler)
public static IServiceCollection AddShardedGatewayHandler<T>(this IServiceCollection services, GatewayEvent<T> gatewayEvent, Delegate handler, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
services.AddSingleton<IShardedGatewayHandler>(services => new DelegateShardedGatewayHandler<T>(gatewayEvent.Name, services, handler));
services.AddSingleton<ShardedGatewayHandlerMetadata>(new DelegateShardedGatewayHandlerMetadata(
DelegateHandlerHelper.CreateHandler<Func<GatewayClient, T, IServiceProvider, ValueTask>>(handler, [typeof(GatewayClient), typeof(T)]),
gatewayEvent.Id,
lifetime is ServiceLifetime.Singleton));

return services;
}

Expand All @@ -153,18 +197,14 @@ public static IServiceCollection AddShardedGatewayHandler<T>(this IServiceCollec
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the <see cref="IShardedGatewayHandler"/> implementations to.</param>
/// <param name="assembly">The assembly to scan for <see cref="IShardedGatewayHandler"/> implementations.</param>
/// <param name="lifetime">The <see cref="ServiceLifetime"/> of the <see cref="IShardedGatewayHandler"/> implementations.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
[RequiresUnreferencedCode("Types might be removed")]
public static IServiceCollection AddShardedGatewayHandlers(this IServiceCollection services, Assembly assembly)
public static IServiceCollection AddShardedGatewayHandlers(this IServiceCollection services, Assembly assembly, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
AddGatewayHandlers(services, typeof(IShardedGatewayHandler), assembly);
return services;
}
foreach (var type in HandlerHelpers.GetHandlers(typeof(IShardedGatewayHandler), assembly))
AddShardedGatewayHandler(services, type, lifetime);

[RequiresUnreferencedCode("Types might be removed")]
private static void AddGatewayHandlers(IServiceCollection services, Type handlerBase, Assembly assembly)
{
foreach (var type in HandlerHelpers.GetHandlers(handlerBase, assembly))
services.AddSingleton(handlerBase, type);
return services;
}
}
Loading