diff --git a/blazorbootstrap/Components/Toasts/Toasts.razor.cs b/blazorbootstrap/Components/Toasts/Toasts.razor.cs index ae41e1328..d9756aa97 100644 --- a/blazorbootstrap/Components/Toasts/Toasts.razor.cs +++ b/blazorbootstrap/Components/Toasts/Toasts.razor.cs @@ -12,7 +12,7 @@ protected override async ValueTask DisposeAsyncCore(bool disposing) Messages = null; if (ToastService is not null) - ToastService.OnNotify -= OnNotify; + ToastService.OnNotify -= OnNotifyAsync; } await base.DisposeAsyncCore(disposing); @@ -21,12 +21,12 @@ protected override async ValueTask DisposeAsyncCore(bool disposing) protected override void OnInitialized() { if (ToastService is not null) - ToastService.OnNotify += OnNotify; + ToastService.OnNotify += OnNotifyAsync; base.OnInitialized(); } - private void OnNotify(ToastMessage toastMessage) + private async Task OnNotifyAsync(ToastMessage toastMessage) { if (toastMessage is null) return; @@ -35,7 +35,7 @@ private void OnNotify(ToastMessage toastMessage) Messages.Add(toastMessage); - StateHasChanged(); + await InvokeAsync(StateHasChanged); } private void OnToastHiddenAsync(ToastEventArgs args) diff --git a/blazorbootstrap/Services/ToastService.cs b/blazorbootstrap/Services/ToastService.cs index 4e18c94eb..8a408a705 100644 --- a/blazorbootstrap/Services/ToastService.cs +++ b/blazorbootstrap/Services/ToastService.cs @@ -4,13 +4,24 @@ public class ToastService { #region Events - internal event Action OnNotify = default!; - + internal event Func OnNotify = default!; + #endregion #region Methods - public void Notify(ToastMessage toastMessage) => OnNotify?.Invoke(toastMessage); + public async Task NotifyAsync(ToastMessage toastMessage) + { + Delegate[] subscribers = OnNotify?.GetInvocationList() ?? Array.Empty(); + if (subscribers.Length > 0) + { + await Task.WhenAll(subscribers.Select(d => + { + Func subscriber = (Func)d; + return subscriber.Invoke(toastMessage); + })); + } + } #endregion }