Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SweetAlert): eliminating latency between call show method twice #3032

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>8.3.1-beta04</Version>
<Version>8.3.1-beta05</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
7 changes: 4 additions & 3 deletions src/BootstrapBlazor/Components/SweetAlert/SwalOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public class SwalOption : PopupOptionBase
internal Modal? Modal { get; set; }

/// <summary>
/// 获得/设置 模态弹窗返回值任务实例
/// 获得/设置 模态弹窗任务上下文
/// </summary>
internal TaskCompletionSource<bool> ReturnTask { get; } = new TaskCompletionSource<bool>();
[NotNull]
internal SweetContext? ConfirmContext { get; set; }

/// <summary>
/// 获得/设置 是否为确认弹窗模式 此属性给模态弹窗时使用 默认为 false
Expand Down Expand Up @@ -148,7 +149,7 @@ public async Task CloseAsync(bool returnValue = true)

if (IsConfirm)
{
ReturnTask.TrySetResult(returnValue);
ConfirmContext.Value = returnValue;
}
}
}
11 changes: 11 additions & 0 deletions src/BootstrapBlazor/Components/SweetAlert/SweetAlert.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public partial class SweetAlert : IAsyncDisposable
[NotNull]
private Func<Task>? OnCloseAsync { get; set; }

private SweetContext _context = default!;

/// <summary>
/// <inheritdoc/>
/// </summary>
Expand All @@ -55,6 +57,10 @@ protected override void OnInitialized()
{
DelayToken.Cancel();
}
if (_context != null)
{
_context.ConfirmTask.TrySetResult(_context.Value);
}
StateHasChanged();
return Task.CompletedTask;
};
Expand Down Expand Up @@ -104,6 +110,11 @@ private Task Show(SwalOption option)
Delay = option.Delay;

option.Modal = ModalContainer;
if (option.IsConfirm)
{
_context = new() { ConfirmTask = new() };
option.ConfirmContext = _context;
}
var parameters = option.ToAttributes();
parameters.Add(nameof(ModalDialog.BodyTemplate), BootstrapDynamicComponent.CreateComponent<SweetAlertBody>(option.Parse()).Render());

Expand Down
23 changes: 23 additions & 0 deletions src/BootstrapBlazor/Components/SweetAlert/SweetContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

namespace BootstrapBlazor.Components;

internal class SweetContext
{
/// <summary>
/// 获得/设置 弹窗返回值
/// </summary>
public bool Value { get; set; }

/// <summary>
/// 获得/设置 弹窗任务上下文
/// </summary>
[NotNull]
#if NET7_0_OR_GREATER
public required TaskCompletionSource<bool>? ConfirmTask { get; init; }
#else
public TaskCompletionSource<bool>? ConfirmTask { get; set; }
#endif
}
6 changes: 3 additions & 3 deletions src/BootstrapBlazor/Extensions/SwalExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static async Task<bool> ShowModal(this SwalService service, SwalOption op
{
option.IsConfirm = true;
await service.Show(option, swal);
return await option.ReturnTask.Task;
return await option.ConfirmContext.ConfirmTask.Task;
}

/// <summary>
Expand All @@ -38,7 +38,7 @@ public static async Task<bool> ShowModal(this SwalService service, SwalOption op
{
if (option.IsConfirm)
{
option.ReturnTask.TrySetResult(false);
option.ConfirmContext.Value = false;
}
if (option.OnCloseAsync != null)
{
Expand All @@ -49,7 +49,7 @@ public static async Task<bool> ShowModal(this SwalService service, SwalOption op
{
if (option.IsConfirm)
{
option.ReturnTask.TrySetResult(true);
option.ConfirmContext.Value = true;
}
if (option.OnConfirmAsync != null)
{
Expand Down