Skip to content

Commit 02baa76

Browse files
authored
Merge pull request #558 from boukenka/dispose
Handle Dispose exceptions
2 parents d547655 + 40fc841 commit 02baa76

File tree

4 files changed

+83
-35
lines changed

4 files changed

+83
-35
lines changed

src/Blazor.Diagrams/Components/DiagramCanvas.razor.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@ public partial class DiagramCanvas : IAsyncDisposable
2929

3030
public async ValueTask DisposeAsync()
3131
{
32-
BlazorDiagram.Changed -= OnDiagramChanged;
32+
try
33+
{
34+
BlazorDiagram.Changed -= OnDiagramChanged;
3335

34-
if (_reference == null)
35-
return;
36+
if (_reference == null)
37+
return;
3638

37-
if (elementReference.Id != null)
38-
await JSRuntime.UnobserveResizes(elementReference);
39+
if (elementReference.Id != null && JSRuntime != null)
40+
await JSRuntime.UnobserveResizes(elementReference);
3941

40-
_reference.Dispose();
42+
_reference.Dispose();
43+
}
44+
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
45+
{
46+
// This exception is expected when the user navigates away from the page
47+
// and the component is disposed. It can be ignored.
48+
}
4149
}
4250

4351
private string GetLayerStyle(int order)
@@ -56,12 +64,20 @@ protected override void OnInitialized()
5664

5765
protected override async Task OnAfterRenderAsync(bool firstRender)
5866
{
59-
await base.OnAfterRenderAsync(firstRender);
67+
try
68+
{
69+
await base.OnAfterRenderAsync(firstRender);
6070

61-
if (firstRender)
71+
if (firstRender)
72+
{
73+
BlazorDiagram.SetContainer(await JSRuntime.GetBoundingClientRect(elementReference));
74+
await JSRuntime.ObserveResizes(elementReference, _reference!);
75+
}
76+
}
77+
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
6278
{
63-
BlazorDiagram.SetContainer(await JSRuntime.GetBoundingClientRect(elementReference));
64-
await JSRuntime.ObserveResizes(elementReference, _reference!);
79+
// This exception is expected when the user navigates away from the page
80+
// and the component is disposed. It can be ignored
6581
}
6682
}
6783

src/Blazor.Diagrams/Components/Renderers/GroupRenderer.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.AspNetCore.Components;
99
using Microsoft.AspNetCore.Components.Rendering;
1010
using Microsoft.AspNetCore.Components.Web;
11+
using Microsoft.JSInterop;
1112

1213
namespace Blazor.Diagrams.Components.Renderers;
1314

@@ -53,17 +54,25 @@ protected override bool ShouldRender()
5354

5455
protected override void OnAfterRender(bool firstRender)
5556
{
56-
if (Size.Zero.Equals(Group.Size))
57-
return;
58-
59-
// Update the port positions (and links) when the size of the group changes
60-
// This will save us some JS trips as well as useless rerenders
61-
62-
if (_lastSize == null || !_lastSize.Equals(Group.Size))
57+
try
58+
{
59+
if (Size.Zero.Equals(Group.Size))
60+
return;
61+
62+
// Update the port positions (and links) when the size of the group changes
63+
// This will save us some JS trips as well as useless rerenders
64+
65+
if (_lastSize == null || !_lastSize.Equals(Group.Size))
66+
{
67+
Group.ReinitializePorts();
68+
Group.RefreshLinks();
69+
_lastSize = Group.Size;
70+
}
71+
}
72+
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
6373
{
64-
Group.ReinitializePorts();
65-
Group.RefreshLinks();
66-
_lastSize = Group.Size;
74+
// This exception is expected when the user navigates away from the page
75+
// and the component is disposed. It can be ignored
6776
}
6877
}
6978

src/Blazor.Diagrams/Components/Renderers/NodeRenderer.cs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ public class NodeRenderer : ComponentBase, IDisposable
2828

2929
public void Dispose()
3030
{
31-
Node.Changed -= OnNodeChanged;
32-
Node.VisibilityChanged -= OnVisibilityChanged;
31+
try
32+
{
33+
Node.Changed -= OnNodeChanged;
34+
Node.VisibilityChanged -= OnVisibilityChanged;
35+
36+
if (_element.Id != null && !Node.ControlledSize && JsRuntime != null)
37+
{
38+
_ = JsRuntime.UnobserveResizes(_element);
39+
}
3340

34-
if (_element.Id != null && !Node.ControlledSize)
41+
_reference?.Dispose();
42+
}
43+
catch (Exception ex) when (ex is JSDisconnectedException || ex is ObjectDisposedException)
3544
{
36-
_ = JsRuntime.UnobserveResizes(_element);
45+
// This exception can be ignored.
3746
}
38-
39-
_reference?.Dispose();
4047
}
4148

4249
[JSInvokable]
@@ -127,18 +134,26 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
127134

128135
protected override async Task OnAfterRenderAsync(bool firstRender)
129136
{
130-
if (firstRender && !Node.Visible)
131-
return;
132-
133-
if (firstRender || _becameVisible)
137+
try
134138
{
135-
_becameVisible = false;
139+
if (firstRender && !Node.Visible)
140+
return;
136141

137-
if (!Node.ControlledSize)
142+
if (firstRender || _becameVisible)
138143
{
139-
await JsRuntime.ObserveResizes(_element, _reference!);
144+
_becameVisible = false;
145+
146+
if (!Node.ControlledSize)
147+
{
148+
await JsRuntime.ObserveResizes(_element, _reference!);
149+
}
140150
}
141151
}
152+
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
153+
{
154+
// This exception is expected when the user navigates away from the page
155+
// and the component is disposed. It can be ignored
156+
}
142157
}
143158

144159
private void OnNodeChanged(Model _)

src/Blazor.Diagrams/Components/Renderers/PortRenderer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,17 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
8080

8181
protected override async Task OnAfterRenderAsync(bool firstRender)
8282
{
83-
if (!Port.Initialized)
83+
try
8484
{
85-
await UpdateDimensions();
85+
if (!Port.Initialized)
86+
{
87+
await UpdateDimensions();
88+
}
89+
}
90+
catch (Exception ex) when (ex is JSDisconnectedException || ex is OperationCanceledException)
91+
{
92+
// This exception is expected when the user navigates away from the page
93+
// and the component is disposed. It can be ignored
8694
}
8795
}
8896

0 commit comments

Comments
 (0)