diff --git a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs index 42488844..0a9a7f96 100644 --- a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs +++ b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs @@ -10,7 +10,14 @@ public static class JSRuntimeExtensions { public static async Task GetBoundingClientRect(this IJSRuntime jsRuntime, ElementReference element) { - return await jsRuntime.InvokeAsync("ZBlazorDiagrams.getBoundingClientRect", element); + try + { + return await jsRuntime.InvokeAsync("ZBlazorDiagrams.getBoundingClientRect", element); + } + catch (TaskCanceledException) + { + return null; + } } public static async Task ObserveResizes(this IJSRuntime jsRuntime, ElementReference element, diff --git a/tests/Blazor.Diagrams.Tests/Extensions/JSRuntimeExtensionsTest.cs b/tests/Blazor.Diagrams.Tests/Extensions/JSRuntimeExtensionsTest.cs new file mode 100644 index 00000000..bd2596fe --- /dev/null +++ b/tests/Blazor.Diagrams.Tests/Extensions/JSRuntimeExtensionsTest.cs @@ -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(); + jsRuntime.Setup(j => j.InvokeAsync(It.IsAny(), It.IsAny())).ThrowsAsync(new TaskCanceledException()); + var result = await JSRuntimeExtensions.GetBoundingClientRect(jsRuntime.Object, default); + Assert.Null(result); + } + } +}