diff --git a/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs b/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs index 443a26ffe0c..3cf47206669 100644 --- a/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs +++ b/src/BootstrapBlazor/Components/SweetAlert/SweetAlertBody.razor.cs @@ -174,29 +174,41 @@ protected override void OnParametersSet() ConfirmButtonIcon ??= IconTheme.GetIconByKey(ComponentIcons.SweetAlertConfirmIcon); } - private async Task OnClickClose() - { - if (OnCloseAsync != null) - { - await OnCloseAsync(); - } + private bool _isClosing; - if (CloseModal != null) - { - await CloseModal(); - } - } + private bool _actionCompleted; + + private Task OnClickClose() => CloseAsync(OnCloseAsync); + + private Task OnClickConfirm() => CloseAsync(OnConfirmAsync); - private async Task OnClickConfirm() + private async Task CloseAsync(Func? callback) { - if (OnConfirmAsync != null) + if (_isClosing) { - await OnConfirmAsync(); + return; } - if (CloseModal != null) + _isClosing = true; + try + { + if (!_actionCompleted) + { + if (callback != null) + { + await callback(); + } + _actionCompleted = true; + } + + if (CloseModal != null) + { + await CloseModal(); + } + } + finally { - await CloseModal(); + _isClosing = false; } } } diff --git a/src/BootstrapBlazor/Extensions/SwalExtensions.cs b/src/BootstrapBlazor/Extensions/SwalExtensions.cs index b68b9f4634b..3e82e99fe91 100644 --- a/src/BootstrapBlazor/Extensions/SwalExtensions.cs +++ b/src/BootstrapBlazor/Extensions/SwalExtensions.cs @@ -14,15 +14,29 @@ public static class SwalExtensions /// /// 异步回调方法 /// 异步callback method + /// 已有弹窗显示时忽略新请求并返回 false。 + /// Ignores a new request and returns false while a popup is already displayed. /// /// /// /// 指定弹窗组件 默认为 null 使用 组件内置弹窗组件指定弹窗component default is为 null 使用 component内置弹窗component public static async Task ShowModal(this SwalService service, SwalOption option, SweetAlert? swal = null) { + var isConfirm = option.IsConfirm; option.IsConfirm = true; - await service.Show(option, swal); - return await option.ConfirmContext.ConfirmTask.Task; + var previousContext = option.ConfirmContext; + // An accepted request assigns its context before awaiting rendering. + var showTask = service.Show(option, swal); + var context = option.ConfirmContext; + await showTask; + + if (context == null || context == previousContext) + { + option.IsConfirm = isConfirm; + return false; + } + + return await context.ConfirmTask.Task; } /// diff --git a/test/UnitTest/Components/SwalTest.cs b/test/UnitTest/Components/SwalTest.cs index 97b7975ea39..13204349eec 100644 --- a/test/UnitTest/Components/SwalTest.cs +++ b/test/UnitTest/Components/SwalTest.cs @@ -7,6 +7,115 @@ namespace UnitTest.Components; public class SwalTest : BootstrapBlazorTestBase { + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task ShowModal_ReturnsConfirmationResult_Ok(bool expected) + { + var cut = Context.Render(pb => + { + pb.AddChildContent(); + }); + var swal = cut.FindComponent().Instance.SwalService; + var option = new SwalOption(); + var modalTask = Task.FromResult(false); + + await cut.InvokeAsync(() => + { + modalTask = swal.ShowModal(option); + }); + + var modal = cut.FindComponents().Last(); + await cut.InvokeAsync(() => cut.Find(expected ? ".btn-danger" : ".btn-secondary").Click()); + await cut.InvokeAsync(() => modal.Instance.CloseCallback()); + + Assert.Equal(expected, await modalTask); + Assert.True(option.IsConfirm); + } + + [Fact] + public async Task ShowModal_IgnoresRequestsWhileBusy_Ok() + { + var cut = Context.Render(pb => + { + pb.AddChildContent(); + }); + var swal = cut.FindComponent().Instance.SwalService; + var option = new SwalOption() + { + Content = "Active Swal" + }; + var modalTask = Task.FromResult(false); + + await cut.InvokeAsync(() => + { + modalTask = swal.ShowModal(option); + }); + + var newOption = new SwalOption() + { + Content = "Ignored Swal" + }; + var newRequestTask = Task.FromResult(true); + await cut.InvokeAsync(() => + { + newRequestTask = swal.ShowModal(newOption); + }); + + Assert.False(await newRequestTask); + Assert.False(newOption.IsConfirm); + + var repeatedRequestTask = Task.FromResult(true); + await cut.InvokeAsync(() => + { + repeatedRequestTask = swal.ShowModal(option); + }); + + Assert.False(await repeatedRequestTask); + Assert.True(option.IsConfirm); + Assert.Contains("Active Swal", cut.Markup); + Assert.DoesNotContain("Ignored Swal", cut.Markup); + + var modal = cut.FindComponents().Last(); + await cut.InvokeAsync(() => cut.Find(".btn-danger").Click()); + await cut.InvokeAsync(() => modal.Instance.CloseCallback()); + + Assert.True(await modalTask); + } + + [Fact] + public async Task CloseAsync_IgnoresConcurrentCall_Ok() + { + var callbackStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var continueCallback = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var callbackCount = 0; + var cut = Context.Render(pb => + { + pb.Add(a => a.IsConfirm, true); + pb.Add(a => a.OnConfirmAsync, async () => + { + callbackCount++; + callbackStarted.TrySetResult(); + await continueCallback.Task; + }); + }); + + var firstClick = cut.InvokeAsync(() => cut.Find(".btn-danger").Click()); + try + { + await callbackStarted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken); + await cut.InvokeAsync(() => cut.Find(".btn-danger").Click()); + + Assert.Equal(1, callbackCount); + } + finally + { + continueCallback.TrySetResult(); + } + + await firstClick; + } + [Fact] public void Show_Ok() {