/
WolfClientBuilderUtilitiesExtensions.cs
43 lines (38 loc) · 1.98 KB
/
WolfClientBuilderUtilitiesExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using Microsoft.Extensions.DependencyInjection;
using TehGM.Wolfringo.Utilities;
using TehGM.Wolfringo.Utilities.Internal;
namespace TehGM.Wolfringo
{
/// <summary>Extension methods for <see cref="WolfClientBuilder"/> that enable easy adding of utilities.</summary>
public static class WolfClientBuilderUtilitiesExtensions
{
/// <summary>Adds <see cref="WolfClientReconnector"/> together with <see cref="WolfClient"/>, and allows configuration.</summary>
/// <param name="clientBuilder">The WOLF client builder.</param>
/// <param name="configure">Delegate that can be used to configure commands.</param>
/// <returns>Current WOLF Client builder instance.</returns>
public static WolfClientBuilder WithAutoReconnection(this WolfClientBuilder clientBuilder, Action<ReconnectorConfig> configure)
{
// get config
ReconnectorConfig config = new ReconnectorConfig();
configure?.Invoke(config);
Action<WolfClient, IServiceProvider> onBuilt = null;
onBuilt = (client, services) =>
{
// use handler class to resolve, to enable automatic disposal
DisposableServicesHandler handler = services.GetService<DisposableServicesHandler>();
// resolve to start
WolfClientReconnector result = handler?.GetRequiredService<WolfClientReconnector>(services)
?? services.GetRequiredService<WolfClientReconnector>();
};
clientBuilder.Built += onBuilt;
// add to builder - use a factory delegate so it's automatically disposed
return clientBuilder.WithService<WolfClientReconnector>(provider =>
{
if (config.Log == null)
config.Log = provider.GetLoggerFor<WolfClientReconnector>();
return new WolfClientReconnector(provider.GetRequiredService<IWolfClient>(), config);
});
}
}
}