using Microsoft.AspNetCore.Components;
namespace Anubis.Server.Components;
///
/// A component that reads its data before drawing anything. With static rendering, a plain OnInitializedAsync
/// that really waits (PostgreSQL does; SQLite answers at once) lets Blazor draw the component once with its fields
/// still empty. Here the data are loaded first, then the component renders once.
///
public abstract class LoadedComponent : ComponentBase
{
private bool _loaded;
public override async Task SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);
if (!_loaded)
{
_loaded = true;
await LoadAsync().ConfigureAwait(true);
}
await base.SetParametersAsync(ParameterView.Empty).ConfigureAwait(true);
}
/// Reads what the component shows. Parameters, cascading values and query values are already set.
protected abstract Task LoadAsync();
}
/// The same for the layout.
public abstract class LoadedLayout : LayoutComponentBase
{
private bool _loaded;
public override async Task SetParametersAsync(ParameterView parameters)
{
parameters.SetParameterProperties(this);
if (!_loaded)
{
_loaded = true;
await LoadAsync().ConfigureAwait(true);
}
await base.SetParametersAsync(ParameterView.Empty).ConfigureAwait(true);
}
///
protected abstract Task LoadAsync();
}