From 24d06f19588c589228f1589376deb4ad64897b4e Mon Sep 17 00:00:00 2001 From: dou Date: Wed, 21 Feb 2024 12:00:48 +1100 Subject: [PATCH 1/2] GetBoundingClientRect don't throw on time out --- .../Extensions/JSRuntimeExtensions.cs | 9 +++++++- .../Extensions/JSRuntimeExtensionsTest.cs | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 tests/Blazor.Diagrams.Tests/Extensions/JSRuntimeExtensionsTest.cs diff --git a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs index 42488844..e8e5d052 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); + } + } +} From d762ab7655af5ae958acb242bf15f75f3a82b317 Mon Sep 17 00:00:00 2001 From: dou Date: Mon, 26 Feb 2024 10:04:47 +1100 Subject: [PATCH 2/2] convert tabs --- src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs index e8e5d052..0a9a7f96 100644 --- a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs +++ b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs @@ -12,8 +12,8 @@ public static class JSRuntimeExtensions { try { - return await jsRuntime.InvokeAsync("ZBlazorDiagrams.getBoundingClientRect", element); - } + return await jsRuntime.InvokeAsync("ZBlazorDiagrams.getBoundingClientRect", element); + } catch (TaskCanceledException) { return null;