From 5ac3162a8bff657b0511f869107197da0d6e2c6e Mon Sep 17 00:00:00 2001 From: Leonam Santos Date: Fri, 16 Aug 2024 13:54:28 +1000 Subject: [PATCH 1/2] Change how DiagramCanvas is disposed --- .../Components/DiagramCanvas.razor.cs | 7 ++++--- .../Extensions/JSRuntimeExtensions.cs | 13 +++++++++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs b/src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs index a6bfda57..2c177fff 100644 --- a/src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs +++ b/src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs @@ -1,6 +1,4 @@ -using System; -using System.Threading.Tasks; -using Blazor.Diagrams.Core.Behaviors; +using Blazor.Diagrams.Core.Behaviors; using Blazor.Diagrams.Core.Geometry; using Blazor.Diagrams.Extensions; using Microsoft.AspNetCore.Components; @@ -40,6 +38,9 @@ public async ValueTask DisposeAsync() await JSRuntime.UnobserveResizes(elementReference); _reference.Dispose(); + _reference = null!; + + GC.SuppressFinalize(this); // CA1816 } private string GetLayerStyle(int order) diff --git a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs index 5509f742..b77b902f 100644 --- a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs +++ b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs @@ -1,6 +1,4 @@ -using System; -using System.Threading.Tasks; -using Blazor.Diagrams.Core.Geometry; +using Blazor.Diagrams.Core.Geometry; using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; @@ -28,7 +26,14 @@ public static async Task ObserveResizes(this IJSRuntime jsRuntime, ElementRef public static async Task UnobserveResizes(this IJSRuntime jsRuntime, ElementReference element) { - await jsRuntime.InvokeVoidAsync("ZBlazorDiagrams.unobserve", element, element.Id); + try + { + await jsRuntime.InvokeVoidAsync("ZBlazorDiagrams.unobserve", element, element.Id); + } + catch (ObjectDisposedException) + { + // Ignore, DotNetObjectReference was likely disposed + } } public static async Task AddDefaultPreventingForWheelHandler(this IJSRuntime jsRuntime, ElementReference element) From f8c24c12bd0ad40d8316d3f26d1df2c4ccad0058 Mon Sep 17 00:00:00 2001 From: Leonam Santos Date: Mon, 19 Aug 2024 09:40:50 +1000 Subject: [PATCH 2/2] Catch JSDisconnected exceptions on Un-observeResizes call --- 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 b77b902f..3e32f8e1 100644 --- a/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs +++ b/src/Blazor.Diagrams/Extensions/JSRuntimeExtensions.cs @@ -30,9 +30,9 @@ public static async Task UnobserveResizes(this IJSRuntime jsRuntime, ElementRefe { await jsRuntime.InvokeVoidAsync("ZBlazorDiagrams.unobserve", element, element.Id); } - catch (ObjectDisposedException) + catch (JSDisconnectedException) { - // Ignore, DotNetObjectReference was likely disposed + // Ignore, JSRuntime was already disconnected } }