I build a notification system which shows a bootstrap alert div in a NotificationHandler blazor component. This works fine but the notifications stay there. I'd like to make these notifications disappear after x seconds. How can I do this while not blocking UI and making sure it is executed on the blazor render thread.
See example code below:
@inject INotificationHandler Handler;
@foreach (var notification in _notifications)
{
<div class="alert alert-success">
<p>@notification</p>
</div>
}
@code
{
private List<string> _notifications = new ();
protected override void OnInitialized()
{
Handler.OnReceived += OnReceived;
}
private async Task OnReceived(string notification)
{
await InvokeAsync(() => {
_notifications.Add(notification);
StateHasChanged();
// TODO: remove notification the _notifications after x seconds
});
}
}
public interface INotificationHandler
{
public event Func<string Task> OnReceived;
}