From e32bf9e26701a9615399a59113dc20d8ca97c253 Mon Sep 17 00:00:00 2001 From: Suiram1701 <110390261+Suiram1701@users.noreply.github.com> Date: Wed, 14 Aug 2024 13:27:03 +0200 Subject: [PATCH 1/3] -Fix: Invoked StateHasChanged with InvokeAsync when adding a Toast via ToastService to avoid exceptions ( #836 ). --- blazorbootstrap/Components/Toasts/Toasts.razor.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/blazorbootstrap/Components/Toasts/Toasts.razor.cs b/blazorbootstrap/Components/Toasts/Toasts.razor.cs index ae41e1328..5fb49ae87 100644 --- a/blazorbootstrap/Components/Toasts/Toasts.razor.cs +++ b/blazorbootstrap/Components/Toasts/Toasts.razor.cs @@ -26,7 +26,7 @@ protected override void OnInitialized() base.OnInitialized(); } - private void OnNotify(ToastMessage toastMessage) + private async void OnNotify(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) From f5de96a4dde406b7318ed8eb4ec5f9dedc511aaf Mon Sep 17 00:00:00 2001 From: Suiram1701 <110390261+Suiram1701@users.noreply.github.com> Date: Sat, 17 May 2025 17:34:19 +0200 Subject: [PATCH 2/3] Changed async void to async Task --- blazorbootstrap/Components/Toasts/Toasts.razor.cs | 2 +- blazorbootstrap/Services/ToastService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/blazorbootstrap/Components/Toasts/Toasts.razor.cs b/blazorbootstrap/Components/Toasts/Toasts.razor.cs index 5fb49ae87..9e7d64bb0 100644 --- a/blazorbootstrap/Components/Toasts/Toasts.razor.cs +++ b/blazorbootstrap/Components/Toasts/Toasts.razor.cs @@ -26,7 +26,7 @@ protected override void OnInitialized() base.OnInitialized(); } - private async void OnNotify(ToastMessage toastMessage) + private async Task OnNotify(ToastMessage toastMessage) { if (toastMessage is null) return; diff --git a/blazorbootstrap/Services/ToastService.cs b/blazorbootstrap/Services/ToastService.cs index 4e18c94eb..9be9c17a5 100644 --- a/blazorbootstrap/Services/ToastService.cs +++ b/blazorbootstrap/Services/ToastService.cs @@ -4,7 +4,7 @@ public class ToastService { #region Events - internal event Action OnNotify = default!; + internal event Func OnNotify = default!; #endregion From 1587726e4850063d05bfd2650a885ce0a3979f6b Mon Sep 17 00:00:00 2001 From: Suiram1701 <110390261+Suiram1701@users.noreply.github.com> Date: Mon, 19 May 2025 14:01:50 +0200 Subject: [PATCH 3/3] Added suggested NotifyAsync method --- blazorbootstrap/Components/Toasts/Toasts.razor.cs | 6 +++--- blazorbootstrap/Services/ToastService.cs | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/blazorbootstrap/Components/Toasts/Toasts.razor.cs b/blazorbootstrap/Components/Toasts/Toasts.razor.cs index 9e7d64bb0..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 async Task OnNotify(ToastMessage toastMessage) + private async Task OnNotifyAsync(ToastMessage toastMessage) { if (toastMessage is null) return; diff --git a/blazorbootstrap/Services/ToastService.cs b/blazorbootstrap/Services/ToastService.cs index 9be9c17a5..8a408a705 100644 --- a/blazorbootstrap/Services/ToastService.cs +++ b/blazorbootstrap/Services/ToastService.cs @@ -5,12 +5,23 @@ public class ToastService #region Events 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 }