Skip to content

Commit

Permalink
Merge pull request #25 from dou867/jstimeout
Browse files Browse the repository at this point in the history
WI00709656 GetBoundingClientRect don't throw on time out
  • Loading branch information
dou867 authored Feb 25, 2024
2 parents 97ae0b1 + d762ab7 commit ce49a3b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ public static class JSRuntimeExtensions
{
public static async Task<Rectangle?> GetBoundingClientRect(this IJSRuntime jsRuntime, ElementReference element)
{
return await jsRuntime.InvokeAsync<Rectangle>("ZBlazorDiagrams.getBoundingClientRect", element);
try
{
return await jsRuntime.InvokeAsync<Rectangle>("ZBlazorDiagrams.getBoundingClientRect", element);
}
catch (TaskCanceledException)
{
return null;
}
}

public static async Task ObserveResizes<T>(this IJSRuntime jsRuntime, ElementReference element,
Expand Down
21 changes: 21 additions & 0 deletions tests/Blazor.Diagrams.Tests/Extensions/JSRuntimeExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Blazor.Diagrams.Core.Geometry;
using Microsoft.JSInterop;
using Moq;
using System.Threading.Tasks;
using Xunit;
using JSRuntimeExtensions = Blazor.Diagrams.Extensions.JSRuntimeExtensions;

namespace Blazor.Diagrams.Tests.Extensions
{
public class JSRuntimeExtensionsTest
{
[Fact]
public async Task TestGetBoundingClientRectDoesNotThrowOnTimeout()
{
var jsRuntime = new Mock<IJSRuntime>();
jsRuntime.Setup(j => j.InvokeAsync<Rectangle>(It.IsAny<string>(), It.IsAny<object?[]?>())).ThrowsAsync(new TaskCanceledException());
var result = await JSRuntimeExtensions.GetBoundingClientRect(jsRuntime.Object, default);
Assert.Null(result);
}
}
}

0 comments on commit ce49a3b

Please sign in to comment.