Skip to content
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
25 changes: 22 additions & 3 deletions src/BootstrapBlazor/Components/Toast/Toast.razor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Data from "../../modules/data.js"
import Data from "../../modules/data.js"
import EventHandler from "../../modules/event-handler.js"

export function init(id, invoke, callback) {
Expand All @@ -12,7 +12,9 @@ export function init(id, invoke, callback) {
showProgress: () => {
return toast.toast._config.autohide
},
progressElement
progressElement,
closing: false,
closed: false
}
Data.set(id, toast);

Expand All @@ -26,8 +28,24 @@ export function init(id, invoke, callback) {
progressElement.style.width = '100%'
}
})
EventHandler.on(el, 'hide.bs.toast', e => {
if (toast.closing) {
e.preventDefault();
}
else {
toast.closing = true;
queueMicrotask(() => {
if (e.defaultPrevented) {
toast.closing = false;
}
});
}
})
EventHandler.on(el, 'hidden.bs.toast', () => {
invoke.invokeMethodAsync(toast.callback)
if (!toast.closed) {
toast.closed = true;
invoke.invokeMethodAsync(toast.callback)
}
})
EventHandler.on(progressElement, 'transitionend', e => {
if (toast.toast._config.autohide === false) {
Expand Down Expand Up @@ -62,6 +80,7 @@ export function dispose(id) {

const { element, progressElement } = toast;
EventHandler.off(element, 'shown.bs.toast');
EventHandler.off(element, 'hide.bs.toast');
EventHandler.off(element, 'hidden.bs.toast');
EventHandler.off(progressElement, 'transitionend');

Expand Down
8 changes: 6 additions & 2 deletions src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,16 @@ private async Task Show(ToastOption option)
/// <param name="option"></param>
public async Task Close(ToastOption option)
{
if (!Toasts.Remove(option))
{
return;
}
Comment on lines +85 to +88

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Failed callback loses toast

When OnCloseAsync throws after the option is removed, Toasts.Remove(option) and StateHasChanged() run before OnCloseAsync; when that callback throws, the toast is already gone, and later Close calls return without retrying the callback, leaving the caller with a dismissed toast and failed close handling.

Retain a per-option in-progress guard and restore or retain the toast if the callback fails, so duplicate calls are suppressed without losing retry behavior.

Prompt for AI agents
In `src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs` at lines 85-88:

**Failed callback loses toast**

When `OnCloseAsync` throws after the option is removed, `Toasts.Remove(option)` and `StateHasChanged()` run before `OnCloseAsync`; when that callback throws, the toast is already gone, and later `Close` calls return without retrying the callback, leaving the caller with a dismissed toast and failed close handling.

Retain a per-option in-progress guard and restore or retain the toast if the callback fails, so duplicate calls are suppressed without losing retry behavior.


StateHasChanged();
if (option.OnCloseAsync != null)
{
await option.OnCloseAsync();
}
Toasts.Remove(option);
StateHasChanged();
}

/// <summary>
Expand Down
51 changes: 51 additions & 0 deletions test/UnitTest/Components/ToastTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,57 @@ public async Task OnCloseAsync_Ok()
Assert.True(close);
}

[Fact]
public async Task Close_WithoutCallback_Ok()
{
var cut = Context.Render<ToastContainer>();
var service = Context.Services.GetRequiredService<ToastService>();
var option = new ToastOption();
await service.Show(option);

Assert.Single(cut.FindComponents<Toast>());

await cut.InvokeAsync(() => cut.Instance.Close(option));

Assert.Empty(cut.FindComponents<Toast>());
}

Comment on lines +259 to +273

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · Unrequested close tests are added

The patch adds two unit tests for a bug fix without an explicit request to add tests, contrary to the repository's default testing policy.

Remove the added unit tests unless their addition is explicitly requested.

Also at test/UnitTest/Components/ToastTest.cs:274-308.

Prompt for AI agents
In `test/UnitTest/Components/ToastTest.cs` at lines 259-273:

**Unrequested close tests are added**

The patch adds two unit tests for a bug fix without an explicit request to add tests, contrary to the repository's default testing policy.

Remove the added unit tests unless their addition is explicitly requested.

Also at `test/UnitTest/Components/ToastTest.cs:274-308`.

From .github/copilot-instructions.md L10:

普通功能开发、缺陷修复等任务,默认不新增、修改或运行单元测试,除非用户明确要求

[Fact]
public async Task Close_IgnoresConcurrentCall()
{
var closeStarted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var continueClose = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
var closeCount = 0;
var cut = Context.Render<ToastContainer>();
var service = Context.Services.GetRequiredService<ToastService>();
var option = new ToastOption()
{
OnCloseAsync = async () =>
{
closeCount++;
closeStarted.TrySetResult();
await continueClose.Task;
}
};
await service.Show(option);

var firstClose = cut.InvokeAsync(() => cut.Instance.Close(option));
Task secondClose = Task.CompletedTask;
try
{
await closeStarted.Task.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
secondClose = cut.InvokeAsync(() => cut.Instance.Close(option));
Assert.Equal(1, closeCount);
Assert.Empty(cut.FindComponents<Toast>());
}
finally
{
continueClose.TrySetResult();
}

await Task.WhenAll(firstClose, secondClose);
}

[Fact]
public void StyleString_Ok()
{
Expand Down
Loading