Refactored message bus handling to the base component

This commit is contained in:
Thorsten Sommer 2024-07-27 22:29:26 +02:00
parent d2b9830f57
commit 08ac5ca654
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
5 changed files with 50 additions and 130 deletions

View File

@ -7,7 +7,7 @@ namespace AIStudio.Components.Blocks;
/// <summary> /// <summary>
/// Configuration component for any boolean option. /// Configuration component for any boolean option.
/// </summary> /// </summary>
public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceiver public partial class ConfigurationOption : ConfigurationBase
{ {
/// <summary> /// <summary>
/// Text to display when the option is true. /// Text to display when the option is true.
@ -32,25 +32,6 @@ public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceive
/// </summary> /// </summary>
[Parameter] [Parameter]
public Action<bool> StateUpdate { get; set; } = _ => { }; public Action<bool> StateUpdate { get; set; } = _ => { };
/// <summary>
/// Is the option disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
private async Task OptionChanged(bool updatedState) private async Task OptionChanged(bool updatedState)
{ {
@ -58,25 +39,4 @@ public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceive
await this.SettingsManager.StoreSettings(); await this.SettingsManager.StoreSettings();
await this.InformAboutChange(); await this.InformAboutChange();
} }
#region Implementation of IMessageBusReceiver
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
} }

View File

@ -9,7 +9,7 @@ namespace AIStudio.Components.Blocks;
/// Configuration component for selecting a value from a list. /// Configuration component for selecting a value from a list.
/// </summary> /// </summary>
/// <typeparam name="T">The type of the value to select.</typeparam> /// <typeparam name="T">The type of the value to select.</typeparam>
public partial class ConfigurationSelect<T> : ConfigurationBase, IMessageBusReceiver public partial class ConfigurationSelect<T> : ConfigurationBase
{ {
/// <summary> /// <summary>
/// The data to select from. /// The data to select from.
@ -29,25 +29,6 @@ public partial class ConfigurationSelect<T> : ConfigurationBase, IMessageBusRece
[Parameter] [Parameter]
public Action<T> SelectionUpdate { get; set; } = _ => { }; public Action<T> SelectionUpdate { get; set; } = _ => { };
/// <summary>
/// Is the selection component disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
private async Task OptionChanged(T updatedValue) private async Task OptionChanged(T updatedValue)
{ {
this.SelectionUpdate(updatedValue); this.SelectionUpdate(updatedValue);
@ -56,25 +37,4 @@ public partial class ConfigurationSelect<T> : ConfigurationBase, IMessageBusRece
} }
private static string GetClass => $"{MARGIN_CLASS} rounded-lg"; private static string GetClass => $"{MARGIN_CLASS} rounded-lg";
#region Implementation of IMessageBusReceiver
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
} }

View File

@ -1,8 +1,8 @@
@typeparam T @typeparam T
@inherits ConfigurationBase @inherits ConfigurationBase
<MudField Label="@this.OptionDescription" Variant="Variant.Outlined" Class="mb-3"> <MudField Label="@this.OptionDescription" Variant="Variant.Outlined" Class="mb-3" Disabled="@this.Disabled()">
<MudSlider @ref="@this.slider" T="@T" Size="Size.Medium" Value="@this.Value()" ValueChanged="@this.OptionChanged" Min="@this.Min" Max="@this.Max" Step="@this.Step" Immediate="@true"> <MudSlider T="@T" Size="Size.Medium" Value="@this.Value()" ValueChanged="@this.OptionChanged" Min="@this.Min" Max="@this.Max" Step="@this.Step" Immediate="@true" Disabled="@this.Disabled()">
@this.Value() @this.Unit @this.Value() @this.Unit
</MudSlider> </MudSlider>
</MudField> </MudField>

View File

@ -1,12 +1,10 @@
using System.Numerics; using System.Numerics;
using AIStudio.Tools;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace AIStudio.Components.Blocks; namespace AIStudio.Components.Blocks;
public partial class ConfigurationSlider<T> : ConfigurationBase, IMessageBusReceiver where T : struct, INumber<T> public partial class ConfigurationSlider<T> : ConfigurationBase where T : struct, INumber<T>
{ {
/// <summary> /// <summary>
/// The minimum value for the slider. /// The minimum value for the slider.
@ -44,52 +42,10 @@ public partial class ConfigurationSlider<T> : ConfigurationBase, IMessageBusRece
[Parameter] [Parameter]
public Action<T> ValueUpdate { get; set; } = _ => { }; public Action<T> ValueUpdate { get; set; } = _ => { };
/// <summary>
/// Is the option disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
private MudSlider<T> slider = null!;
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
private async Task OptionChanged(T updatedValue) private async Task OptionChanged(T updatedValue)
{ {
this.ValueUpdate(updatedValue); this.ValueUpdate(updatedValue);
await this.SettingsManager.StoreSettings(); await this.SettingsManager.StoreSettings();
await this.InformAboutChange(); await this.InformAboutChange();
} }
#region Implementation of IMessageBusReceiver
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
} }

View File

@ -8,7 +8,7 @@ namespace AIStudio.Components;
/// <summary> /// <summary>
/// A base class for configuration options. /// A base class for configuration options.
/// </summary> /// </summary>
public partial class ConfigurationBase : ComponentBase public partial class ConfigurationBase : ComponentBase, IMessageBusReceiver
{ {
/// <summary> /// <summary>
/// The description of the option, i.e., the name. Should be /// The description of the option, i.e., the name. Should be
@ -23,6 +23,12 @@ public partial class ConfigurationBase : ComponentBase
[Parameter] [Parameter]
public string OptionHelp { get; set; } = string.Empty; public string OptionHelp { get; set; } = string.Empty;
/// <summary>
/// Is the option disabled?
/// </summary>
[Parameter]
public Func<bool> Disabled { get; set; } = () => false;
[Inject] [Inject]
protected SettingsManager SettingsManager { get; init; } = null!; protected SettingsManager SettingsManager { get; init; } = null!;
@ -30,6 +36,44 @@ public partial class ConfigurationBase : ComponentBase
protected MessageBus MessageBus { get; init; } = null!; protected MessageBus MessageBus { get; init; } = null!;
protected const string MARGIN_CLASS = "mb-6"; protected const string MARGIN_CLASS = "mb-6";
protected static readonly Dictionary<string, object?> SPELLCHECK_ATTRIBUTES = new();
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Configure the spellchecking for the instance name input:
this.SettingsManager.InjectSpellchecking(SPELLCHECK_ATTRIBUTES);
// Register this component with the message bus:
this.MessageBus.RegisterComponent(this);
this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]);
await base.OnInitializedAsync();
}
#endregion
protected async Task InformAboutChange() => await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED); protected async Task InformAboutChange() => await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED);
#region Implementation of IMessageBusReceiver
public Task ProcessMessage<TMsg>(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data)
{
switch (triggeredEvent)
{
case Event.CONFIGURATION_CHANGED:
this.StateHasChanged();
break;
}
return Task.CompletedTask;
}
public Task<TResult?> ProcessMessageWithResult<TPayload, TResult>(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data)
{
return Task.FromResult<TResult?>(default);
}
#endregion
} }