This commit is contained in:
TheXamlGuy
2024-02-01 22:13:45 +00:00
parent 6dbe2db584
commit 4edb0a7eb5
21 changed files with 126 additions and 85 deletions
@@ -13,6 +13,7 @@ public static class IServiceCollectionExtensions
services.AddHandler<WidgetExtensionEnumerator>();
services.AddHandler<WidgetExtensionHandler>();
services.AddHandler<WidgetHostHandler>();
services.AddHandler<WidgetStartedHandler>();
return services;
}
@@ -0,0 +1,21 @@
namespace Hyperbar.Widget;
public class WidgetAvailabilityChangedHandler(IWidgetHost host) :
INotificationHandler<Changed<WidgetAvailability>>
{
public async Task Handle(Changed<WidgetAvailability> args,
CancellationToken cancellationToken)
{
if (args.Value is WidgetAvailability widgetAvailability)
{
if (widgetAvailability.Value)
{
await host.StartAsync(cancellationToken);
}
else
{
await host.StopAsync(cancellationToken);
}
}
}
}
+8 -8
View File
@@ -25,13 +25,18 @@ public class WidgetBuilder :
})
.ConfigureServices((context, services) =>
{
services.AddHostedService<WidgetService>();
services.AddSingleton<IWidgetHost, WidgetHost>();
services.AddScoped<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) =>
ActivatorUtilities.CreateInstance(provider, type, parameters!)));
services.AddScoped<IMediator, Mediator>();
services.AddScoped<IDisposer, Disposer>();
services.AddSingleton<IValue<WidgetAvailability>, Value<WidgetAvailability>>();
services.AddHandler<WidgetAvailabilityChangedHandler>();
services.AddValueChangedNotification<WidgetConfiguration,
WidgetAvailability>((config) => (args) =>
{
args.Value = config.IsEnabled;
});
});
}
@@ -40,9 +45,7 @@ public class WidgetBuilder :
public IWidgetHost Build()
{
IHost host = hostBuilder.Build();
return (IWidgetHost)ActivatorUtilities.CreateInstance(host.Services,
typeof(WidgetHost), host);
return host.Services.GetRequiredService<IWidgetHost>();
}
public IWidgetBuilder UseConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate)
@@ -56,7 +59,6 @@ public class WidgetBuilder :
}
configurationRegistered = true;
TConfiguration configuration = new();
configurationDelegate(configuration);
@@ -88,7 +90,6 @@ public class WidgetBuilder :
}
viewModelTemplateRegistered = true;
hostBuilder.ConfigureServices(services =>
{
services.AddWidgetTemplate<TViewModel>();
@@ -107,7 +108,6 @@ public class WidgetBuilder :
}
viewModelTemplateRegistered = true;
hostBuilder.ConfigureServices(services =>
{
services.AddWidgetTemplate<TWidgetContent, TWidgetTemplate>();
+1 -1
View File
@@ -11,7 +11,7 @@ public class WidgetConfiguration
internal Guid Id { get; set; } = Guid.NewGuid();
[JsonInclude]
internal bool Enabled { get; set; }
internal bool IsEnabled { get; set; }
}
public class WidgetConfiguration<TConfiguration>;
@@ -1,4 +0,0 @@
namespace Hyperbar.Widget;
public record WidgetConfigurationChanged<TConfiguration> : INotification;
+10 -5
View File
@@ -1,14 +1,19 @@
namespace Hyperbar.Widget;
public class WidgetConfigurationHandler(IValue<WidgetAvailability> widgetAvailability) :
INotificationHandler<ConfigurationChanged<WidgetConfiguration>>
public class WidgetConfigurationHandler(IEnumerable<IConfigurationValueChangedNotification<WidgetConfiguration>>
configurationValueChangedNotifications ) :
INotificationHandler<Changed<WidgetConfiguration>>
{
public async Task Handle(ConfigurationChanged<WidgetConfiguration> notification,
public async Task Handle(Changed<WidgetConfiguration> args,
CancellationToken cancellationToken)
{
if (notification.Configuration is WidgetConfiguration configuration)
if (args.Value is WidgetConfiguration configuration)
{
await widgetAvailability.SetAsync(args => args with { Value = configuration.Enabled });
foreach (IConfigurationValueChangedNotification<WidgetConfiguration> notification in
configurationValueChangedNotifications)
{
await notification.PublishAsync(configuration);
}
}
}
}
+6 -8
View File
@@ -6,15 +6,15 @@ namespace Hyperbar.Widget;
public sealed class WidgetHost :
IWidgetHost
{
private readonly IHost host;
private readonly IServiceProvider services;
private readonly IMediator mediator;
private readonly IProxyService<IMediator> proxyMediator;
public WidgetHost(IHost host,
public WidgetHost(IServiceProvider services,
IMediator mediator,
IProxyService<IMediator> proxyMediator)
{
this.host = host;
this.services = services;
this.mediator = mediator;
this.proxyMediator = proxyMediator;
@@ -24,7 +24,7 @@ public sealed class WidgetHost :
public WidgetConfiguration Configuration =>
Services.GetRequiredService<WidgetConfiguration>();
public IServiceProvider Services => host.Services;
public IServiceProvider Services => services;
public void Dispose()
{
@@ -33,8 +33,6 @@ public sealed class WidgetHost :
public async Task StartAsync(CancellationToken cancellationToken = default)
{
await host.StartAsync(cancellationToken);
if (proxyMediator.Proxy is IMediator mediator)
{
await mediator.PublishAsync(new Started<IWidgetHost>(this),
@@ -45,8 +43,8 @@ public sealed class WidgetHost :
cancellationToken);
}
public async Task StopAsync(CancellationToken cancellationToken = default)
public Task StopAsync(CancellationToken cancellationToken = default)
{
await host.StopAsync(cancellationToken);
return Task.CompletedTask;
}
}
+1 -14
View File
@@ -2,22 +2,9 @@
namespace Hyperbar.Widget;
public class WidgetHostHandler(IMediator mediator) :
public class WidgetHostHandler :
INotificationHandler<Created<IWidgetHost>>
{
//public async Task Handle(Started<IWidgetHost> notification,
// CancellationToken cancellationToken)
//{
// if (notification.Value is IWidgetHost host)
// {
// if (host.Services.GetService<IWidgetViewModel>() is IWidgetViewModel viewModel)
// {
// await mediator.PublishAsync(new Created<IWidgetViewModel>(viewModel),
// nameof(WidgetViewModel), cancellationToken);
// }
// }
//}
public async Task Handle(Created<IWidgetHost> notification,
CancellationToken cancellationToken)
{
+20
View File
@@ -0,0 +1,20 @@
using Microsoft.Extensions.DependencyInjection;
namespace Hyperbar.Widget;
public class WidgetStartedHandler(IMediator mediator) :
INotificationHandler<Started<IWidgetHost>>
{
public async Task Handle(Started<IWidgetHost> notification,
CancellationToken cancellationToken)
{
if (notification.Value is IWidgetHost host)
{
if (host.Services.GetService<IWidgetViewModel>() is IWidgetViewModel viewModel)
{
await mediator.PublishAsync(new Created<IWidgetViewModel>(viewModel),
nameof(WidgetViewModel), cancellationToken);
}
}
}
}